ARTICLE AD BOX
The answer is right there in the question :) "Ruby will return the last calculated code if the return keyword is not specified".
In your first version, the last calculated code is the block in if args[-1].class == String. That block will result in nil if it evaluates to false and whatever its contents are otherwise. You can test the first version of the code with a numeric input to verify.
In your second version, the last calculated code is the block in if args[-1].class == Integer. The first block is calculated, but the result is simply lost. The two solutions would be (1) to use return explicitly, or (2) to use else if instead:
def aMethod(*args) if args[-1].class == String if args.length > 1 args[-1] elsif args.length == 1 args[-1][-1] end elsif args[-1].class == Integer args[-1] end endOr even better, use case:
def aMethod(*args) case args[-1] when String if args.length > 1 args[-1] elsif args.length == 1 args[-1][-1] end when Integer args[-1] end endIn either case, note that any un-captured cases will return nil. For example, if your input is of a different class, or if it's a String of length 0 ('').
3 Comments
In Ruby, if is an expression, i.e. it can be evaluated and – if so – it returns a value, e.g.:
result = if true 123 end result #=> 123likewise:
result = if false 123 end result #=> nilRuby will return the last calculated code
More specifically, it will return the result of the last expression that was evaluated. In your method, the last expression is an if and its result will become the return value.
Applied to your code, it basically works like this:
def aMethod(*args) result = if args[-1].class == String if args.length > 1 args[-1] elsif args.length == 1 args[-1][-1] end end return result endIt you add another if afterwards, you have another expression which now determines the method's return value. You can think of it as every expression producing a result and the last result will become the method's return value:
def aMethod(*args) result = if args[-1].class == String if args.length > 1 args[-1] elsif args.length == 1 args[-1][-1] end end result = if args[-1].class == Integer args[-1] end return result end115k14 gold badges157 silver badges233 bronze badges
Explore related questions
See similar questions with these tags.
