[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

object literal like JavaScript in Ruby

Peter Michaux

6/6/2006 4:46:00 AM

Hi,

I've been programming JavaScript a lot lately and would like to find a
way to create and object without defining a class like the JavaScript
object literal. Is there a quick an dirty object in Ruby?

In JavaScript

var a = {foo: 34, bar:89}
a.foo
a.bar


Thanks,
Peter

2 Answers

Park Heesob

6/6/2006 6:24:00 AM

0

Hi,
<petermichaux@gmail.com> wrote in message
news:1149569152.804306.262190@j55g2000cwa.googlegroups.com...
> Hi,
>
> I've been programming JavaScript a lot lately and would like to find a
> way to create and object without defining a class like the JavaScript
> object literal. Is there a quick an dirty object in Ruby?
>
> In JavaScript
>
> var a = {foo: 34, bar:89}
> a.foo
> a.bar
>
>

a = Struct.new(:foo,:bar).new(34,89)
a.foo
a.bar

> Thanks,
> Peter
>
Regards,
Park Heesob


Robert Klemme

6/7/2006 5:47:00 PM

0

Park Heesob <phasis68@kornet.net> wrote:
> Hi,
> <petermichaux@gmail.com> wrote in message
> news:1149569152.804306.262190@j55g2000cwa.googlegroups.com...
>> Hi,
>>
>> I've been programming JavaScript a lot lately and would like to find
>> a way to create and object without defining a class like the
>> JavaScript object literal. Is there a quick an dirty object in Ruby?
>>
>> In JavaScript
>>
>> var a = {foo: 34, bar:89}
>> a.foo
>> a.bar
>>
>>
>
> a = Struct.new(:foo,:bar).new(34,89)
> a.foo
> a.bar

There is also OpenStruct

>> require 'ostruct'
=> true
>> o=OpenStruct.new(:foo=>10,:bar=>30)
=> #<OpenStruct bar=30, foo=10>
>> o.foo
=> 10
>> o.bar
=> 30

Kind regards

robert