Prop = x => y

Why cant I do things like:

my_prop = :foo => :bar

when its possible to do:

my_meth :foo => :bar

??

ive tried with
my_prop = (:foo => :bar)
to but no luck…

//Roger

also , is it wrong to use the x => y construct in other context than
describing hashtables?

in my case my idea was to associate symbols with values in a sort of
.net attribute/annotation way

"
meta.some_prop= :parent_of => [:sometype,:someprop]
attr_accessor :some_prop
"

it works just fine for everything except for set properties. (whatever
you might call them in ruby)

"
meta.my_meth :foo => :bar
def my_meth()
end
"

works just fine…
(the “meta” is an extended method on Class which returns an attribute
object which in turn handles method_missing and then stores the
class,methodid and data in a static lookup)

Hi –

On Sat, 24 Jun 2006, Roger J. wrote:

ive tried with
my_prop = (:foo => :bar)
to but no luck…

You need to use the hash literal constructor:

my_prop = { :foo => :bar }

The only time you’re allowed not to use it, and still have Ruby know
that you’re trying to construct a hash, is the special case of a
method call:

my_meth :a => “b”, :c => “d”

which is equivalent to:

my_meth({:a => “b”, :c => “d”})

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!

When you do my_method :foo => :bar Ruby turns this in to
my_method({:foo => :bar}) to provide a pseudo-keyword-argument system.
This isn’t legal anywhere else in Ruby code.

Hi –

On Sat, 24 Jun 2006, Roger J. wrote:

also , is it wrong to use the x => y construct in other context than
describing hashtables?

I don’t know of any meaning for => other than hash separator.

class,methodid and data in a static lookup)
Be advised: nothing is static in Ruby :slight_smile:

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!