2

Is there any way to assign an attribute to a method? I know you can do it with classes, but I have a series of similar methods and it would really be helpful if I could assign each one an attribute. Does anyone know how to do it or if it is even possible in Ruby?

Edit: What I basically mean is that when you have a class you can assign attributes to it like this:

 class Person
  attr_accessor :age
  ...
 end

I'm wondering if it's also possible to do that with methods, like this:

class Trucks
 def get_fire_truck_name
   attr_accessor :size
   ...
 end
end

(Btw this is just an example. The actual program is much more complex which explains why I can't simply turn my methods into classes in order to give them attributes.)

Walker
  • 4,879
  • 3
  • 20
  • 16
  • You should provide examples to describe your problem, otherwise your question is off. – megas Jul 15 '12 at 19:10
  • 3
    like you can do in Python or Javascript? Ruby's methods are not objects, so the answer is no. – tokland Jul 15 '12 at 19:21
  • 2
    @tokland Ruby methods are [definitely objects](http://www.ruby-doc.org/core-1.9.3/Method.html). – Brandan Jul 15 '12 at 19:49
  • Yeah, I guess I won't be able to do it the way I wanted it then, but I think I've figured way to work around the problem. Thanks for the help anyway guys! – Walker Jul 15 '12 at 19:58
  • 3
    Brandan: The fact that there is a class named `Method` says nothing about methods themselves being objects. Can you send a method as an argument as you do with strings or blocks? no, you have to convert them to a `Method` object first: `obj.method(:name)`. Methods are not first-class in Ruby, blocks are. http://stackoverflow.com/a/2602485/188031 – tokland Jul 15 '12 at 20:42
  • In my opinion, there's not really such a thing as an "attribute" in Ruby. It's not a language construct, just a social convention. http://stackoverflow.com/questions/1194585/what-is-the-difference-between-methods-and-attributes-in-ruby – Andrew Grimm Jul 15 '12 at 23:33
  • The question seems like a snark hunt. What is the **underlying problem** that you're trying to solve with the whole "method attributes" approach? – Todd A. Jacobs Jul 16 '12 at 00:59

2 Answers2

2

Since methods are objects in Ruby, you can define methods on a Method object just as you would any other object. If you specifically want to use attr_accessor, you can open the method's eigenclass like this:

class Person
  attr_accessor :age
end

m = Person.instance_method(:age)
class << m
  attr_accessor :units
end

m.units = 'years'
puts m.units       #=> 'years'

You could also explicitly define the methods on the object using the def keyword:

def m.units
  @units || 'years'
end

def m.units=(u)
  @units = u
end

puts m.units
m.units = 'decades'
puts m.units
Brandan
  • 14,735
  • 3
  • 56
  • 71
0

if you're just talking about being able to call methods as variables on an object there are at least three paths that I know of...

using an attr_reader accessor Accessors

attr_reader :your_cool_method_name

You can call send on the object. Note that this way ignores scope (public, protected, private)

@object.send :your_cool_mehod_name

Or you could do it through a method_missing call but this is a bit advanced and based on your question I would hold off on this approach until your familiarity with Ruby increases.

Community
  • 1
  • 1
engineerDave
  • 3,887
  • 26
  • 28
  • As an aside I'd highly recommend checking out the book metaprogramming ruby as the first part provides a great general overview of Ruby. http://pragprog.com/book/ppmetr/metaprogramming-ruby – engineerDave Jul 15 '12 at 19:27