Classes and objects
Define a class
A class is a blueprint for objects. It groups data and behavior in one named place, so related methods live next to the values they use.
Shared behavior can also come from outside the class, as shown in
Modules and mixins.
class User def hello puts "Hello" end end user = User.new user.hello
ruby first_class.rb
Hello
Initialize objects
When you call new, Ruby builds a fresh object and runs a special method named initialize. It receives the arguments you pass to new and stores them in instance variables that start with @.
Each object keeps its own copy of these variables, so two users never share a name.
class User def initialize(name) @name = name end def hello puts "Hello, " + @name end end user = User.new("Ada") user.hello
ruby initialize_demo.rb
Hello, Ada
attr_reader and accessor
Instance variables hide inside the object. To read them from outside, declare attr_reader with the variable name without @.
Use attr_accessor when outside code must also change the value. Prefer the reader by default and open writing only where needed.
class User attr_reader :name attr_accessor :email def initialize(name) @name = name end end user = User.new("Ada") puts user.name
ruby attr_demo.rb
Ada
What self is
Inside a method, self is the current object, the one that received the call. You can call other methods on it, or ask it for its class.
At the top level of a program self is a plain object called main, which is why small scripts can call methods without a receiver.
class Counter def show_self puts self.class end end Counter.new.show_self
ruby self_demo.rb
Counter
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What runs when
User.new("Ada")is called?Answer
initialize, with "Ada" as its argument
new builds the object and calls initialize with the arguments it was given.
-
What does this program print?
class User def hello puts "Hello" end end User.new.helloAnswer
Hello
new works without an initialize method, and hello prints its line.
-
What does this program print?
class User def initialize(name) @name = name end def hello puts "Hello, " + @name end end User.new("Ada").helloAnswer
Hello, Ada
initialize stores the argument in the instance variable, which hello reads.
-
What does
attr_reader :nameadd to a class?Answer
A reader method called name that returns @name
The reader exposes the instance variable without opening writing.
-
What does this program print?
class User attr_accessor :email end user = User.new user.email = "a@b" p user.emailAnswer
"a@b"
attr_accessor gives both the reader and the writer, and the writer stores the value.
-
What does this program print?
class User attr_reader :name def initialize(name) @name = name end end p User.new("Ada").nameAnswer
"Ada"
p shows the string with its quotes, unlike puts.
-
What is
selfat the top level of a Ruby program?Answer
An object called main
That plain object is why a small script can call methods without a receiver.
-
What does this program print?
class Counter def me self end end p Counter.new.me.classAnswer
Counter
self in an instance method is the object that received the call, and its class is Counter.
-
What does this program print?
class User attr_reader :name def initialize(name) name = name end end p User.new("Ada").nameAnswer
nil
The local variable name is assigned to itself; @name was never set, so the reader answers nil.
-
What does
attr_reader :@nameraise?Answer
NameError: invalid attribute name '@name'
The declaration takes the bare attribute name, without the @.
Article author: Arthur Isaev