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:
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
class Yoga def method_name end end
Pretty simple. Now we'll need to initialize our
What's an instance variable?
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
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
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.

