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

F

falcon

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
 
M

mark sparshatt

falcon said:
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
 
J

James Britt

falcon said:
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
 
R

Robert Klemme

mark sparshatt said:
falcon said:
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:
=> <OpenStruct foo="bar">

Kind regards

robert
 
F

falcon

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.
 
H

Hal Fulton

itsme213 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,812
Messages
2,569,694
Members
45,478
Latest member
dontilydondon

Latest Threads

Top