C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Initialize: In this method, assign 2 fields (@width and @height) to the arguments. Fields use a leading @ character.
New: When we call new, the initialize method is run. The arguments are stored in the memory of the class fields.
Info: We call the constructor by using the class name (Box) and the new method. Box.new returns a new instance of the Box class.
Ruby program that uses class
class Box
# The constructor.
def initialize(width, height)
# Assign fields from arguments.
@width = width
@height = height
end
# A custom method.
def display()
puts @width
puts @height
end
end
# Create a new Box.
x = Box.new(10, 5)
# Call the display method.
puts x.display()
Output
10
5
And: If the internals change, we can modify just get and set. When fewer code changes are made, fewer things are likely to break.
Getter: A custom syntax exists for getters and setters. In a getter, we have no arguments and a single statement—the return value.
Setter: In a setter, we use the equals sign at the end of the name. Then in the body, we assign the targeted field.
Ruby program that uses getter, setter
class Box
# Getter.
def size
@size
end
# Setter.
def size=(number)
@size = number
end
end
# Create a new Box.
x = Box.new()
# Use setter.
x.size = 10
# Use getter and display.
puts x.size
Output
10
Here: We specify the to_s method on the Box class. The @width and @height fields are inserted into the string.
Info: When a class is passed to a method like puts, the to_s method is automatically invoked. This all happens implicitly.
Tip: We have no need to call to_s in many program contexts. But to_s can be directly called.
Ruby program that defines to_s
class Box
def initialize(width, height)
# Initialize the class.
@width = width
@height = height
end
def to_s
# Return this string.
"Box, width: #@width, height: #@height"
end
end
# Create new Box.
b = Box.new(10, 20)
# Display it (calls to_s).
puts b
Output
Box, width: 10, height: 20
Super: With a call to super() we invoke a same-named method from the class' parent. So from Square's super() we invoke Shape display().
Ruby program that uses inheritance, super
class Shape
def display()
puts("Shape display")
end
end
# This class inherits from Shape.
class Square < Shape
def display()
puts("Square display")
# Call superclass method of the same name.
super()
end
end
# Create the class.
item = Square.new()
item.display()
Output
Square display
Shape display
Here: We call test() and pass it the self instance. The self variable evaluates to an object.
This: The self instance is the same thing as a "this" instance in other languages like C# or Java.
Ruby program that uses self
def test(s)
# See if the Shape's color is red.
puts(s.color == "red")
end
class Shape
def initialize(color)
@color = color
end
def color
@color
end
def analyze()
# Pass this object to another method.
test(self)
end
end
# Create Shape and call the analyze method.
s = Shape.new("red")
s.analyze()
Output
true
Protected: This is a hybrid of public and private—it means public only to subclasses, private to everything else.
Ruby program that uses public, private
class Navigator
def initialize(location)
@location = location
end
def location()
update()
@location
puts @location
end
def update()
@location = @location.upcase()
end
public :location
private :update
end
# Create new class instance and call location public method.
n = Navigator.new("japan")
n.location()
Output
JAPAN
Example that calls private method: Ruby
n = Navigator.new("japan")
n.update()
Output
/Users/sam/Documents/test.rb:24:in '<main>':
private method 'update' called for #<Navigator:0x007fa3cb082200
@location="japan"> (NoMethodError)
Reopen: Modules can be reopened and changed many times. We can modify them. We just specify the module another time to make changes.
Syntax: We can access a method or type (like a class) within a module with two ":" characters. We access "Cat::meow" as an example.
Ruby program that uses module, include
module Cat
def pet()
puts "Cat petted"
end
end
# Reopen and change the module (use mix-in).
module Cat
def meow()
puts "Cat meows"
end
end
# Include the module and call its methods.
include Cat
Cat::pet()
Cat::meow()
Output
Cat petted
Cat meows