Make a symbol represent a method?

I just read a FAQ page online about Ruby symbols and someone mentions
this

Also, the entirely realistic reasoning for using symbols: If you
are going to refer to a method name, use a symbol. Because by
defining the method, the symbol exists anyway.

Can someone show me an example of this? Is this referring to the method
of a class?

Chris I.:

Also, the entirely realistic reasoning for using symbols: If you are going
to refer to a method name, use a symbol. Because by defining the method,
the symbol exists anyway.

Can someone show me an example of this? Is this referring to the method
of a class?

irb> def my_cool_method; end
=> nil
irb> Symbol.all_symbols.collect { |s| s.to_s }.grep /cool/
=> [“my_cool_method”]

Malte

On Dec 2, 2005, at 2:38 PM, Chris I. wrote:

One more thing, Can I use the symbol to call a method and pass
arguments to it? Or is it just a reference to the methods name so
that’s not possible?

You’re misunderstanding what a Symbol is.

A Symbol is is just an immutable String for Ruby. A simple data type.

One use for Symbols is to reference a method by name:

send(:method_name)

You could also use a String for this though:

send(“method_name”)

And you may want to use Symbols for something completely different.
I generally use them for keys to my internal Hashes:

@parsers = { :leading_fields => Parser.new,
:row => Parser.new,
:line_endings => Parser.new }

To answer your question, a little indirectly, yes, send() allows you
to pass arguments to the function:

send(:method_name, arg_1, arg_2, …)

Hope that helps.

James Edward G. II

On 12/2/05, Chris I. [email protected] wrote:

K… I get it
One more thing, Can I use the symbol to call a method and pass arguments
to it? Or is it just a reference to the methods name so that’s not
possible?

Sure thing. The symbol is not a reference to the method at all. What
the symbol is is a key into the objects method table, the value
corresponding to that key is the reference to the Method object. I can
use a symbol (it’ll “auto-vivify”, for lack of a better word) even
without a corresponding method.

As far as syntax for calling a method just using it’s named symbol,
there are two ways you can do this. The first is to use send:

class Greeter
def hello( name )
puts “Hello, #{name}!”
end
end

greeter = Greeter.new
greeter.send( :hello, “World” )

prints “Hello, World!”

An alternate technique is to grab an actual reference to the method
itself. This is available using the ‘method’ method:

greeter = Greeter.new
meth = greeter.method( :hello )
meth.call( “World” )

same output

This latter approach allows you to perform the name lookup only once,
even if you need to call the method several times. It’s also useful
when metaprogramming; you can grab a reference to an existing method
before redefining it, then call the original method from inside the
redefined method. Nifty!

Jacob F.

Hi –

On Sat, 3 Dec 2005, Jacob F. wrote:

without a corresponding method.
True… though it’s a bit like saying, “It’s possible to create a
string even if you don’t use it as a hash key” :slight_smile: I’d put it more
simply: a symbol is a built-in data type which can be, and is, used
for many purposes both internally and in user-space. From the user
side, I don’t think there’s any special relation between the symbol :x
and a method called x. The use of one, in itself, never has any
implications for the other.

As far as syntax for calling a method just using it’s named symbol,
there are two ways you can do this. The first is to use send:
[…]
An alternate technique is to grab an actual reference to the method
itself. This is available using the ‘method’ method:

Neither of these actually requires a Symbol object, though (as opposed
to a String).

David


David A. Black
[email protected]

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!

Jacob F. wrote:

Sure thing. The symbol is not a reference to the method at all. What
puts “Hello, #{name}!”
greeter = Greeter.new
Jacob F.

Thank’s everyone :slight_smile:

On 12/2/05, Chris I. [email protected] wrote:

I just read a FAQ page online about Ruby symbols and someone mentions this

Also, the entirely realistic reasoning for using symbols: If you
are going to refer to a method name, use a symbol. Because by
defining the method, the symbol exists anyway.

Can someone show me an example of this? Is this referring to the method
of a class?

It simply comes down to this. When you define a method the method
definition is saved away and connected to a lookup table for the
object you defined the message on. The key into that lookup table? A
symbol representing the method’s name. So if I define:

def hello
puts “Hello, world!”
end

The body is complied into a Method object, and that Method object is
then attached to the current self (Kernel, is you’re not in anything
else) with the symbol :hello as the key. After defining the above, try
this:

send(:hello)

Anytime you make a method call, it’s essentially just an invocation of
send with the method name and arguments as paramenters. (Well, it’s a
little more complicated, but that’s why I said “essentially”.)

Anyways, the point of the quote from the FAQ is that just be defining
the method hello, the symbol :hello already exists, since it’s used to
index the receiver’s method table. So, if you’re going to be passing
the method name around (common with metaprogramming), you save by
using the already existing :hello symbol, instead of creating a new
“hello” String object.

I hope that makes sense…

Jacob F.

Jacob F. wrote:

of a class?
end
little more complicated, but that’s why I said “essentially”.)
Jacob F.

K… I get it
One more thing, Can I use the symbol to call a method and pass arguments
to it? Or is it just a reference to the methods name so that’s not
possible?