Skip to content

Instantly share code, notes, and snippets.

@iamsok
Created September 4, 2014 22:03
Show Gist options
  • Save iamsok/2a96dc806aac9592f6d5 to your computer and use it in GitHub Desktop.
Save iamsok/2a96dc806aac9592f6d5 to your computer and use it in GitHub Desktop.
exercise overview
# 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