The basics
1 class Book
2 def initialize(isbn, price)
3 @isbn = isbn
4 @price = price
5 end
6 end
7
8 book = Book.new("isbn", 3)
initialize is a special method in Ruby programs. When you call Book.new to create a new object, Ruby allocated memory to hold an uninitialized object and then calls that object's initialize method. T
initialize is an objects constructor. The call to Book.new calls the Book's constructor (initialize) and allocates a new instance of Book.
@isbn in an instance field of the class. when you want to declare a instance field you must prefix the name of the variable with the "@" sign.
To customize how an object is displayed via puts or p you can override the "to_s" method.
1 class Movie
2 def initialize(name, studio, year_published)
3 @name = name
4 @studio = studio
5 @year_published = year_published
6 end
7 def to_s
8 "Name: #{@name}, Studio: #{@studio}, Year: #{@year_published}"
9 end
10 end
11
12 p Movie.new("MegaMind", "DreamWorks", 2010)
Attributes (methods)
By default instance variables are private and cannot be accessed from outside the class. You can define "attributes" on the class to be able to see and change the internal state of the class. These are equivelant to properties in c#.
You can define read only attribute explicitly, like this.
1 class VideoGame
2 def initialize(name)
3 @name = name
4 end
5 def name
6 @name
7 end
8 end
9
10 game = VideoGame.new("call of duty")
11 puts game.name
Ruby has a handy little shorthand to save you from having to type out your read only attributes. It's called "attr_reader".
1 class VideoGame
2
3 attr_reader :name
4
5 def initialize(name)
6 @name = name
7 end
8 end
9
10 game = VideoGame.new("call of duty")
11 puts game.name
To create a method that would allow me to change the name of a video game I can define a method named "name=".
1 class VideoGame
2
3 def initialize(name)
4 @name = name
5 end
6 def name=(new_name)
7 @name = new_name
8 end
9 end
10
11 game = VideoGame.new("call of duty")
12 game.name="GTA4"
There's also a shorthand for having to write your own setter methods. It's called attr_writer.
1 class VideoGame
2
3 attr_writer :name
4
5 def initialize(name)
6 @name = name
7 end
8 end
9
10 game = VideoGame.new("call of duty")
11 game.name="GTA4"
There's also a shorthand for defining a setter, and getter for your instance variable called "attr_accessor"
1 class VideoGame
2
3 attr_accessor :name
4
5 def initialize(name)
6 @name = name
7 end
8 end
9
10 game = VideoGame.new("call of duty")
11 game.name="GTA4"
12 puts game.name
Access Control
You get 3 levels of protection:
- Public methods (default): can be called by anyone.
- Protected methods: can be invoked by defining class and subclasses.
- Private methods: can only be called within the context of the current object.
It is never possible to access another object's private methods directly, even if the object is of the same class as the caller.
1 class MyClass
2 def public_method
3 end
4
5 protected
6 def protected_method
7 end
8
9 private
10 def private_method
11 end
12
13 public
14 def another_public_method
15 end
16 end
17
18 # or
19
20 class AnotherClass
21 def public_method
22 end
23
24 def protected_method
25 end
26
27 def private_method
28 end
29
30 public :public_method
31 protected :protected_method
32 private :private_method
33 end
Variables
Variables are used to keep track of objects; each variable holds a reference to an object. A variable is simply a reference to an object.
1 movie = Movie.new("man on fire")
2 another_movie = movie
3 movie = Movie.new("batman")
4
5 puts movie.name
6 puts another_movie.name