K
Karl von Laudermann
Hi, I'm a Ruby newbie (hey, that rhymes!), and I have two questions.
They're not important, but they're bugging me, and I'd like to better
understand the Ruby language.
1) In both the "pickaxe book" and The Ruby Way, it is said that when you
create a string using the %q notation, you can use *any* character as a
delimiter. So how come it doesn't work when I try to use an alphanumeric
character as a delimiter? (I'm using Ruby 1.6.7, if that matters.)
Example:
irb(main):001:0> s = %q*hello* # This works
"hello"
irb(main):002:0> s = %qzhelloz # This doesn't
SyntaxError: compile error
(irb):2: unknown type of %string
s = %qzhelloz # This doesn't
^
(irb):2: parse error
s = %qzhelloz # This doesn't
^
from (irb):2
2) In The Ruby Way, it talks about the importance of using whitespace
consistently, and gives the following examples:
x = y + z
x = y+z
x = y+ z
x = y +z
Regarding the last form, the text says, "...the parser thinks that y is
a method call and +z is a parameter passed to it! It will then give an
error message for that line if there is no method named y."
My question is, what if there is a method named y, which takes one
parameter? Is there a way that this line can be executed successfully,
without an error message? Can +z ever be a meaningful and correct
argument in a method call? Variable names can't begin with a "+"
character, correct?
Thanks in advance for any insight.
They're not important, but they're bugging me, and I'd like to better
understand the Ruby language.
1) In both the "pickaxe book" and The Ruby Way, it is said that when you
create a string using the %q notation, you can use *any* character as a
delimiter. So how come it doesn't work when I try to use an alphanumeric
character as a delimiter? (I'm using Ruby 1.6.7, if that matters.)
Example:
irb(main):001:0> s = %q*hello* # This works
"hello"
irb(main):002:0> s = %qzhelloz # This doesn't
SyntaxError: compile error
(irb):2: unknown type of %string
s = %qzhelloz # This doesn't
^
(irb):2: parse error
s = %qzhelloz # This doesn't
^
from (irb):2
2) In The Ruby Way, it talks about the importance of using whitespace
consistently, and gives the following examples:
x = y + z
x = y+z
x = y+ z
x = y +z
Regarding the last form, the text says, "...the parser thinks that y is
a method call and +z is a parameter passed to it! It will then give an
error message for that line if there is no method named y."
My question is, what if there is a method named y, which takes one
parameter? Is there a way that this line can be executed successfully,
without an error message? Can +z ever be a meaningful and correct
argument in a method call? Variable names can't begin with a "+"
character, correct?
Thanks in advance for any insight.