Truthiness in Ruby

12 views
Skip to first unread message

Kris Luminar

unread,
Mar 28, 2014, 11:01:12 AM3/28/14
to iowaru...@googlegroups.com
This makes no sense to me. Why does Ruby return the the array `[1]` from the below example instead of `false`

1.9.3 > true && [1]
=> [
  [0] 1
]

It behaves as I expect for `false`:
1.9.3 > false && [1]
=> false

David W. Body

unread,
Mar 28, 2014, 11:59:11 AM3/28/14
to iowaru...@googlegroups.com
Kris,

Maybe it's a bit weird, but && and `and` return their first argument if it is false. Otherwise they evaluate and return their second argument.

Similarly, || and `or` return their first argument if it is truthy. Otherwise they evaluate and return their second argument.

nil && [1]        # => nil
false && [1]      # => false
"hello" && [1]    # => [1]

nil || [1]        # => [1]
false || [1]      # => [1]
"hello" || [1]    # => "hello"

Of course in Ruby, nil and false are false, and everything else is truthy.

--David



--
You received this message because you are subscribed to the Google Groups "Iowa Ruby Brigade Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iowaruby-tal...@googlegroups.com.
To post to this group, send email to iowaru...@googlegroups.com.
Visit this group at http://groups.google.com/group/iowaruby-talk.
For more options, visit https://groups.google.com/d/optout.

Nick Parker

unread,
Mar 28, 2014, 11:59:16 AM3/28/14
to iowaru...@googlegroups.com
Hi Kris,

I believe it is just returning the last valid expression that does not fail.  With that:

false && [1]

terminates at false and would not evaluate [1]

true && [1]

simply returns [1] within irb for me.

true && [1] && [1,2]

returns [1,2] for me.

true && [1] && [1,2] && 2 + 3

returns 5
On Fri, Mar 28, 2014 at 10:01 AM, Kris Luminar <kris.l...@gmail.com> wrote:

--

Charles Hoffman

unread,
Mar 28, 2014, 12:02:18 PM3/28/14
to iowaru...@googlegroups.com
This is completely the intended and expected behavior, and is common to many programming languages, and is behind some common programming idioms.

Kris Luminar

unread,
Mar 28, 2014, 3:48:16 PM3/28/14
to iowaru...@googlegroups.com
Ah, so Ruby returns the last argument that determined the truthiness of the expression, not the truth value of the expression itself. If that argument happened to be `false`, or `true` then it looks like Ruby returns the truthiness of the statement but it's really returning that argument.

Thanks!

Charles Hoffman

unread,
Mar 28, 2014, 4:11:51 PM3/28/14
to iowaru...@googlegroups.com
Yep. Commonly used as a shorthand for doing things conditionally, such as:

i.odd? && i = i - 1      # subtract 1 from i only if i is odd
file_open? || open_file  # open file only if we didn't already

usually in these cases though, the thing you want on the right in something you want for its side effects rather than its value, because if you wanted to use this for its value within an expression you're stuck dealing with the fact that it may or may not return a Boolean

Charles Hoffman

unread,
Mar 28, 2014, 4:20:14 PM3/28/14
to iowaru...@googlegroups.com
The word versions "and" and "or" also do this, but bind differently ("higher") which can be confusing. Usually the advice is to just not use "and" and "or" ever. If that's good enough for you and you don't want the confusing answer, don't read further :D : 

"and" and "or" operate at more of a "statement" level when parsing, so they can be used to do this kind of short-circuiting where the expression on the left might have || or && in it. Something like: " !file_exists? || file_empty? and raise "No data" " Sometimes in old perl scripts you see "or die" used to exit on an error like "open_file or die("no file!")"
Reply all
Reply to author
Forward
0 new messages