4.3 KiB
WDI-PANTHALASSA
Title: Ruby classes I
Type: Homework
Duration: One evening
Creator: Thom Page
Course: WDIr-Panthalassa
Competencies: Ruby classes, instance methods, inheritance
RUBY CLASSES
#1. Classes Watch the short video here. 6 mins 23 seconds.
##Exercises:
- Create a Muppet class that takes in a
nameand puts"This muppet is called #{name}!"oninitialize.
- Assign the muppet a random color on initialize.
- Create a Bird class that takes in an
adjectiveand anameand 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 Muppet
- Add a
honkinstance method to your muppet that puts"#{name} is making a honking sound!". Test that it works. - Add a
flailinstance mthods that puts"#{name} is flailing its arms!". Test that it works.
- Add a
- Add instance methods to your Bird
- Add a
hungrymethod that puts"#{name} wants some regurgitated worms!". Test that it works. - Add a
flymethod that puts"Flap! #{name} is taking flight!". Test that it works.
- Add a
- Make your bird's
adjectiveandnameaccessible. Test that this works.
#3. 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
#5. Reps with Classes
The Universe
What's in a Class? Make a class called Universe that:
-
Takes three parameters. These are the three things within the universe. Save these things to instance variables using an
initializemethod. -
Has an instance variable
@expanding = true -
Has an instance variable
@conservation = true -
Has a method that will print out all the things in this universe.
-
Has a method called
createthat takes a parameter and will add a new thing to the universe (taken from the parameter). Ifconservation = truethen one of the three things in the universe is replaced by the new thing. If not, then a new thing is added to the others. -
Has a method that changes
expandingtofalseif there are more than ten things in the universe, which means the universe is now contracting . . . (there's no stable universe in this scenario). -
Has a method called
whimthat changesconservationfromtruetofalse -
Has a method called
crunchwherein, ifexpandingequalsfalse, removes each item from the universe (sets it to null) and prints a string saying the name of the item and that it has been crunched due to gravity. When the final item remains, remove that item and print "The Universe has ended."
Test out your Universe by instantiating a new Universe with its parameters and running your commands.
#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
- Add a class method to make puppies!
- add an instance variable that adds 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!
#X. Ruby problems
100 goblins caesar cipher
