Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

object literal like JavaScript in Ruby

280 views
Skip to first unread message

peterm...@gmail.com

unread,
Jun 6, 2006, 12:45:52 AM6/6/06
to
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

Park Heesob

unread,
Jun 6, 2006, 2:23:34 AM6/6/06
to
Hi,
<peterm...@gmail.com> wrote in message
news:1149569152.8...@j55g2000cwa.googlegroups.com...

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

> Thanks,
> Peter
>
Regards,
Park Heesob


Robert Klemme

unread,
Jun 7, 2006, 1:47:29 PM6/7/06
to

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

0 new messages