How to Create a Class in Ruby
Ruby is a language built for defining classes, as it is first and foremost an object-oriented language. Ruby provides a number of shortcuts to create a class, which can be easily followed for those who have a bit of experience with the language.
Instructions
-
Create a Class in Ruby
-
1
Choose a short and descriptive name for your class. The name should be concise enough that anyone reading your code can have a good idea of what the class does.
-
2
Use Camel case for the class name. Camel case uses a capital letter at the beginning of the class name and capitalize the first letter of the next word when joining words. This is instead of using an underscore. For example, Ruby method names look like this:
method_name;but class names should look like this:ClassName. -
-
3
Create an empty class statement:
class TestClass
end -
4
Plan methods for for each thing your class should be able to do. Write, one by one, empty methods for each of the methods. A Microwave class, for example, should be able to do things like turn on and put food in it:
class Microwave
def put_food_in
enddef take_food_out
enddef turn_on
enddef turn_off
end
end
Write the Code for the Methods
-
5
Use the @variable syntax if the methods have to store a variable inside of an object. The "at" symbol basically says "This variable is not a local variable, it's a member variable. It belongs to the object." You can also use local variables in class methods. The example uses the @food member variable, wherein the variable stores what food is currently in the microwave:
class Microwave
def put_food_in(food)
@food = food
enddef take_food_out
food = @food
@food = nil
return food
enddef turn_on
puts "Microwave is on"
enddef turn_off
puts "Microwave is off"
end
end -
6
Begin a label with a ":" character and the name of the variable. For example, ":food". Here ":food" means "the thing called food." "@food" can't be passed to the attr_* methods, because that would pass the value of "@food". Since you essentially want to say "the thing called food can be accessed from outside the class," ":food" should be used.
-
7
Put one of the following three types of attr_* methods in your class, but outside of any methods. For example,
attr_readerallows code outside the class to read the variable, but not write to it. On the other hand,attr_writerallows code outside the class to write to a variable, but not read it. Finally,attr_accessorallows both reading and writing. -
8
Put one of the following three types of attr_* methods in your class, but outside of any methods:
class Microwave
attr_reader :fooddef put_food_in(food)
@food = food
end# ... the rest of the methods go here, taken out for brevity
end
-
1
Tips & Warnings
If you're reading blocks of code, be sure you disregard anything written after the operator "#." These are notes designed to help the programmers. While these notes are beneficial when there is an entire team working on the same program, they can be easily confused with code block to the untrained eye.