Hooray for Classes!

Boooo chores

Posted by Ricky Thomas on October 8, 2014

When I was a boy, my sister and I would always argue about who had to do which chores over the weekend. I always wanted to mow the lawn because at age ten the driving lawn mower may as well have been Falcor from "The Neverending Story." It rocked. I absolutely hated doing the dishes, and being the younger child, it seemed that was always my task. I am telling you...somehow...she rigged it! That damn sister of mine! So for this blog I'm going to solve one of my ten year old problems.

Creating a ChoreChart Class

class ChoreChart
def initialize
@chores = ["sweep", "vacuum", "rake", "dishes", "bathroom", "mow"]
@ricky = []
@alicia = []
end
end

So in the above code. We are establishing a new class called 'ChoreChart' and initializing a few instance variables. Those are the words with an '@' at the start of them. These variables can be set and used now throughout any other methods we create in the ChoreChart class. So now, as you can see, we've made two empty arrays (representing myself and my sister) and one array containing six strings representing all of the weekend chores. The next thing that we want is a way to randomly assign three chores each to my sister and I. Let's create a ChoreChart method for this:

def assign
into_hat = @chores.shuffle
@ricky << into_hat.first(3)
@alicia << into_hat.drop(3)
end

In the method above (which will be included in the class), we shuffle up the chore names in the @chore array and make that into a new array that I've designated as 'into_hat.' Just like putting slips of paper with chores on them into a hat! Then we put the first three chores in the @ricky array and the last three into the @alicia array.

So now that we have a way to mix up the chores, and split them in a fair and equal manner, we can build one last method to tell us what our chores are for the weekend:

def tell
@ricky = @ricky.join(", ")
puts "Ricky's chores: #{@ricky}."
@alicia = @alicia.join(", ")
puts "Alicia's chores: #{@alicia}."
end

In the 'tell' method above, you can see that we turn our new and improved versions of @ricky and @alicia ([random_chore, random_chore, random_chore]) and join all the elements into one long string that we now reset @ricky and @alicia too. Then we print each list to the console with puts.

Here's the whole class together:

class ChoreChart
def initialize
@chores = ["sweep", "vacuum", "rake", "dishes", "bathroom", "mow"]
@ricky = []
@alicia = []
end

def assign
into_hat = @chores.shuffle
@ricky << into_hat.first(3)
@alicia << into_hat.drop(3)
end

def tell
@ricky = @ricky.join(", ")
puts "Ricky's chores: #{@ricky}."
@alicia = @alicia.join(", ")
puts "Alicia's chores: #{@alicia}."
end

end

Now consider running following code:

weekend_chores = ChoreChart.new
weekend_chores.assign
weekend_chores.tell

The first line of the above code creates a new instance of our class titled "weekend_chores."

Next, we call our assign method on weekend_chores so that our instance variables (@ricky and @alicia) are given their chore assignments.

Finally, we can call our tell method and see printed out lists telling us which chores we have! Wooooooo chores!!!

Can you see how the instance variables we created are used throughout multiple methods in the new class? Also, note that we represent a real world object with ChoreChart. Data structures often can be set up to resemble real world objects. For example, five arrays of five elements each stacked on top of one another looks just like a bingo board!
I hope this helps you in your learning and thanks for reading!!
-Ricky