[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

object.["somepropertyOrmethod"] == object.somepropertyOrmethod ?

falcon

4/18/2005 4:18:00 PM

Does Ruby allow javascript like invocation of methods (or access to
properties) like this:
someProperty = "nameOfStudent"
Object[someProperty]

rather than just Object.nameOfStudent?

I need something like this because how an object is manipulated depends
on runtime...basically it will be an experimental database system so
users will be able to manipulate data in any way they desire.
Iterators and lambda like functions make this very easy already.

Also, is there a limit to how deep one can do recursion? Recursion can
easily cause stack overflow error in Java, but some languages don't
have that problem (I guess it is called tail call optimization).

Thanks!
Falcon

7 Answers

mark sparshatt

4/18/2005 4:42:00 PM

0

falcon wrote:
> Does Ruby allow javascript like invocation of methods (or access to
> properties) like this:
> someProperty = "nameOfStudent"
> Object[someProperty]
>
> rather than just Object.nameOfStudent?

The way to do this would be to use the send method

Object.send(someProperty)

Or you could define a [] method for your class

def [](propertyname)
self.send(propertyname)
end

then you'd be able to use

Object[someProperty]

>
> I need something like this because how an object is manipulated depends
> on runtime...basically it will be an experimental database system so
> users will be able to manipulate data in any way they desire.
> Iterators and lambda like functions make this very easy already.
>
> Also, is there a limit to how deep one can do recursion? Recursion can
> easily cause stack overflow error in Java, but some languages don't
> have that problem (I guess it is called tail call optimization).
>

Ruby doesn't currently use tail call implementation so there is a limit
to recursion.

IFAIK this'll be changed in Rite.


HTH

--
Mark Sparshatt



wannes

4/18/2005 4:42:00 PM

0

falcon wrote:
> Does Ruby allow javascript like invocation of methods (or access to
> properties) like this:
> someProperty = "nameOfStudent"
> Object[someProperty]

If i understand your question correctly, you could do something like
Object.send(someProperty, args)

> Also, is there a limit to how deep one can do recursion? Recursion can
> easily cause stack overflow error in Java, but some languages don't
> have that problem (I guess it is called tail call optimization).

the short answer: it depends ;)
There has been a thread some time ago about the stack-size
http://rubyurl... [1]

grtz,
wannes

[1]
<http://www.ruby-talk.org/cgi-bin/vframe.rb/ruby/ruby-talk/133981?133902-...

james_b

4/18/2005 4:52:00 PM

0

falcon wrote:
> Does Ruby allow javascript like invocation of methods (or access to
> properties) like this:
> someProperty = "nameOfStudent"
> Object[someProperty]
>
> rather than just Object.nameOfStudent?

module ECMAtronic
def []( m_name )
send( m_name )
end

def []=( m_name , val)
send( m_name + "=", val )
end
end

class Foo
include ECMAtronic
attr_accessor :foo, :bar

def initialize( foo = nil, bar = nil )
@foo = foo
@bar = bar
end

def some_method
"You have foo = '#@foo' and bar = '#@bar'"
end

end


f = Foo.new( "foo", "bar")

f[ "foo" ] = "This is foo"
puts f[ "foo" ]
puts f[ "some_method" ]



James


Robert Klemme

4/18/2005 5:05:00 PM

0


"mark sparshatt" <msparshatt@yahoo.co.uk> schrieb im Newsbeitrag
news:4263E07F.2010503@yahoo.co.uk...
> falcon wrote:
>> Does Ruby allow javascript like invocation of methods (or access to
>> properties) like this:
>> someProperty = "nameOfStudent"
>> Object[someProperty]
>>
>> rather than just Object.nameOfStudent?
>
> The way to do this would be to use the send method
>
> Object.send(someProperty)
>
> Or you could define a [] method for your class
>
> def [](propertyname)
> self.send(propertyname)
> end
>
> then you'd be able to use
>
> Object[someProperty]

You can as well use a Hash instead. You might also want to look at
OpenStruct, which might help you, too. An OpenStruct happily accepts every
getter or setter when invoked:

>> require 'ostruct'
=> true
>> o = OpenStruct.new
=> <OpenStruct>
>> o.foo
=> nil
>> o.foo = "bar"
=> "bar"
>> o.foo
=> "bar"
>> o
=> <OpenStruct foo="bar">

Kind regards

robert

falcon

4/18/2005 6:02:00 PM

0

Thanks all for the replies. I checked to see if my message was posted
on the newsgroup yet...I didn't expect 4 or 5 responses already!

I read somewhere that YARV was supposed to be released in April...but I
haven't seen any announcements related to it. Also, Isn't ruby 2.0
scheduled to be out? Any idea when? Thanks again.

Its Me

4/18/2005 6:40:00 PM

0

"falcon" <shahbazc@gmail.com> wrote in message
news:1113841085.371559.282020@g14g2000cwa.googlegroups.com...
> Does Ruby allow javascript like invocation of methods (or access to
> properties) like this:
> someProperty = "nameOfStudent"
> Object[someProperty]
>
> rather than just Object.nameOfStudent?

Hal's SuperStruct will fit the bill.



Hal E. Fulton

4/19/2005 2:19:00 AM

0

itsme213 wrote:
> "falcon" <shahbazc@gmail.com> wrote in message
> news:1113841085.371559.282020@g14g2000cwa.googlegroups.com...
>
>>Does Ruby allow javascript like invocation of methods (or access to
>>properties) like this:
>>someProperty = "nameOfStudent"
>>Object[someProperty]
>>
>>rather than just Object.nameOfStudent?
>
>
> Hal's SuperStruct will fit the bill.
>

But if you try it, be aware it has bugs. And if you see
them, please report.

Hal