What is the meaning of the if / except or modifier?

I couldn't find a formal specification of the modifier if

(or unless

) anywhere :

123 if x > 0

      

What is the meaning of the above statement if x

not greater than zero? irb

suggests nil

, but is it documented somewhere?

(yes this is probably a stupid question, sorry, couldn't find the spec).

+3


source to share


2 answers


Yes it is nil

. What else can be returned? The statement in the expression is if

used for the if itself and the statement is then

not executed. There is nothing to return, therefore nil

.

Corresponding specification



it "returns nil if else-body is empty and expression is false" do
  if false
    123
  else
  end.should == nil
end

      

+3


source


An expression that is not evaluated is the same as one that does not exist. And your condition should be part of some block of code like definition or main environment etc. A block of code without content is evaluated as nil

.

class A; end
# => nil

def foo; end
foo
# => nil

()
# => nil

begin; end
# => nil

eval("")
# => nil

      



So, the reason your example is returning nil

has nothing to do with the condition. This is simply due to the lack of evaluation.

+4


source







All Articles