ruby classes

Posted on June 03, 2011 @ 21:44 ruby oop

Programming Ruby 1.9

The basics

1
2
3
4
5
6
7
8
  class Book
    def initialize(isbn, price)
      @isbn = isbn
      @price = price
    end
  end

  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
2
3
4
5
6
7
8
9
10
11
12
  class Movie
    def initialize(name, studio, year_published)
      @name = name
      @studio = studio
      @year_published = year_published
    end
    def to_s
      "Name: #{@name}, Studio: #{@studio}, Year: #{@year_published}"
    end
  end

  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
2
3
4
5
6
7
8
9
10
11
  class VideoGame
    def initialize(name)
      @name = name
    end
    def name
      @name
    end
  end

  game = VideoGame.new("call of duty")
  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
2
3
4
5
6
7
8
9
10
11
  class VideoGame

    attr_reader :name

    def initialize(name)
      @name = name
    end
  end

  game = VideoGame.new("call of duty")
  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
2
3
4
5
6
7
8
9
10
11
12
  class VideoGame

    def initialize(name)
      @name = name
    end
    def name=(new_name)
      @name = new_name
    end
  end

  game = VideoGame.new("call of duty")
  game.name="GTA4"

There’s also a shorthand for having to write your own setter methods. It’s called attr_writer.

1
2
3
4
5
6
7
8
9
10
11
  class VideoGame

    attr_writer :name

    def initialize(name)
      @name = name
    end
  end

  game = VideoGame.new("call of duty")
  game.name="GTA4"

There’s also a shorthand for defining a setter, and getter for your instance variable called “attr_accessor”

1
2
3
4
5
6
7
8
9
10
11
12
  class VideoGame

    attr_accessor :name

    def initialize(name)
      @name = name
    end
  end

  game = VideoGame.new("call of duty")
  game.name="GTA4"
  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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  class MyClass
    def public_method
    end

  protected
    def protected_method
    end

  private
    def private_method
    end

  public 
    def another_public_method
    end
  end
  
# or

  class AnotherClass
    def public_method
    end

    def protected_method
    end

    def private_method
    end

    public      :public_method
    protected   :protected_method
    private     :private_method
  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
2
3
4
5
6
  movie = Movie.new("man on fire")
  another_movie = movie
  movie = Movie.new("batman")

  puts movie.name
  puts another_movie.name