Ruby code not getting result [closed] - ruby

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So heres what I'm using
puts "How old are you?"
age = gets
if age == 10
puts "Yo"
end
I expect to see Yo if I enter 10, but don't. I'm fairly new, any ideas?

Yes, change it to
age = gets.to_i
Kernel#gets gives a string, you need to convert it to integer to do the integer comparisons. Read String#to_i method too.

You are typing a string and expecting it to return an integer
Do this:
puts "How old are you?"
age = gets.chomp
if age == "10"
puts "Yo"
end

Related

In Ruby how can I check if a symbol is in an array of symbols? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Can I do the following? If so how?
I have an array of symbols
symbols = %w{:sym1 :sym2 :sym3} # is this correct implementation?
# I'm putting this in a function for this illustration
def check_symbol(symbol)
symbols.include?(symbol)
end
puts check_symbol(:sym1) # expect true, but I get false
puts check_symbol(:sym44) # expect false of course
How can I do so I get true on the first puts statement?
You can specify an array of symbols as %i[ ... ].
And to check if your symbols is in an array of symbols you could use all? to check if all they respond with true to .is_a?(Symbol) and then if the array includes your specific one, like:
array_of_symbols = %i[sym1 sym2 sym3]
p array_of_symbols.all? { |e| e.is_a?(Symbol) } && array_of_symbols.include?(:sym1)
# true
You don't have an array of symbols, what you have is an array of strings that look like symbols.
The correct definition would be
symbols = %i{sym1 sym2 sym3}

What is the syntax error in this short ruby block? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
What is the syntax error in this short ruby block?
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this (1,2,3)
end
end
do_this (4,5,6)
I get errors on the fourth and seventh lines, where "do_this" is called.
The error is: 'unexpected ")", expecting "." or...' [...]
Remove spaces between method name and parentheses.
You need to avoid using spaces between the method call and the arguments in parenthesis:
def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this(1,2,3)
end
end
do_this(4,5,6)

unexpected tidentifier expecting keyword_end [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I've got a problem in Ruby, "unexpected tidentifier expecting keyword_end", how Can I solve it?
def riko(user)
if user.name.eql? 'Mia Khalifa Fan'
#client.send_msg 'Hola Mia <3 ¿Cómo te trato este dia, cosa guapa y sensual?',
else
if user.mame.eql? 'Skul Goy'
#client.send_msg 'Muerete. '
else
#client.send_msg "Hola #{user.name} o/ \ :v / "
end
end
You're using else if which works fine in other languages, but in Ruby represents 2 distinct conditionals. You probably want to replace it with elsif instead, which is the Ruby equivalent.

Use of block passed to Hash#fetch [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Hash#fetch returns the value in a hash for the given key.
It also accepts an optional block:
h = { "a" => 100, "b" => 200 }
h.fetch("a") #=> 100
h.fetch("z", "go fish") #=> "go fish"
h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
One would assume that a block will help us work on the value associated with given the key. However, the block seems to yield the key instead of the value (refer to the third call of fetch above).
What is the use of such implementation? I don't see any point in key being passed to the block as it is already known; it is the value that one is interested in, as is evident by call to fetch.
UPDATE: This is invalid question, I misread the documentation. My apologies
The block is used for dealing with missing values - the value is not yielded because there isn't one.
You're supposed to use the key to return a suitable value (or raise an exception if appropriate)

Iterating over hash prints hash? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm iterating over a bunch of nested hashes in Ruby with this:
#data.each do |key, value|
puts "Key: #{key}"
puts "Value: #{value}"
end
with the output:
Key: 1.0
Value: {"label"=>"Default Label"}
{"1.0"=>{"label"=>"Default Label"}}
Now I don't really understand why that last line is printed. It even gets printed when I remove the two "puts" calls from the loop.
I tried to find something about this behaviour online but couldn't find anything.
Any way I can prevent this from happening? Or am I misunderstanding the "each" call on the hash?
You're doing this in the REPL.
The REPL, by default, returns (and prints) the value of the last statement executed.
In this case, it's each, which returns the collection it iterated over, so you see your original hash.

Resources