New to programming and to Ruby, and I hope this question about symbols is in line. I understand that symbols in Ruby (e.g., :book
, :price
) are usef
attr_accessor
and such are all methods that belong to the class, Class. They expect symbols as arguments. You could write your own version of attr_
that used strings, if you wanted. Its just a ruby idiom. Here's an example of attr_acessor
that stores all the previous values of attr_accessor
I made for a homework assignment.
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s # make sure it's a string
attr_reader attr_name # create the attribute's getter
attr_reader attr_name+"_history" # create bar_history getter
class_eval %Q"
def #{attr_name}=(value)
if !defined? @#{attr_name}_history
@#{attr_name}_history = [nil]
end
@#{attr_name} = value
@#{attr_name}_history << value
end
"
end
end
(Answer to your comment)
dog = 'dog'
or String.new("dog")
After dog = String.new, the field class of instance dog points to class String.
class << dog
puts "inside #{self}" #=> inside #<Class:#<String:0x007fb38a83a820>>
def bark
puts 'woof'
end
end
dog.bark #=> "woof"
p dog.singleton_methods #=> ["bark"]
With class << dog
or def dog.bark
, Ruby creates an anonymous class, the field class of instance dog now points to this anonymous class, and from there to String. Methods defined in this context with def or define_method go into the methods table of the anonymous class.
Ruby 1.9.2 has introduced Object#singleton_class. [The Pickaxe] Returns the singleton class of obj, creating one if necessary. (I add) It is equivalent to class << self; self end
.
The Ruby Programming Language (O'Reiily) simply says : to open the eigenclass [singleton class] of the object o, use class << o.
So I don't know how to read it loud. I have read that some would prefer o >> class
. It's only recently that I have found how to figure out what this strange expression means. I pronounce : go from o to it's anonymous class.
class << MyClass
def dog
puts 'dog as class method'
end
end
MyClass.dog #=> dog as class method
The same is true for a class. With class MyClass
, MyClass, as instance of Class, is an object with a pointer to its class Class. With def MyClass.some_method
or class << MyClass
, Ruby creates an anonymous class which is inserted between MyClass and Class, and class methods go into it.
Maybe something like: "from class, instantiate singleton object self
Yes for "from class/object" to anonymous singleton class/eigenclass/metaclass.
But we don't instantiate self. Self (in Smaltalk, this in C++/Java) is kind of a reserved word which designate the receiver of the message. dog.bark
: in OO language we say that the message bark in sent to object dog. Inside the method bark
, self will be set to dog, so that we can reference dog. This is more obvious with
o1 = MyClass.new; o2 = MyClass.new
o1.some_method; o2.some_method
some_method must be able to reference the receiver in a generic way, is it o1 or o2, this is what self is for.
Symbols used in accessor methods are not variables. They are just representing the name of a variable. Variables hold some reference, so you cannot use a variable itself in defining accessor methods. For example, suppose you wanted to define an accessor method for the variable @foo
in a context where its value is "bar"
. What would happen if Ruby's syntax were to be like this:
attr_accessor @foo
This would be no different from writing:
attr_accessor "bar"
where you have no access to the name @foo
that you are interested in. Therefore, such constructions have to be designed to refer to variable names at a meta level. Symbol is used for this reason. They are not variables themselves. They represent the name of a variable.
And the variable relevant to accessor methods are instance variables.
Symbols are not variables, but a type of literal value, like numerals and quoted strings. Significantly, symbols are used to represent variables and other named values in the Ruby runtime. So when the Ruby interpreter sees the name foo
used as a variable or method name, what it looks up in the Hash of runtime values is the symbol :foo
, not the string "foo"
. This was, in fact, the original use of the term "symbol" in programming language terminology; variables, functions, constants, methods, and so on are said to be stored in the compiler or interpreter's "symbol table".
Pretty much any time you're passing around the name of something in Ruby, you're going to use a symbol. If you use method_missing
as a catch-all to implement arbitrary methods on your object class, a symbol is what it receives as an argument telling it the name of the method that was actually called. If you inspect an object with .methods
or .instance_variables
, what you get back is an array of symbols. And so on.
Cool, I guess you understand them by now. However why are they so important?
Symbols in Ruby are immutable whereas strings are mutable. You think ok cool, so what?
Let's assume you have an array of strings, like so:
[ "a", "b", "a", "b", "a", "b", "c" ]
For each new string you create ruby is going to create a string/object which holds the value of "a" and because strings are mutable things ruby assigns a different id to each of them. If you were to use symbols instead:
[ :a, :b, :a, :b, :a, :b, :c ]
Ruby now will point to those symbols and it will only create them once.
Let's do some benchmarking:
require 'benchmark'
Benchmark.bm do |x|
x.report("Symbols") do
a = :a
1000_000.times do
b = :a
end
end
x.report("Strings") do
a = "a"
1000_000.times do
b = "a"
end
end
end
ruby -w symbols.rb
Symbols 0.220000 0.000000 0.220000 ( 0.215795)
Strings 0.460000 0.000000 0.460000 ( 0.452653)
If you'd like to see all the symbols you have already created you could do:
Symbol.all_symbols
You can also send a message to them asking about their id:
:a.object_id #=> 123
:a.object_id #=> 123
"a".id #=> 23323232
"a".id #=> some_blob_number
Again that's because Strings in Ruby are mutable and Symbols are not. Ruby Symbols represent names inside the Ruby Interpreter.
This video really helped me: Ruby's Symbols Explained
I hope it helps you all.
To address your "If true that they are variables" and "scope" questions, it would have been simpler to answer that accessor symbols have nothing to do with instance variables, even if it sounds iconoclastic. They don't point to instance variables. Accessors only define getter and setter methods. Under Object#instance_variables, the Pickaxe(*) says : Note that simply defining an accessor does not create the corresponding instance variable.
In Ruby, a variable does not exist until you assign a value to it. The following code demonstrates this.
class MyClass
attr_accessor :name
attr_reader :book
end
obj = MyClass.new # line 6
print '1) obj.instance_variables : '; p obj.instance_variables
print '2) obj.name : '; p obj.name
obj.name = 'xyz'
print '3) obj.instance_variables : '; p obj.instance_variables
print '4) obj.name : '; p obj.name
print '5) obj.book : '; p obj.book
class MyClass
def initialize(p_book)
@book = p_book
end
end
obj = MyClass.new('The Pickaxe') # line 21
print '6) [new] obj.book : '; p obj.book
class MyClass
method_name = 'title'
attr_accessor method_name # line 26
end
obj.title = 'Programming Ruby'
print '7) obj.instance_variables : '; p obj.instance_variables
print '8) obj.title : '; p obj.title
Output :
$ ruby -w t.rb
1) obj.instance_variables : []
2) obj.name : nil
3) obj.instance_variables : ["@name"]
4) obj.name : "xyz"
5) obj.book : nil
6) [new] obj.book : "The Pickaxe"
7) obj.instance_variables : ["@title", "@book"]
8) obj.title : "Programming Ruby"
1) empty array : accessors have not defined instance variables
2) asking for instance variable @name answers nil : it does not exist
3) assigning a value has created the instance variable.
Note that name =
is a syntactic sugar for using the setter as an ordinary method with a parameter : obj.name=('xyz')
4) the getter method name
answers the value of @name
5) the getter method book
answers nil because the instance variable @book does not exist. Defining an accessor attr_reader :book
has not defined the corresponding instance variable
6) the getter method book
answers the value assigned in initialize
, called by new
on line 21. The instance variable @book has been created by @book = p_book
line 26) I have always believed that accessors accept only symbols. I discover that a variable is possible, but of limited interest.
7) the setter method title=
has created @title. This also shows that instance variables belong to a single object. We often believe that they belong to all instances of the class, as in other languages. In this case, @name belongs only to the object created on line 6.
8) the getter method title
answers the value of @title
class MyClass
def title # line 34
@book + ' (cheating !)'
end
end
print '9) obj.title : '; p obj.title
Output :
t.rb:34: warning: method redefined; discarding old title
9) obj.title : "The Pickaxe (cheating !)"
9) of course there is a tight correlation between an accessor symbol and the corresponding instance variable, because, behind the scene, Ruby creates methods which reference an instance variable of the same name. You could define your own getter and cheat.
Note that besides class variables (@@var, some dislike them as ugly as global variables), classes can also have instance variables. I call them class instance variables :).
class MyClass
: Ruby allocates a new instance of class Class, defines a constant MyClass, and assigns the new instance to that constant. Thus MyClass is an ordinary object (instance of Class) and as such can have instance variables.
if RUBY_VERSION[0..2] == '1.8'
class Object
def singleton_class
class << self
self
end
end
end
end
class MyClass
singleton_class.instance_eval do
attr_accessor :counter
end
@counter = 0
def initialize(p_book)
@book = p_book
self.class.counter += 1
end
end
print '10) MyClass.singleton_methods : '; p MyClass.singleton_methods
print '11) MyClass.instance_variables : '; p MyClass.instance_variables
obj = MyClass.new('Ruby')
print '12) [new] obj.book ', MyClass.counter, ': '; p obj.book
obj = MyClass.new('Metaprogramming')
print '13) [new] obj.book ', MyClass.counter, ': '; p obj.book
Output :
t.rb:55: warning: method redefined; discarding old initialize
10) MyClass.singleton_methods : ["counter", "counter="]
11) MyClass.instance_variables : ["@counter"]
12) [new] obj.book 1: "Ruby"
13) [new] obj.book 2: "Metaprogramming"
More on singleton methods here : What does def `self.function` name mean?
(*) http://pragprog.com/book/ruby3/programming-ruby-1-9