Reputation: 9496
I have got a code which if,unless block a number of times like this:
Option is hashmap.
unless functioncall? options[:product]
puts "Hello wrold"
end
Can anyone explain the flow of this unless block. I am new to ruby and I use curly braces block in place of this.
Upvotes: 0
Views: 1580
Reputation: 2432
In if
syntax, this is equivalent to:
if functioncall?(options[:product])
else
puts "Hello World"
end
OR
if !functioncall?(options[:product])
puts "Hello World"
end
The method functioncall?(options[:product])
will return true if the options hash has a key called product
.
Depending on the contents of the options hash, the method above will produce:
# options = { :key => value, :product => 'stuff'}
functioncall?(options[:product]) #=> true
# the output of your code would be nil
# options = { :key => value, :foo => 'bar'}
functioncall?(options[:product]) #=> false
# the output of your code would be "Hello World"
Upvotes: 2