Newbie questions

  • Thread starter Karl von Laudermann
  • Start date
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.
 
K

Karl von Laudermann

Lyle Johnson said:
The "+" character isn't modifying the variable name (i.e. the variable
name is still "z"). This is what is known as the "unary plus" operator,
which practically speaking just multiplies the following value by
positive one (ho hum).

Ah, that's what I was missing. Thanks. I forgot that some languages have
a unary plus operator, since it doesn't do anything.

I believe I found the answer to my first question as well. In the Ruby
Language Reference Manual, written by Matz himself, the section on
string literals (found at http://www.ruby-lang.org/en/man-1.4/syntax.html#string)
says, "Any non-alphanumeric delimiter can be used...". So I guess that
the pickaxe book and The Ruby Way are misleading in that they neglect to
use the "non-alphanumeric" qualifier. Of course, the Language Reference
to which I refer is for version 1.4.6 of Ruby, so it's possible that
this was supposed to have changed since then.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top