yes = "yes"
no = "no"
x = nil
p x.nil? yes: no
Hmm. Not interesting?
Well, the main thing here is that the ‘?’ and ‘:’ are part of the
accompanying symbols so they would be classed as part of the symbol name.
Ruby already does this for symbols ending in ‘?’. I don’t see what the
problem is. If we were talking about a C/C++ parser then that’s a different
matter…
Really? That surprises me about Ruby … lets see:
irb(main):014:0> "".size ? true : false
true
irb(main):013:0> "".size? true : false
SyntaxError: compile error
(irb):13: parse error
"".size? true : false
^
from (irb):13
Okay, I guess you’re right. If a method is defined having a
trailing ‘?’ then whitespace matters. Totally surprised me.
But then, this is funny:
irb(main):003:0> 1 ? true : false
true
irb(main):002:0> 1?true:false
true
There IS no method #1? defined. Why doesn’t the second code
snippet also generate a parse error? Interestingly:
irb(main):015:0> true? true : false
SyntaxError: compile error
(irb):15: parse error
true? true : false
^
from (irb):15
irb(main):016:0> true?true:false
SyntaxError: compile error
(irb):16: parse error
true?true:false
^
from (irb):16
irb(main):017:0> true ? true : false
true
I would not have expected the middle code snippet to generate
a parse error (given that “1?true:false” doesn’t) … since
#true? isn’t a defined method, either.
This is all using:
$ ruby -v
ruby 1.6.7 (2002-03-19) [i386-linux]
It also happens in Ruby 1.7.2 as well:
$ ruby-1.7.2 -v
ruby 1.7.2 (2002-05-30) [i686-linux]
Also, ‘yes:’ would only be valid inside a method argument list and so would
cause a parsing error in this case.
Why would it cause a parsing error? Really, it would cause an
“ArgumentError: wrong # of arguments(1 for 0)” as #nil?'s arity
is 0, but (if the named parameter “foo: bar” was implemented)
you would be passing it 1 arg, the object referenced in variable
“no” as the “yes” parameter to #nil? which doesn’t exist.
– Dossy
···
On 2002.08.12, Justin Johnson justinj@mobiusent.com wrote:
–
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)