Created
October 7, 2017 22:34
-
-
Save khan-hasan/6446066d4b726018cc8ea3da99c2dd70 to your computer and use it in GitHub Desktop.
CF 3.5 | oop, pt. 2 (instance methods, inheritance, etc._
This file contains hidden or 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
str.is_a?(Object) | |
str.class | |
# here we extend Ruby's string class meaning that all strings in the environment will belong to this class | |
class String | |
def star | |
self + " *" | |
end | |
end | |
puts str.star | |
puts "yay".star | |
class String | |
def self.announce | |
puts "I am the String class" | |
end | |
end | |
# The class above uses the keyword self, which refers to the instance of the class on which the method is called (the particular object). Class methods are methods that work on the class rather than an instance of the class. | |
puts String.announce # works | |
puts "hello".announce # error |
This file contains hidden or 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
# everything that Cats and Dogs have in common go in this Pet class | |
class Pet | |
attr_reader :color, :breed, :name | |
# There is no need for a separate attr_writer and separate attr_reader for name because attr_accessor can combine those methods into one (DRY) | |
# attr_writer :name | |
attr_accessor :name | |
def initialize(color, breed) | |
@color = color # Instances variables are available to other methods inside of this class. This is unlike local variables, which only exist inside the method they're called in. | |
@breed = breed | |
@hungry = true | |
end | |
def feed(food) | |
puts "Mmmm, " + food + "!" | |
@hungry = false | |
end | |
def hungry? | |
if @hungry | |
puts "I\'m hungry!" | |
else | |
puts "I\'m full!" | |
end | |
@hungry | |
end | |
end # end Pet class | |
# Cat class which inherits from Pet class | |
class Cat < Pet | |
def speak | |
puts "Meow!" | |
end | |
end | |
# Dog class which inherits from Pet class | |
class Dog < Pet | |
def speak | |
puts "Woof!" | |
end | |
end | |
kitty = Cat.new("grey", "Persian") | |
puts "Let's inspect our new cat:" | |
puts kitty.inspect | |
puts "What class does our new cat belong to?" | |
puts kitty.class | |
puts "Is our new cat an object?" | |
puts kitty.is_a?(Object) | |
puts "What color is our cat?" | |
puts kitty.color # if the class has not attr_reader, this returns and error because 'color' is an undefined method for class Cat. When an attr_reader is added to the class, the error goes away. | |
# kitty.color = "orange" # this will return an error if the Cat class doesn't have an attr_writer | |
puts "Let's give our new cat a name" | |
kitty.name = "Betsy" | |
puts kitty.name | |
puts "Is our cat hungry now?" | |
kitty.hungry? | |
puts "Let's fed our cat" | |
kitty.feed("tuna") | |
puts "Is our cat hungry now?" | |
kitty.hungry? | |
puts "Our cat can make noise" | |
kitty.speak | |
# instance of Dog class | |
puppy = Dog.new("white", "Husky") | |
puppy.speak # "Woof!" | |
puts puppy.color # "white" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment