You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.5 KiB
2.5 KiB
RUBY CLASSES
#1. Classes Watch the short video here. 6 mins 23 seconds.
##Exercises:
- Create a Dog class that takes in a name and puts
"Meet #{name}, your new dog!"oninitialize.
- Assign the dog a random fur color on initialize
- Create a Bird class that takes in an adjective and a name and puts
"#{name} is a(n) #{adjective} bird!"oninitialize.
- Assign the bird a species at random on initialize
#2. Instance Methods Watch the short video here here. 8 mins 22 seconds.
##Exercises:
- Add instance methods to your dog
- Add a
barkinstance method to your dog that puts"#{name} is barking!" - Add a
growlinstance mthods that puts "Grrrrrrr..."
- Add instance methods to your bird
- Add a
hungrymethod that puts"#{name} wants a cracker!" - Add a
flymethods that puts"Flap! #{name} is taking flight!"
- Make your bird's ajective and name accessible
#3. Class Methods and 'Self' Watch the short video here. 8 mins 28 seconds.
##Exercises:
- Experiment with
self
- Add a class method
self.what_is_selfto your Bird class and have it putself. - Add an instance method
what_is_selfto your Bird class and have it putself. - Try calling both of these methods in Pry and see what they return.
- Add a class method to make puppies!
- add an instance variable of quality to your dog (here we will put a characteristic like 'playful', 'brave', 'loyal')
- add a class method of
make_puppyto your dog class that takes in two dogs and returns a new dog. - the new dog should have fur color of the first dog and the quality of the second dog.
- make puppies!
#4. Inheritance Watch the short video here. 6 mins 56 seconds.
##Exercises:
- Create a class called
Dessertthat has instance variables of@nameand@sugar_content - Give it an instance method of
eatthat puts"Yum! This #{name} is sooooo delicious!" - Make two new classes called
PieandDeepFriedDessertthat inherit fromDessert - Give the
DeepFriedDessertits owneatmethod that instead puts"Yum! This #{name} is sooo...ack! ugh! *heart-attack*" - Make a new class
IceCreamthat inherits fromDessertusesuperto get the instance variables of@nameand@sugar-content. Also giveIceCreamits own unique instance variable of@toppings