Forum: Ruby Make a symbol represent 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.
chris.irish (Guest)
on 2005-12-02 21:11
(Received via mailing list)
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?
lukfugl (Guest)
on 2005-12-02 21:24
(Received via mailing list)
On 12/2/05, Chris I. <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.
malte__ (Guest)
on 2005-12-02 21:36
(Received via mailing list)
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
chris.irish (Guest)
on 2005-12-02 22:41
(Received via mailing list)
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?
James G. (Guest)
on 2005-12-02 22:53
(Received via mailing list)
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
lukfugl (Guest)
on 2005-12-02 22:53
(Received via mailing list)
On 12/2/05, Chris I. <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.
chris.irish (Guest)
on 2005-12-02 23:06
(Received via mailing list)
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 :)
dblack (Guest)
on 2005-12-02 23:55
(Received via mailing list)
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!
This topic is locked and can not be replied to.