Skip to content

Instantly share code, notes, and snippets.

@Jhowden
Last active December 23, 2015 18:59
Show Gist options
  • Save Jhowden/6679767 to your computer and use it in GitHub Desktop.
Save Jhowden/6679767 to your computer and use it in GitHub Desktop.
def bottle_song(n)
# n.downto(3) do |x|
# puts "#{x} bottles of beer on the wall, #{x} bottles of beer."
# puts "Take one down, pass it around, #{x-1} bottles of beer on the wall!"
# end
if n == 2
puts "#{n} bottles of beer on the wall, #{n} bottles of beer."
puts "Take one down, pass it around, #{n-1} bottle of beer on the wall!"
puts "#{n-1} bottle of beer on the wall, #{n-1} bottle of beer."
puts "Take one down, pass it around, no more bottles of beer on the wall"
else
puts "#{n} bottles of beer on the wall, #{n} bottles of beer."
puts "Take one down, pass it around, #{n-1} bottles of beer on the wall!"
bottle_song(n-1)
end
end
bottle_song(99)
# This is how you define your own custom exception classes
class NoOrangesError < StandardError
end
class OrangeTree
attr_reader :age, :height, :oranges
attr_accessor
def initialize
@age = 0
@height = 0
@oranges = []
@dead = false
end
# Ages the tree one year
def age!
@age += 1
self.height!
orange_producing
end
def height!
if @age < 5
@height += 1 + rand(6)
elsif @age >= 5 && @age < 20
@height += 1 + rand(3)
else
@height
end
end
def dead?
age == 25 && @dead == false ? true : false
end
def orange_producing
(1 + rand(15)).times { |oranges| @oranges << Orange.new } unless @age < 5 || @age >= 21
end
# Returns +true+ if there are any oranges on the tree, +false+ otherwise
def any_oranges?
@oranges.size > 0 ? true : false
end
# Returns an Orange if there are any
# Raises a NoOrangesError otherwise
def pick_an_orange!
raise NoOrangesError, "This tree has no oranges" unless self.any_oranges?
# orange-picking logic goes here
picked_orange = @oranges.sample
@oranges.delete(picked_orange)
return picked_orange
end
end
class Orange
attr_reader :diameter
# Initializes a new Orange with diameter +diameter+
def initialize
@diameter = (1 + rand(5))
end
end
tree = OrangeTree.new
tree.age! until tree.any_oranges?
puts "Tree is #{tree.age} years old and #{tree.height} feet tall"
until tree.dead?
basket = []
# It places the oranges in the basket
# IT PLACES THE ORANGES IN THE BASKET
while tree.any_oranges?
basket << tree.pick_an_orange!
end
basket_radius = []
basket.each { |orange| basket_radius << orange.diameter }
diameter_sum = basket_radius.reduce(:+)
if diameter_sum == nil
avg_diameter = 0
else
avg_diameter = diameter_sum / basket.size
end
puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
puts "Harvest: #{basket.size} oranges with an average diameter of #{avg_diameter} inches"
puts ""
# Age the tree another year
tree.age!
end
puts "Alas, the tree, she is dead!"
# This is how you define your own custom exception classes
class NoFruitError < StandardError
end
class FruitTree
attr_reader :age, :height, :fruit
def initialize
@age = 0
@height = 0
@fruit = []
@dead = false
end
def age!
@age += 1
self.height!
fruit_producing
end
def height!
if @age < 5
@height += 1 + rand(6)
elsif @age >= 5 && @age < 20
@height += 1 + rand(3)
else
@height
end
end
def fruit_producing
(1 + rand(8)).times { |fruit| @fruit << Fruit.new } unless @age < 5 || @age >= 21
end
def dead?
@age == 25 && @dead == false ? true : false
end
# Returns +true+ if there are any oranges on the tree, +false+ otherwise
def any_fruit?
@fruit.size > 0 ? true : false
end
# Returns an Fruit if there are any
# Raises a NoFruitError otherwise
def pick_a_fruit!
raise NoFruitError, "This tree has no fruit" unless self.any_fruit?
# fruit-picking logic goes here
picked_fruit = @fruit.sample
@fruit.delete(picked_fruit)
return picked_fruit
end
def mature_tree
if @age > 5 && @age <= 21
true
else
false
end
end
end
class Fruit
attr_reader :diameter
# Initializes a new Fruit with diameter +diameter+
def initialize
@diameter = (1 + rand(5))
end
end
# puts "---------------------"
class AppleTree < FruitTree
def initialize
super()
@age = rand(5)
end
def height!
if @age < 3
@height += 1 + rand(7)
elsif @age >= 3 && @age < 9
@height += 1 + rand(5)
elsif @age >= 9 && @age < 15
@height += 1 + rand(3)
else
@height
end
end
def fruit_producing
(1 + rand(6)).times { |fruit| @fruit << Apple.new } unless @age < 3 || @age >= 16
end
def mature_tree
if @age > 3 && @age <= 16
true
else
false
end
end
def dead?
age == 19 ? true : false
end
# Returns an Orange if there are any
# Raises a NoOrangesError otherwise
def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
# apple-picking logic goes here
picked_apple = @apples.sample
@apples.delete(picked_apple)
return picked_apple
end
end
class Apple < Fruit
def initialize
super()
end
end
# puts "---------------------------"
class OrangeTree < FruitTree
def initialize
super()
@age = 1 + rand(3)
@height = 1 + rand(7)
end
# Ages the OrangeTree one year
def age!
@age += 1
self.height!
orange_producing
end
def height!
if @age < 5
@height += 1 + rand(6)
elsif @age >= 5 && @age < 20
@height += 1 + rand(3)
else
@height
end
end
def orange_producing
(1 + rand(6)).times { |oranges| @fruit << Orange.new } unless @age < 5 || @age >= 21
end
def mature_tree
if @age > 5 && @age <= 21
true
else
false
end
end
def dead?
age == 30 ? true : false
end
end
class Orange < Fruit
def initialize
@diameter = (1 + rand(7))
end
end
# puts "---------------------------"
class PearTree < FruitTree
def initialize
super()
end
def height!
if age < 7
@height += 1 + rand(7)
elsif @age >= 7 && @age < 22
@height += 1 + rand(3)
else
@height
end
end
def fruit_producing
(1 + rand(5)).times { |fruit| @fruit << Pear.new } unless @age < 7 || @age >= 22
end
def mature_tree
if @age > 7 && @age <= 22
true
else
false
end
end
def dead?
age == 26 ? true : false
end
end
class Pear < Fruit
def initialize
@diameter = (1 + rand(3))
end
end
class TreeGrove
attr_reader :grove
def initialize(tree_array)
@grove = tree_array
end
def age!
@grove.each do |fruit|
fruit.age!
end
end
def trees
@grove.each do |tree|
tree.class
end
end
def mature_trees
@grove.each_with_index do |tree,index|
if tree.mature_tree
puts "Tree #{index} is mature."
end
end
end
def dead_trees
@grove.each_with_index do |tree, index|
if tree.dead? == false
puts "Tree #{index} has died..."
end
end
end
end
array_of_trees = [PearTree.new, OrangeTree.new, AppleTree.new]
treegrove = TreeGrove.new(array_of_trees)
# 7.times do
# treegrove.age!
# end
# p treegrove.trees
def pig_latin_word(user_word)
if user_word.index(/[aeiou]/i) != 0
until user_word.index(/[aeiou]/i) == 0
user_word += user_word.slice!(0)
end
user_word += "ay"
else
user_word
end
end
# p "Please give us a word to convert to pig Latin:"
# user_word = gets.chomp
# p pig_latin_word(user_word) == "igpay"
def pig_latin_sentence(user_sentence)
sentence_breakdown = user_sentence.split()
comparative_sentence = sentence_breakdown.dup
sentence_breakdown.map! do |word|
pig_latin_word(word)
end
count = (comparative_sentence - sentence_breakdown).count
p "There has been #{count} changes."
return sentence_breakdown.join(" ")
end
p "Please give us a sentence to convert to pig Latin:"
user_sentence = gets.chomp
p pig_latin_sentence(user_sentence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment