



(3 ratings)
Classes and methods
Now we are ready to create our very own Address class. Let's start simple. Let's start with an address that only contains the "street" field.
This is how you define a class:
class Address |
Let's go through this:
The class keyword defines a class.
By defining a method inside this class, we are associating it with this class.
The initialize method is what actually constructs the data structure. Every class mustinitialize method. contain an
@street is an object variable. Similar to the keys of a hash. The @ sign distinguishes @street as an object variable. Every time you create an object of the class Address, this object will contain a @street variable.
Let's use this class to create an address object.
address = Addres.new("23 St George St.")
|
That's it. address is now an object of the class Address
Reading the data in an object
Suppose that we want to read the data in the address object. To do this, we need to write a method that returns this data:
class Address |
Now the method Address#street lets you read the street of the address. In irb:
>> address.street |
A property of an object, which is visible outside, is called an attribute. In this case, street is is an attribute. In particular, it is a readable attribute. Because this kind of attribute is very common, Ruby offers you a shortcut through the attr_reader keyword:
class Address |
Changing the data in an object
We can also define a method to change the data in an object.
class Address |
Ruby is pretty smart in its use of the street= method:
address.street = "45 Main St." |
Notice that you can put spaces betten street and =. Now that we can change the address data, we can simplify the initialize method, and have it simply default the street to the empty string "".
class Address |
This might not seem like much of a simplification, but when we add the city, state and zip fields, and more methods this will make the class definition a bit simpler.
Now, street is also a writable attribute. As before, you can declare it as such with attr_writer:
class Address |
Accessing data
Very often, you have attributes that are both readable and writable attributes. Ruby lets you lump these together with attr_accessor. I guess these would be called "accessible attributes", but I have never seen them be called that.
class Address |
With this knowledge, it is now easy to define the entire addressbook structure. As it turns out, attr_accessor and friends all accept multiple arguments.
class Address
attr_accessor :street, :city, :state, :zip
def initialize
@street = @city = @state = @zip = ""
end
end
20 Random Tutorials from the same category :
Classes and methods in RUBY
Writing iterators In RUBY
More features IN RUBY
Printing the addressbook RUBY
What can ruby arrays do?
Progress Bars with GD2 and Ruby
The Scalability of Ruby
Hashes In RUBY
Difference of Gtk+ and Ruby/Gtk
Implementing AddressBook In RUBY
Sorting the addressbook RUBY
Iterators In RUBY
Example: Addressbook RUBY
Arrays In RUBY













