# Make a symbol represent a method?

**URL:** <https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337>\
**Category:** Ruby\
**Created:** [December 2, 2005, 8:11pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337 "2005-12-02T20:11:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 8:11pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/1 "2005-12-02T20:11:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 8:36pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/2 "2005-12-02T20:36:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 9:53pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/3 "2005-12-02T21:53:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 9:53pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/4 "2005-12-02T21:53:56Z")

</div>

On 12/2/05, Chris I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 10:55pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/5 "2005-12-02T22:55:22Z")

</div>

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” 🙂 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  
removed\_email\_address@domain.invalid

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

---

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 10:06pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/6 "2005-12-02T22:06:04Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 8:24pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/7 "2005-12-02T20:24:08Z")

</div>

On 12/2/05, Chris I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![chris.irish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4bbf92/32.png) [@chris.irish](https://www.ruby-forum.com/u/chris.irish)\
**Post date:** [December 2, 2005, 9:41pm UTC](https://www.ruby-forum.com/t/make-a-symbol-represent-a-method/49337/8 "2005-12-02T21:41:48Z")

</div>

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?
