Array not working with the modulo operator [closed] - ruby

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
The first code works, but I don't understand why the second one doesn't. Any insight would be appreciated. I know in this example I really don't need an array, I just wanted to get it to work for the sake of learning.
def stamps(input)
if input % 5 == 0
puts 'Zero!'
else
puts 'NO!'
end
end
print stamps(8)
But this doesn't work:
array_of_numbers = [8]
def stamps(input_array)
if input_array % 5 == 0
puts 'Zero!'
else
puts 'NO!'
end
end
print stamps(array_of_numbers)

Because input_array is an array and 8 is a number. Use first to retrieve the first element of the array.
array_of_numbers = [8]
def stamps(input_array)
if input_array.first % 5 == 0
puts 'Zero!'
else
puts 'NO!'
end
end
print stamps(array_of_numbers)

The following function works in case the input is number or array:
def stamps(input)
input = [input] unless input.is_a?(Array)
if input.first % 5 == 0
puts 'Zero!'
else
puts 'NO!'
end
end

Related

Ruby Loop Countdown method not Counting down [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 6 years ago.
Improve this question
I'm trying to define a method that countdowns 10 to 0 and at the end returns HAPPY NEW YEARS! however i don't want i"am doing wrong?
def countdown(number)
while number > 0
puts "#{number} SECONDS(S)!"
number -= 1
end
"HAPPY NEW YEAR!"
end
A quick Google search revealed that you are apparently trying to solve https://learn.co/lessons/countdown-to-midnight (you really should have included that link)
Your code is not passing the spec, because it contains an additional S:
puts "#{number} SECONDS(S)!"
# ^
It has to be:
puts "#{number} SECOND(S)!"
def countdown(number)
while number > 0
puts "#{number} SECONDS(S)!"
number -= 1
end
puts "HAPPY NEW YEAR!"
end
I added a puts on the last line of your code. You method is counting down to 0 seconds, but the last line only return the string "HAPPY NEW YEAR!", and does not print it to the screen. Or if you need to return the string and print it on the screen, you can do p "HAPPY NEW YEAR!"

How can I produce one whole string from iteration on array of arrays 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 have array of arrays looking something like this :
arr = [[f,f,f,f,f], [f,f,t,f,f], [f,t,f,t,f]]
and am I outputing it formatted on the console like this:
arr.each {|a| puts a.join.gsub('t','<b></b>').gsub('f','<i></i>')}
and it generates something like this:
<i></i><i></i><i></i><i></i><i></i>
<i></i><i></i><b></b><i></i><i></i>
<i></i><b></b><i></i><b></b><i></i>
but it is only in the output. I am wondering how I can assign it to a string? With the new lines and everything, exactly the way it looks,
a= [["f","f","f","f","f"], ["f","f","t","f","f"], ["f","t","f","t","f"]].map do |arr|
arr.join.gsub(/[ft]/) do |x|
if x =~ /f/
'<i></i>'
elsif x =~ /t/
'<b></b>'
end
end
end.join("\n")
puts a
# >> <i></i><i></i><i></i><i></i><i></i>
# >> <i></i><i></i><b></b><i></i><i></i>
# >> <i></i><b></b><i></i><b></b><i></i>

How to get keys from hash 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
The next code search if keyword appear in hash values and print yes if so,
but it works well in codeacademy console, but in my Rubymine it give me exception
NoMethodError: undefined method `keys' for nil:NilClass
I've tried to use each_key method but it was the same rusult.
arr = [
{ name: "2222", num:"4444 kod"},
{ name: "3222 kod", num:"43423444"},
{ name: "224422", num:"4442424"}
]
p = "kod"
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
esle
puts "no"
end
end
Can you give some advice?)
You have 2 mistakes:
You wrote esle instead of else
You are missing one end clause
Your blocks need end keyword. And else should be spelt correctly.
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
else
puts "no"
end
end
end

Ruby Trying to get a 3 line return [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
Hi I'm trying to get the program to return all three lines on top of one another when a 5 is entered. It's only returning the third line. This is Ruby. (My first time trying it)
moveOne = gets.to_i
if moveOne == 5
puts = "1,2,X"
"4,O,6"
"X,8,9"
puts is a method that accepts one or more arguments and writes them (their #inspect-ed value) to an IO object separated by a newline. As written you are trying to assign puts a value rather than passing the values as parameters.
Try this
puts "1, 2, X",
"4, 0, 6",
"X, 8, 9"
That's passing three strings to puts and preserves your desired readability.
moveOne = gets.to_i
if moveOne == 5
puts "1,2,X"
puts "4,O,6"
puts "X,8,9"
end
P.S: Please try to learn more before posting a question.

Dynamically create variables 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 was hoping something like this would work:
while i < 3 do
puts i
#b[i] = Benchmark.new
i += 1
#a += 1
end
puts "Here is a #{#a}"
puts #b0.inspect
puts #b1.inspect
puts #b2.inspect
Sadly, it doesn't work at all. []= is regarded as an unrecognised method!
You can also use instance_variable_set
3.times{|i| instance_variable_set "#x#{i}", i }
#x1 # => 1
#x2 # => 2
Though for this particular task you should use arrays, it's a rookie mistake to use lots of variables instead of lists.
benchmarks = []
n.times { benchmarks << Benchmark.new } # or benchmarks = (0..n).map { Benchmark.new }
benchmarks.each do |bm|
# do stuff
end
This is clearly a job for an array, not for many instance variables.
benchmarks = number.times.map { Benchmark.new }
puts "Here is a #{number}"
benchmarks.each { |b| puts b.inspect }
Answered my own question! The eval method is the answer:
puts "Welcome to Benchmark 1.0! How many benchmarks do you wish to perform? (We recommend three)"
number = gets.chomp.to_i
#a = 0
i = 0
while i < number do
puts i
eval("#b#{i} = Benchmark.new")
i += 1
#a += 1
end
puts "Here is a #{#a}"
puts #b0.inspect
puts #b1.inspect
puts #b2.inspect

Resources