Keepin it Classy

Nichole Shelton

November 30, 2014

Currently I'm pretty obsessed with yoga. I definitely need it to help me get through DBC! Right now I'm working on the coveted headstand. Believe, once I achieve it, I'll be doing a little of this:
Handstand Gif

I like to infuse my coding sessions with a little yoga. Let's see if I can achieve that in this blog post. We're going to discuss

What is a class?

A class is an object. According to the Well Grounded Rubyist "a class let's your group behaviors (methods) into convenient bundles, so that you can quickly create many objects that behave essentially the same way." In other words, classes are a way to objects and methods. Below, I'm going to create a class Yoga. Classes have to appear in CamelCase.

class Yoga def method_name end end

Pretty simple. Now we'll need to initialize our instance variables.

What's an instance variable?

Instance variables are pretty cool. They are variables that you create and are accessible throughout the entire life of the class (to the class end statement). You can recognize the instance variables by the @ sign in front of them. Let's add two instance variables to our Yoga class. I want to add the type of yoga and the favorite asana. In order to do this you must define a method "initialize". This will store the instance variables and allow the programmer to access them through the life of the class.

class Yoga def initialize(type, asana) @type = type @fav_asana = fav_asana end end

Very good. We're getting somewhere. Now let's add a few methods and see how this little program will work.

class Yoga def initialize(name, type, asana) @name = name @type = type @asana = asana end def name @name end def type @type end def fav_asana @asana end def add_them_together return "#{@name}'s favorite type of yoga is #{@type} and she loves to practice the #{@asana}." end end

When we call the class we need to define a new variable (be descriptive) and call the class using the class name + .new Check it.

my_yoga = Yoga.new("Nichole", "Ashtanga", "Shirshasana - headstand") puts my_yoga.fav_asana

And our output is the following:

Nichole's favorite type of yoga is Ashtanga and she loves to practice the Shirshasana - headstand.

We've learned so much, but that's not all! This week, we learned about these nifty syntatic sugary methods. I love the saying DRY - Don't Repeat Yourself. Attributes allow you to shorten your code. Notice the methods above where we simply defined the method and set it to the instance variable. Look at the 3 methods above (type, name, fav_asana)...Go ahead scroll up... I"ll wait. So we can get rid of alllll of that code. How might you ask? Getter and setter attributes. I could probably do a blog post on just them so I'm going to just leave a little link for you here to read up on them: Getter and Setter Attributes. Did ya read it? Awesome. Let's take a look at how these attributes help us to consolidate our code. I'm just going to use the attr_reader method here.

class Yoga attr_reader :name, :type, :asana def initialize(name, type, asana) @name = name @type = type @asana = asana end def add_them_together return "#{@name}'s favorite type of yoga is #{@type} and she loves to practice the #{@asana}." end end my_yoga = Yoga.new("Nichole", "Ashtanga", "Shirshasana - headstand") puts my_yoga.add_them_together

OUTPUT
Nichole's favorite type of yoga is Ashtanga and she loves to practice the Shirshasana - headstand.

The output was the same! Hope this was helpful. I know it certainly helped me to learn by writing it out.