rails

H

Haris Bogdanoviæ

Could someone explain to me:

class Movie << ActiveRecord::Base
has_many :Actors
end

How can I call a method within class definition ? This is not Ruby anymore.
And this : semicolon stands for symbol. Never heard of symbols in Ruby.
 
J

Jesús Gabriel y Galán

Could someone explain to me:

class Movie << ActiveRecord::Base
=C2=A0 =C2=A0has_many :Actors
end

How can I call a method within class definition ? This is not Ruby anymor=
e.

In fact it is. A method call without an explicit receiver goes to
whatever object happens to be "self". Inside a class definition, self
is the class object, in this case Movie (which is an instance of class
Class). Remember than in Ruby classes are objects too. Check this, for
example:

irb(main):001:0> class Base
irb(main):002:1> def self.has_many(thing)
irb(main):003:2> puts "I have many #{thing}"
irb(main):004:2> end
irb(main):005:1> end
=3D> nil
irb(main):006:0> class Movie < Base
irb(main):007:1> has_many :Actors
irb(main):008:1> end
I have many Actors
=3D> nil
And this : semicolon stands for symbol. Never heard of symbols in Ruby.

http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.re=
d

Check here for example for some explanations of symbols, although with
1.9 I believe they have become more string-like than in 1.8

Hope this helps,


Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top