Skip to content

Instantly share code, notes, and snippets.

@kajatiger
Created October 20, 2016 12:01
Show Gist options
  • Save kajatiger/ce3cd2607fd2f295f642b6f2274b22d5 to your computer and use it in GitHub Desktop.
Save kajatiger/ce3cd2607fd2f295f642b6f2274b22d5 to your computer and use it in GitHub Desktop.
creating a class
class Cat
attr_reader :color, :breed #:name
attr_accessor :name #instead of mentioning the attribute both in reader and writer, we can do both in the same time with accessor
#attr_writer :name
def initialize(color, breed)
@color = color
@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
def speak
puts "meow!"
end
end
kitty = Cat.new("grey", "Persian")
puts "lets inspect our new cat"
puts kitty.inspect
puts "what class does it belong to?"
puts kitty.class
puts "is our new cat an object?"
puts kitty.is_a?(Object)
puts "what color is your cat?"
puts kitty.color
puts "give your cat a new name"
kitty.name = "tiger"
puts kitty.name
puts "Is our cat hungry now?"
kitty.hungry?
puts "Let's feed our cat"
kitty.feed("tuna")
puts "Is our cat hungry now?"
kitty.hungry?
puts "our cat can make noise"
kitty.speak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment