This is odd, I was trying to use “and” to chain together some
statements, which I thought would work, but it ends up, it doesn’t.
I tried “and”, and was surprised that it didn’t execute the second
half of the statement
I tried “&&” with some extra parenthesis just to see (although I was
pretty sure and was syntactic sugar for &&), and still no joy.
I tried “&”, and it works with some statements, but not with return
statements.
I know I can do this using a multi-line if, but I’m rather fond of
single line, sentence like statements.
I also tried “or”, but that makes for slightly nonsensical statement,
so I’m leaving out that option. #This works, but makes a cumbersome statement. #puts “#{x} is divisible by two” or return false if x%2==0
It begs the question, just how does and work, if it doesn’t continue
executing the right half of a statement, even when the left half was
fine?
Thanks,
Kyle
def something(x)
puts “#{x} is divisible by two” and return false if x%2==0
puts “#{x} makes me happy”
true
end
def something_different(x)
(puts “#{x} is divisible by two”)&&(return false) if x%2==0
puts “#{x} makes me happy”
true
end
def something_that_doesnt_work(x)
(puts “#{x} is divisible by two”)&(return false) if x%2==0
puts “#{x} makes me happy”
true
end
def something_longer(x)
if x%2==0
puts “#{x} is divisible by two”
return false
end
puts “#{x} makes me happy”
true
end
Mac,
If your two arguments were methods however, and the first one
succeeded, wouldn’t you expect it to execute the second?
The problem is that puts returns nil, which causes the boolean logic
to short-circuit and not even bother with the second.
I take that back. A problem is that puts returns nil. The problem
is that you’re trying to do too much on one line.
if x % 2 == 0
puts 'even'
return false
end
It’s possible to use ‘and’ and ‘or’ and post-condition (statement
modification) ‘if’ or ‘unless’ for clever flow control, but it often
turns out messier-looking than you think.
I should have realized & was bitwise and. Interesting how it works
with nil though.
This thread made me look up NilClass in the docs. I learned something
new.
& is a method of nil, which always returns false.
nil & return(something)
doesn’t work because the & method expects an argument, not a keyword…
I’d rather not redefine puts just to write things in a certain way.
I often do:
puts “error” or exit if condition
which reads a little funny, but it works and is concise.
(I hate reading code where the error handling takes up way more space
than
the essential logic)
Mac,
If your two arguments were methods however, and the first one
succeeded, wouldn’t you expect it to execute the second?
No, because “and”, “or”, “&&” and “||” short circuit - for good reason!
This allows for safer and more efficient code. Consider
foo = … # may be nil
foo and foo.do_something
If foo is nil you do not want #do_something to be invoked on it because
it will raise an exception. Also, it is not worthwhile to invoke it
because regardless of return value the expression will be false (because
foo is false).
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.