Personclass is a first-class citizen in the language, just like
Personto compute its full name:
returnkeyword to signify what value I wanted to replace
hs.full_namein the original expression after it's been evaluated.
selfkeyword to refer to the object who was asked to calculate its full name, since I can't know in advance what (if any) variable name will be used.
Personthat we create will have the ability to compute their age based on their own dob attribute:
Hashto model everything, it's much more powerful to create classes for each kind of thing, and then empower them with behavior (methods) in addition to information.
Students can do everything people can, and a little bit more.
person4.role? How about
person1.grade? Why? What would the error message be?
app/modelsfolder. For example, if you create a file in that folder called
person.rbwith the following:
Personclass from anywhere in the app: from within any controller, any view template, in the
rails console-- or even from within another model.
Personclass above and then
full_namein the class definition, I make it much easier to re-use elsewhere and share.
routes.rb, we tell Rails to listen for requests for "/rock", and when it hears one, run the
play_rockmethod on an instance of
GamesController. Behind the scenes, Rails would do something like
ApplicationController, which is a pre-written class we get from Rails.
render(), which knows how to parse a
.html.erbembedded Ruby view template and process it into pure HTML suitable for a browser to display.