On 1/23/06, Zach <zacharooni / comcast.net> wrote:
> I may be totally off base, being a Ruby Newbie myself, but I believe the
> point of the :variable is sort of passing a HashMap to the method...for
> instance:
>
> inside the method
>     center = { :x => 1, :y => 2 }

The fact that the right-hand side is surrounded by curly braces is
what makes it a Hash. Each key/value pair initially added to the Hash
looks like "key => value". In this particular case, they decided to
use symbols for keys which is pretty common. The colon means that the
word following it is a symbol. They could have also used String keys.

There was a long discussion about what symbols are recently, so I
hesitate to try to simplify this, but here goes. Think of a symbol as
a string that will be the same object in memory each time you use it.
For example, "foo" and "foo" will be two different objects in memory,
but :foo and :foo will refer to the same object.

> you can refer to the parameters as :x and :y.

Well, in a Hash they are called keys.

> As opposed to mandating
> the prototype of the method being center(x, y).

That's a good point. Passing a Hash to a method, in a way, allows you
to pass arbitrary parameters if you think of the keys as being
parameter names the values as being parameter values.

> The benefits I can see are variable length arguments, and the arguments
> placed don't have to be in the same order. Coming from java, I'm
> actually a little wary about this, but I sort of understand the
> usefulness, especially after reading the Rails book.

I don't think it's common in Ruby to use Hashes for this purpose.
Usually methods have fixed parameters and you don't pass them in a
Hash.

> Mage wrote:
>
> > Joe Van Dyk wrote:
> >
> >> On 1/23/06, Mage <mage / mage.hu> wrote:
> >>
> >>
> >>
> >> The second edition of the book was released two years ago or so and
> >> covers Ruby 1.8.  You should read that one instead.
> >>
> >> Here's a pretty good summary of the changes in Ruby 1.8.
> >> http://whytheluckystiff.net/articles/rubyOneEightOh.html
> >>
> >>
> > Thanx.
> >
> > One of the things I don't understand are the semicolons. When I read
> > the Book, it said:
> >
> > class SomeThing
> >  atrr_reader :x, :y
> > end
> >
> > I figured out that the attr_reader need to get the name of the
> > instance variables.
> >
> > However, what is this code good for?
> >
> > def make_point_hash( point )
> >   center = { :x => 1, :y => 2 }
> >   big = { :r => 10 }
> >   center.merge( big ).merge( point ) end
> > make_point_hash( { :x => 20 } )
> >
> > #=> {:y=>2, :r=>10, :x=>20}
> >
> > What is that those semicolons do?
> >
> >     Mage
> >
> >
> >
> >
> >
>
>


--
R. Mark Volkmann
Partner, Object Computing, Inc.