Created
September 4, 2014 22:03
-
-
Save iamsok/2a96dc806aac9592f6d5 to your computer and use it in GitHub Desktop.
exercise overview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# exercise is a hash | |
exercise = { name: 'jog', category: 'cardio', duration: 10 } | |
exercise.[](:name) # .[]() is a method we can call on only hashes | |
# exercise is an Exercise object | |
exercise = Exercise.new(exercise) | |
# exercise[:name] CAN'T DO THIS! exercise is not a hash anymore, so we can't call hash methods on it | |
# instead, we need to use the attr_reader methods to get attributes of the exercise | |
exercise.name # .name is a method we can call only on Exercise objects | |
class Exercise | |
attr_reader :name # or def name; @name; end | |
def initialize(exercise_data) | |
@name = exercise_data[:name] | |
@category = exercise_data[:category] | |
@duration = exercise_data[:duration] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment