[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie questions

Karl von Laudermann

10/27/2003 11:56:00 PM

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.

--
-- Karl J. von Laudermann -- karlvonl(at)rcn.com --
-- <http://www.geocities.com/~kar... --
"It will take many years, but [Microsoft] will eventually have to compete. It'll
be a whole new world for them. I'm looking forward to it."- Larry Ellison
2 Answers

Lyle Johnson

10/28/2003 1:26:00 PM

0

Karl von Laudermann wrote:

> 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?

The definition for such a method would look something like this:

def y(value)
2*value
end

> Is there a way that this line can be executed successfully,
> without an error message?

Yes; as suggested by the text that you quoted from The Ruby Way, if you
first define the "y" method, and then try to execute that line, you
shouldn't get an error message:

def y(value)
2*value
end

z = 4
x = y +z (now x = y(+z) = 2*(+4) = 8)

> Can +z ever be a meaningful and correct argument in a method call?
> Variable names can't begin with a "+" character, correct?

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). His more outgoing brother is the "unary minus"
operator, which multiplies the following value by negative one and thus
flips its sign, which is usually the more interesting case.

Karl von Laudermann

10/29/2003 11:58:00 PM

0

In article <3F9E6E51.8030807@knology.net>,
Lyle Johnson <lyle@knology.net> wrote:

> 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.h...)
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.

--
-- Karl J. von Laudermann -- karlvonl(at)rcn.com --
-- <http://www.geocities.com/~kar... --
"It will take many years, but [Microsoft] will eventually have to compete. It'll
be a whole new world for them. I'm looking forward to it."- Larry Ellison