Skip to content

Instantly share code, notes, and snippets.

@marekdano
Last active November 5, 2015 15:08
Show Gist options
  • Save marekdano/da05c7f8ae91dea5d59e to your computer and use it in GitHub Desktop.
Save marekdano/da05c7f8ae91dea5d59e to your computer and use it in GitHub Desktop.
careerfoundry intro to ruby
class Cat
attr_reader :color, :breed
attr_accessor :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
kitten = Cat.new("white", "pet")
puts "What class does our new cat belong to?"
puts kitten.class
puts "Is our new cat an object?"
puts kitten.is_a?(Object)
puts "Let's give our new cat a name"
puts kitten.name = "Lia"
puts "Is our cat hungry now?"
kitten.hungry?
puts "Let's feed our cat"
kitten.feed("tune")
puts "Is our cat hungry now?"
kitten.hungry?
puts "Our cat can make noise"
kitten.speak
def fav_foods
food_array = []
3.times do
puts "Name a favourite food."
food_array << gets.chomp
end
p food_array
puts "Your favourite foods are #{food_array.join(", ")}."
end
def defined_food
my_food = ["sausage", "crisps", "bacon"];
puts "My defined foods are #{my_food}"
my_food.sort!
puts "My foods in sorted order #{my_food}"
puts "I also like #{my_food.slice(1).slice(1..5).sub(/s/, '')}!!!"
puts "I'd say this is: "
my_food.map {|food| puts "my #{food}"}
login = { username: "marek", password: "howareyouhacker?" }
puts "#{login[:username]}'s password is \"#{login[:password]}\""
end
fav_foods
puts "\n\n"
defined_food
class Pet
attr_reader :color, :breed
attr_accessor :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
end
class Cat < Pet
def speak
puts"Meow!"
end
end
class Dog < Pet
def speak
puts self
puts "Woof!"
end
end
kitten = Cat.new("white", "Persian")
puts "What class does our new cat belong to?"
puts kitten.class
puts "Is our new cat an object?"
puts kitten.is_a?(Object)
puts "Let's give our new cat a name"
puts kitten.name = "Lia"
puts "Is our cat hungry now?"
kitten.hungry?
puts "Let's feed our cat"
kitten.feed("tune")
puts "Is our cat hungry now?"
kitten.hungry?
puts "Our cat can make noise"
kitten.speak
puts "\n\n"
puppy = Dog.new("white", "Hassky")
puppy.speak
puts "Says #{puppy.color.upcase} #{puppy.breed.upcase}"
puts puppy.hungry?
def greeting
puts "Please enter your name: "
name = gets.chomp
puts "Hello " + name
end
greeting
if (5+5==10)
puts "this is true"
else
puts "this is false"
end
#################################################
#
# Choose method using if, else statement
#
#################################################
def if_choose
puts "Do you like programing? Yes or no please."
choice = gets.chomp
if (choice.downcase) == "yes"
puts "That's great!"
elsif(choice.downcase == "no")
puts "That's too bad!"
else
puts "That wasn't a yes or no."
end
end
if_choose
#########################################
#
# Choose method using switch statement
#
#########################################
def switch_choose
puts "Do you like programing? Yes, no or maybe please."
choice = gets.chomp
case choice.downcase
when "yes"
puts "That's great!"
when "no"
puts "That's too bad!"
when "maybe"
puts "Glad you are giving it a chance!"
else
puts "I have no idea what that means."
end
end
switch_choose
# number = 0
# while (number <= 10) do # while this condition is true, do...
# p "the number is now #{number}" # whatever is in this code block
# number += 1 # short for number = number + 1
# end # don’t forget your end
3.times { puts "Hi there!" }
(0..10).each do |n|
p "Then new number is #{n}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment