Sahil
Sahil

Reputation: 9496

Alternate If-Else Block in ruby

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

Answers (1)

jstim
jstim

Reputation: 2432

translation to if-syntax

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

Output of boolean method

The method functioncall?(options[:product]) will return true if the options hash has a key called product.

Output of your method

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

Related Questions