Forum: Ruby Adding properties to a method?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7b233adda3fc73f42e358587a0cf9d83?d=identicon&s=25 jonT (Guest)
on 2006-02-05 19:09
(Received via mailing list)
Hi,

I'd like to be able to do the following:

class Method; attr_accessor :foo; end
def bar; "some_res"; end
method(:bar).foo=1

method(:bar).foo #= 1

Of coures what i'd actually get is:
method(:bar).foo #= nil

because method(:bar) returns a new object each time.

Is it possible to bind properties to a method definition? If so, how?

Thanks,
Jon
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-02-05 19:30
(Received via mailing list)
jonT wrote:
> Of coures what i'd actually get is:
> method(:bar).foo #= nil
>
> because method(:bar) returns a new object each time.

A String, in your example.  Do you want the string  to respond to the
message 'foo=' ?


>
> Is it possible to bind properties to a method definition? If so, how?
>

What do you mean by "bind" and "properties"?


--
James Britt

"Programs must be written for people to read, and only incidentally
  for machines to execute."
   - H. Abelson and G. Sussman
   (in "The Structure and Interpretation of Computer Programs)
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2006-02-05 19:45
(Received via mailing list)
You can check out Facets' kernel/method.

T.
7b233adda3fc73f42e358587a0cf9d83?d=identicon&s=25 jonT (Guest)
on 2006-02-05 19:48
(Received via mailing list)
>> Of coures what i'd actually get is:
>> method(:bar).foo #= nil
>> because method(:bar) returns a new object each time.
>A String, in your example.  Do you want the string to respond to the
>message 'foo=' ?

method(:bar)
# => <Method: Object#bar>
method(:bar).class
# => Method

method(:bar) actually returns a method object.

>What do you mean by "bind" and "properties"?
Sorry. By bind I meant simply to set the value of a properety in the
usual way. By property I mean property: a public field.

Essentially what I'm aiming to do is to have some way of storing data
about a method (which is either private to Object or a member of some
class or module) such that it is publicly available. Example:

def cd(dir)
	FileUtils.cd(dir.path)
	nil
end
method(:cd).args_classes={0=>JDir}

not terribly Ruby like but you get the picture.

Jon
7b233adda3fc73f42e358587a0cf9d83?d=identicon&s=25 jonT (Guest)
on 2006-02-05 20:34
(Received via mailing list)
Thanks, method! from
<http://facets.rubyforge.org/rdoc-nano/classes/Obje...
works great.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-02-06 20:34
(Received via mailing list)
On Feb 5, 2006, at 10:08 AM, jonT wrote:

> class Method; attr_accessor :foo; end
> def bar; "some_res"; end
> method(:bar).foo=1
>
> method(:bar).foo #= 1
>
> Of coures what i'd actually get is:
> method(:bar).foo #= nil
>
> because method(:bar) returns a new object each time.

Of course!  It may not be the same method.  Another thread may
overwrite bar with a new implementation.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.