K
Kris
Yet Another Ruby Nuby (YARN) question.
I see Rails makes extensive use of class-declarations.
For example:
class LineItem < ActiveRecord::Base
belongs_to roduct
......
belongs_to is a class-declaration in the above code. Coming from a C
family of languages I could not grasp this initially. When does this
code get executed - does it happen when the class is loaded for the
first time or every time a new instance of the class is created?
In this case I think the class-declaration code(belongs_to) looks at
the corresponding database table structure and injects attributes and
methods into the LineItem class. Am I right?
I created the following example
class B
def hello
print "Hello"
end
end
class D < B
hello
def bye
print "bye"
end
end
I tried this in irb and got an error. Why doesn't this work? In Rails I
think mixins are used to provide this kind of functionality.
To me these seem to be idioms of the language. Is there a site where
you could find gems like these?
Thanks.
I see Rails makes extensive use of class-declarations.
For example:
class LineItem < ActiveRecord::Base
belongs_to roduct
......
belongs_to is a class-declaration in the above code. Coming from a C
family of languages I could not grasp this initially. When does this
code get executed - does it happen when the class is loaded for the
first time or every time a new instance of the class is created?
In this case I think the class-declaration code(belongs_to) looks at
the corresponding database table structure and injects attributes and
methods into the LineItem class. Am I right?
I created the following example
class B
def hello
print "Hello"
end
end
class D < B
hello
def bye
print "bye"
end
end
I tried this in irb and got an error. Why doesn't this work? In Rails I
think mixins are used to provide this kind of functionality.
To me these seem to be idioms of the language. Is there a site where
you could find gems like these?
Thanks.