Class and Instance Variables in Ruby

Sunday, November 30, 2014

In Ruby, creating a class allows you to apply methods to an object. With instance methods, each time an object is created from the class, it will do the same thing as the other objects. For example, let's pretend that you are running a senior home where all three meals are provided and served to the elders living there. You can create a class DiningRoomPlan that looks at what each senior will want for their breakfast, lunch, and dinner based on their preferences.

So in this case, the object will be the senior citizen for the class DiningRoomPlan. The instance methods breakfast, lunch, and dinner let us know how they like their chicken prepared (texture) and spice level preference (spice). How do we know what the texture variable and spice variable are? They are given to us when that person is created i.e. it is part of the arguments when we create a new object with .new


class DiningRoomPlan
    def initialize(texture, spice)
      @texture = texture
      @spice = spice
    end

    def breakfast
      puts "I want to eat #{@texture} chicken omelette with a side of fried potatoes, #{@spice} ."
    end

    def lunch
      puts "I'm craving #{@texture} chicken salad with tortilla soup, #{@spice}."
    end

    def dinner
      puts "Tonight I will have #{@texture} chicken with some veggie fried rice, #{@spice}."
    end
end

You can see that texture and spice are now instance variables (denoted with @ before their name) so that we can use it for not only breakfast method, but all the methods in the class. Instance variables allow us to expect information from the arguments every time an object (the senior citizen) is created. It means that every time we add a person to DiningRoomPlan class, the computer will spit out their preferences so that you can prepare their meals accordingly.

We call the method .breakast on esther to see what she would want for breakfast.


esther = DiningRoomPlan.new("extra crispy deep fried", "a little bit spicy")

puts esther.breakfast
puts "Hurry up, I don't have all day here!"
puts
puts esther.lunch
puts "Are you tone-deaf? Go get it!"
puts
puts esther.dinner
puts "Well?? What are you waiting for? I'm hungry!"
puts

=> I want to eat extra crispy deep fried chicken omelette with a side of fried potatoes, a little bit spicy.
=> Hurry up, I don't have all day here!

=> I'm craving extra crispy deep fried chicken salad with tortilla soup, a little bit spicy.
=> Are you tone-deaf? Go get it!

=> Tonight I will have extra crispy deep fried chicken with some veggie fried rice, a little bit spicy.
=> Well?? What are you waiting for? I'm hungry!

Now let's see what happens when we create another person called theodore:


theodore = DiningRoom.new("grilled", "very spicy, like 1 to 5 (5 being the hottest), level 6 spicy")

puts theodore.breakfast
puts "Thanks sweetheart, and grab me a bottle of that Tabasco, why dontcha."
puts
puts theodore.lunch
puts "Don't forget about the Taptio hot sauce!"
puts
puts theodore.dinner
puts "You know to bring extra sriracha right?"

=> I want to eat grilled chicken omelette with a side of fried potatoes, very spicy, like 1 to 5
(5 being the hottest), level 6 spicy.
=> Thanks sweetheart, and grab me a bottle of that Tabasco, why dontcha.

=> I'm craving grilled chicken salad with tortilla soup, very spicy, like 1 to 5 (5 being the hottest),
level 6 spicy.
=> Don't forget about the Taptio hot sauce!

=> Tonight I will have grilled chicken with some veggie fried rice, very spicy, like 1 to 5 (5 being
the hottest), level 6 spicy.
=> You know to bring extra sriracha right?

So, here you see that the purpose of using a class method is when you create objects (in our case esther and theodore) and they all behave the same way (tell us how they want food prepared), thanks to the instance methods (breakfast, lunch, dinner) defined inside the class (DiningRoomPlan).

And now you know about classes! *clap clap*