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

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

Related

How to ask a number in Ruby? [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 want to make a program like,ask a number and print 1 to number by using gets and by using loop. So,I am asking about gets and how to do program which is I given below as program title.
How to ask a number by using gets?If possible explain me with example.
By using gets,I want to print 1 to number. My program titleis Ask a number and print 1 to number by using Ruby.
How can I solve that program?Please help me on this.
As Arup, suggested use Kernel#gets to capture a user input from terminal. The remaining bit can be simply done with a for loop:
num = gets.to_i #Convert the user input to integer
for i in 1..num
puts i
end
You can further modify this to suit your need.
Do as below using Kernel#gets. #gets will give you a string, then to convert the number string to a number use String#to_i.
number = gets.to_i
If I want to make program which from 1 to number then what should I do?
Use a Range then.
(1..number).each do |n|
# code
end

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

How to check if a character appears in a string exactly one time or multiple times? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
So say I have three strings. I am trying to check if the letters in two appear EXACTLY ONCE in "doopdedoo", and if the letters in three appear an unlimited amount of times.
one = "doopdedoo"
two = "dp"
three = "o"
if one.{|a| a.chars.all? {|c| "#{three}".include?(c)}} && one.{|a| a.chars.once? {|c| "#{two}".include?(c)}}
I have used the above to test for the presence of an unlimited amount of o's. How to test for a limited amount of d's and p's?
Edit:
Sorry but I need to clarify. My expected output would be nothing for this case.
[]
Because doopdeedoo contains more than one instance of d or p.
It does contain many o's, so that's fine.
I also added the &&... part to the method above. I realize there is no 'once' method but if there is something like that I'd like to use it.
You can use the String#count method like this:
test_string = "foopaad"
must_appear_once = ['d', 'p']
must_appear = ['o']
must_appear_once.all? {|c| test_string.count(c) == 1} \
and must_appear.all? {|c| test_string.count(c) > 0}
This ensures that 'd' and 'p' each appear exacly once and that 'o' appears in the string (no matter how often).

ruby cut out text from string to arrays [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 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.

Ruby -- 'gets' adds newline character [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 this:
print "Enter your name:"
name = gets
puts "Hello #{name}. Please to meet you."
and the result was like this:
Hello Moemen
. Pleased to meet you
Why is the remainder of the string after the variable continued in another line? I want it to be "Hello Moemen. Pleased to meet you." Am I missing something?
I'm using sublime text 2, and I couldn't get the gets method to let me input data; it just prints the outcome in the console without giving me a chance to input anything. Any idea?
When you use gets, the input is going to be followed by a newline so you will need to strip the newline before printing it out.
print "Enter your name:"
name = gets.chomp
puts "Hello #{name}. Pleased to meet you."
That should solve your problem.
What you report would not happen. It cannot be true. When you get a string by gets, you may get a string like "Moemen\n". Since an input is delimited by "\n", the string has that at the end. When it is interpolated into "Hello #{name}. Please to meet you.", you would get:
Hello Moemen
. Please to meet you.
but not
Hello Moemen
. Pleased to meet you
as you report. That does not happen.
In order to get "Hello Moemen. Pleased to meet you.", you need to change the string to "Hello #{name.chomp}. Pleased to meet you.".

Resources