From: David Alan Black Date: 2001-10-29T08:50:29+09:00 Subject: [ruby-talk:23700] Re: newbie syntax question? Hello -- On Mon, 29 Oct 2001, Chochain Lee wrote: > Hi, > > I ran into something like the following code which gave me a small > surprise. Can some explain why? > > puts (123 && 456).type # => 456 > puts ((123 && 456).type) # => Fixnum In the first one, you're asking for Ruby to evaluate the type of the expression "puts (123 && 456)". As a side effect, that expression prints the value of (123 && 456) (which is 456). And then it evaluates the type (which actually is NilClass, because that's the return value of puts). But it doesn't do anything with it :-) In other words, your first example is parsed as: (puts(123 && 456)) .type In the second one, by grouping all that stuff together you're asking Ruby to, *first*, to evaluate (123 && 456), *then* to determine the type of the result (which is Fixnum), and *then* to print out the result of that determination. If you rewrite the first one like this, you might see more clearly what's happening: puts(123 && 456) .type The (123 && 456) binds more tightly to puts than it does to .type. In fact, if you use the -w flag, Ruby will warn you about this: candle:~$ ruby -we 'puts (123 && 456).type' -e:1: warning: puts (...) interpreted as method call 456 That warning means: even though the gap before the "(" makes it look like the (...) part binds with ".type", it really binds as (puts(...)) .type. (The way not to get this warning is to not put a space between the method name and the parens.) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav