ruby cut out text from string to arrays [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have code code like
#!/usr/bin/ruby
require 'open-uri'
require 'nokogiri'
def bash_org()
bash = Nokogiri::HTML(open("http://bash.org/?random"),'utf-8')
bash = bash.css("p[class='qt']").text
print(bash.gsub("\n","").gsub("\t",""))
end
def print(text)
if text.include? "\r"
text = text.split("\r")
text.each do |line|
if !line.empty?
puts line
end
end
else
text = text.split("<")
text.each do |line|
if !line.empty?
puts "<#{line}"
end
end
end
end
Everything is working great except I can not distinguish single quotes that are between class="qa" tags.
I would like to extract single quotes from bash random page and put them into separate arrays.

Ok, nevermind my comments. I think I've just noticed it.
Currently your code gets "all the qt tags" and extracts the text from them as whole. This way, you get one huge blob of text that in fact cannot be "separated" into single posts.
You have all the bits in place, but you've joined them wrong. Nokogiri's css operator returns a collection of matches. Just iterate over it instead of .texting it.
Please review this one:
def bash_org()
bash = Nokogiri::HTML(open("http://bash.org/?random"),'utf-8')
tags = bash.css("p[class='qt']") #<-- no .text here!
tags.each{|tag| #<-- loop over them!
txt = tag.text #<-- text'ize them one-by-one
print(txt.gsub("\n","").gsub("\t",""))
puts '------'
}
end
Note the indicated differences. Once you notice that the "tags" are collection of "tag" objects, it'll be obvious.

Related

How do I add a string at the end of another string if there is not match using sub [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to add string1 to the end of an string2 if string2 does not include string1.
Can I do this using sub?
I tried a few combinations but they are not doing anything. I'm not even sure I'm doing it right. I'm very bad at Regex and would really appreciate some help.
Generally you'd use this pattern:
string = "test"
insert = "er"
string << insert unless (string.match(insert))
# => "tester"
Why do you need a sub? you can just do something like:
a = "abcd"
b = "bc"
c = a + b unless a.include?(b)

how to enter value in an array during runtime using ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
how to enter the values in an array dynamically and to use it
and I'm trying to reverse it after getting the input from the keyboard.
This should get you started:
array = []
puts "Please enter each item on a separate line, then"
puts "end the input by hitting ENTER on an empty line."
while line = gets.chomp
break if line.empty?
array << line
end
puts "You entered:"
puts array.reverse
You really need to read a basic tutorial about Ruby, I have recommended this one to a few people and they liked it: https://pine.fm/LearnToProgram/.
If you want to reverse-load an array (that is, load it in reverse order, so oldest is last) then use 'unshift' while you're adding the array elements.
array.unshift = gets.chomp

ruby how to use #capitalize! and keep string numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
so I'm doing prep work for a ruby dev bootcamp and need to create a program that will capitalize titles. My current code uses #capitalize! but when a number is included in the string it is omitted.
words = title.split(' ')
words.map! do |word|
if %w(a aboard about above absent across after against along alongside amid amidst among amongst an and around as aslant astride at
athwart atop barring before behind below beneath beside besides between beyond but by despite down during except for from in inside
into like mid minus near next nor notwithstanding of off on onto opposite or out outside over past per plus regarding round save
since so than the through throughout till times to toward towards under underneath unlike until up upon via vs. when with within
without worth yet ).include?(word) && word != words[0]
word
else
word.capitalize!
end
so when what I wish I knew when I was 20 is input I get What I Wish I Knew When I Was
any suggestions?
Use capitalize instead of capitalize!.
By the way, if your intention of word != words[0] is to leave any word in the list uncapitalized if it is not the first word, then you are wrong. It does not work like that. The reason is left to you as a homework.
just change word.capitalize! to word.capitalize! || word
"20".capitalize! #=> nil
"20".capitalize! || "20" #=> 20

exception in if statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wrote a searcher function in ruby for a class that iterates on a list of people (named #personList).
It faces an exception.
My Code‌ :
def search (nCode)
for x in #personList
if x.nCode == nCode
x.to_s
end
end
I wrote the same code using each, it also faced the same exception.
What's wrong with my code ?
(I'm new to Ruby! and I couldn't solve that)
This if is delimited by a pair of newlines:
x.to_s if x.nCode == nCode
This if needs an explicit end:
if x.nCode == nCode
x.to_s
end
You have opened three blocks (def, for..in, if) in your first sample, but only closed two. Your indentation indicates the if is the one you didn't close. Thus, you have a syntax error. Otherwise, both samples are identical in functionality.
Ruby if requires an end to make it complete. If you use end for your exception code, it will work.
if x.nCode == nCode
x.to_s
end
For second case, that worked or you, it is actually completes the condition and the execution in one line. That's why it didn't require any end, and it worked for you.
You'll find more detail about if from here.

Ruby String/Array Write program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to write a program in ruby programming language which prints the longest name among others,use the split method,size max ,length.
This is what I have so far:
name = gets.chomp.split
name.each do |x|
puts x.size
for i in 1..x.size do
puts i.max
end
end
Use a a variable which is initially an empty string.
max_name = ""
When you are inside the loop, check if each x.size is larger than max_name.size. If that is the case, you have found a new max_name, so do max_name = x.
The code fails when trying to get the maximum of the integer 1. That's an odd-looking guess at the correct code, and means you should probably revise how Ruby's blocks work (you appear to be expecting an interaction between the max and each that really doesn't exist).
The usual way to get the maximum of something from a list, if you are not allowed to use built-ins, is to set a "current maximum" value and then scan through the list, checking each item to see if it is larger than the current. If it is, set the current value to that instead. At the end, you will have the largest item.
name = gets.chomp.split
current_max = ''
name.each do |x|
if x.size > current_max.size
current_max = x
end
end
puts current_max

Resources