Usage of symbols and table of symbols

  1. In many of the Rails methods, symbols are being passed as arguments. I'm not sure how some of those arguments are references to other methods. In other words, the symbol points to another method. I understand how a symbol like :something will always point to the same memory allocation, which is different from multiple instances of, say, "something". If I have a symbol :x and also defined a method x(), does it mean that these two are linked somehow?

  2. A Ruby book describes the concept of a table of symbols:

    A symbol is an identifier whose value is itself. This describes, in a broad sense, they way that symbols behave from the point of view of the Ruby programmer. But it doesn't tell you what symbols are literally from the point of view of the Ruby interpreter. A symbol is, in fact, a pointer into the symbol table. The symbol table is Ruby's internal list of known identifiers—such as variable and method names.

    I stumbled upon how it refers to different methods or strings. Can someone help me understand this table of symbols and how it relates to symbols being used as method names?

Answers


Symbols by themselves are not connected with methods in any way. Certain methods can accept symbols to dynamically call other methods, using symbols as their names. Observe:

class Foo
  def bar
    "called bar"
  end

  def baz
    "called bazzz"
  end

  def invoke m
    send m
  end
end

f = Foo.new
f.invoke :bar # => "called bar"
f.invoke :baz # => "called bazzz"

# since `invoke` just delegates execution to `send`, and `send` can accept strings as well
#   we can pass a string here. 
f.invoke 'baz' # => "called bazzz"

This is the same case as in

before_filter :init_session
validates_presence_of :first_name

And many others

Edit:

if I have a symbol :x and also defined a method x(), does it mean that these two are linked somehow?

No, they are not linked in any way. You can safely ignore that passage about symbol table. It's unnecessary implementation detail for you at the moment.


Symbol names and method names are not linked in any way. A symbol is most closely related to a string. The difference between a string and a symbol is that a symbol is a unique object for the entire Ruby interpreter whereas strings are always unique objects. To illustrate this, open up IRB:

irb
1.9.3-p362 :001 > "hello".object_id
 => 70162149682220 
1.9.3-p362 :002 > "hello".object_id
 => 70162149699280 
1.9.3-p362 :003 > :hello.object_id
 => 460328 
1.9.3-p362 :004 > :hello.object_id
 => 460328 
1.9.3-p362 :005 > "hello".to_sym.object_id
 => 460328 

As you can see, two identical strings containing "hello" have different object ids, even though they contain the same contents. Symbols on the other hand, always have the same object id as long as the contents of the symbol are the same. You can see that converting the string "hello" to a symbol gives the same object id as the symbol :hello. Symbols are much more memory efficient to use, and equality comparison is much faster. The trade off is that a symbol is essentially a memory leak, because you can never remove one from memory once you define it.

As Sergio said, you can invoke methods using the send method, but send uses Ruby's robust reflection to execute methods by name at runtime, it has nothing inherently to do with symbols other than the method names are identified by a symbol.

EDIT: If you are curious about how this works, check out Ruby Metaprogramming to see the things you are able to do. To understand exactly how this works, however, you would need to look at the C source code of the Ruby language.


If you define symbol :x and also defined a method x() and print :x.class => Symbol , x.class => "Datatype of return field or if nothing is return then NilClass".

This Shows that there is no relation between symbol :x and method x().

Things done with Symbol can also be done with String in Ruby.

Symbols are used instead of string if u want one instance of string means you want one memory location for particular string ,obviously for performance benefit.

Internally Ruby saves Methods name as Symbol.

So its better to use Symbol when passing Methods names around like in case of attr_accessor and send but its not compulsory and you can use String otherwise.


Need Your Help

Ruby Mechanize Outlook Web Access

ruby mechanize

I am trying to use ruby mechanize to access specific emails from my outlook web access account.

Using TinyButStrong, how to get results to load into option tags?

php mysql tinybutstrong

Environment: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.