- Create methods that print the steps for different dances moves using
def
- Replay these moves back by calling them systematically
When we learned about printing directly to the console through the Macarena, the steps were quite literal and simply echoed back each individual line as desired. Now that we know what methods are, we can use them to consolidate or package individual steps and instructions together. This will prove to be incredibly useful! Let's put our smooth moves to the test and dance Gangnam Style.
Roughly, the abstracted components involved are as follows:
Assume the proper stance
- Plant legs far apart, bend knees slightly and keep posture loose
Base Footwork
-
Lift right foot
-
Return right foot to the ground
-
Finishing with a small skip-step backward
Skip Step Left
- Lower left foot to the ground
- Bounce left foot back up slightly, kicking it a few inches back
- Left, Right, Left and Left
Skip Step Right
- Lower right foot to the ground
- Bounce right foot back up slightly, kicking it a few inches back
- Right, Left, Right and Right
- Holding your arms out in front of you, straight and at chest level
- Cross your right wrist over your left and hold them together
- Lift your arms up and down in a loose bouncing movement, in time with the beat of the song. This movement is repeated eight times.
Lasso
- Start arm movement by holding left arm so the back of your wrist is near your chin, pointing your left elbow straight left.
- Lift right arm up and point right elbow diagonally to the right.
- Raise right forearm so that it points straight up, and whip it in small circles to the beat of the song, as if you were a cowboy holding a rope lasso. This movement is repeated eight times as well.
- Be cool
- Be snazzy!
Now that we've dissected each piece of the dance, let's learn the proper sequence these should occur. When we're grooving on the dance floor we don't usually think about how our upper body is coordinating with our leg movement since it often happens so naturally. But just like any good programmer knows, the computer never assumes anything, so we'll have to be very literal and bring these movements together simultaneously.
Bob the reins eight times, WHILE ALSO stepping right, left, right, right, then left, right, left, left, all at the same time. Your arm movements and steps should match up.
THEN
Lasso eight times WHILE ALSO stepping RLRR, LRLL.
- Assume stance
- Bust a move
- Footwork
- Bob the reins + skip-steps x8
- Lasso + skip-steps x8
Now that we know how to do the dance correctly, let's perform it using methods in Ruby.
D.R.Y. is a good principle to abide by while writing code. It stands for Don't Repeat Yourself:
"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."
In other words, the DRY principle states that duplication in logic should be eliminated via abstraction; duplication in process should be eliminated via automation. This is exactly what we're doing with methods! Instead of writing all the steps out individually, we've packaged them nicely into methods.
-
Write a method for each section of the dance in
dance_instructions.rb
. This method shouldputs
out strings advising the user what to do for that particular element of the dance. -
Then, call the methods at the end of
dance_instructions.rb
, ensuring they are called in the proper order. This will bring all the elements of the dance together, and we can do so in code very elegantly.