Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > syntax quickie

Reply
Thread Tools

syntax quickie

 
 
Kev Jackson
Guest
Posts: n/a
 
      10-24-2005
Hi all,

What exactly does the following do and how can I test it? It looks like
its creating a new value with the key name, but how do you call it?

def [](name)
@val[name]
end

Any help appreciated

Kev




 
Reply With Quote
 
 
 
 
Christophe Grandsire
Guest
Posts: n/a
 
      10-24-2005
Selon Kev Jackson <(E-Mail Removed)-vn.com>:

> Hi all,
>
> What exactly does the following do and how can I test it? It looks lik=

e
> its creating a new value with the key name, but how do you call it?
>
> def [](name)
> @val[name]
> end
>
> Any help appreciated
>


Just call it using array notation: foo[name]. Array notation is just synt=
actic
sugar for the method [](argument), i.e. foo[name] is the same as foo.[](n=
ame).

So basically you have here an object that contains a hash as an instance
variable (named @val, not the clearest name ever chosen ), and allows =
you to
directly read the values in that hash using array notation, as if the obj=
ect was
a hash itself.
--
Christophe Grandsire.

http://rainbow.conlang.free.fr

It takes a straight mind to create a twisted conlang.


 
Reply With Quote
 
 
 
 
Kev Jackson
Guest
Posts: n/a
 
      10-24-2005
Christophe Grandsire wrote:

>Selon Kev Jackson <(E-Mail Removed)-vn.com>:
>
>
>
>>Hi all,
>>
>>What exactly does the following do and how can I test it? It looks like
>>its creating a new value with the key name, but how do you call it?
>>
>>def [](name)
>> @val[name]
>>end
>>
>>Any help appreciated
>>
>>
>>

>
>Just call it using array notation: foo[name]. Array notation is just syntactic
>sugar for the method [](argument), i.e. foo[name] is the same as foo.[](name).
>
>So basically you have here an object that contains a hash as an instance
>variable (named @val, not the clearest name ever chosen ), and allows you to
>directly read the values in that hash using array notation, as if the object was
>a hash itself.
>--
>Christophe Grandsire.
>
>

Thanks, so I had actually accessed it by accident anyway .

Kev



 
Reply With Quote
 
Dido Sevilla
Guest
Posts: n/a
 
      10-25-2005
On 10/24/05, Kev Jackson <(E-Mail Removed)-vn.com> wrote:
> Hi all,
>
> What exactly does the following do and how can I test it? It looks like
> its creating a new value with the key name, but how do you call it?
>
> def [](name)
> @val[name]
> end
>


It's actually Ruby's syntax for operator overloading. An instance of
a class that defines this can be "indexed" as though it were an array
or hash. Other operators are overriden in the same way, e.g.

def +(x)
...
end

Remember that everything in Ruby is an object, so when you see even
something as simple as 1 + 1 in Ruby, that's actually syntactic sugar
for 1.+(1), calling the + method for an instance of the Fixnum class.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quickie: definition of "context"? Roy ASP .Net 1 01-26-2006 05:51 PM
Quickie: Printing out code? Roy ASP .Net 2 10-19-2005 06:32 PM
Javascript Quickie Craig G ASP .Net 3 01-25-2005 12:14 PM
>> Quickie question about hyperlinks in XML Gene Ellis XML 0 08-25-2004 06:30 PM
just a quickie Annette Kurten Computer Support 3 01-18-2004 11:43 PM



Advertisments