first commit
This commit is contained in:
commit
f6abf65e91
10 files changed
+346
No files matched your search
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
class Basket
|
||||
attr_accessor :size, :color, :fruits
|
||||
|
||||
def initialize(size, color, fruits)
|
||||
@size = size
|
||||
@color = color
|
||||
@fruits = fruits
|
||||
end
|
||||
|
||||
def description
|
||||
puts "Mon panier est #{@size} et il est #{@color}."
|
||||
end
|
||||
|
||||
def contents
|
||||
puts "Il est rempli de :"
|
||||
|
||||
@fruits.each do |fruit|
|
||||
puts fruit
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
basket = Basket.new(
|
||||
"grand",
|
||||
"rouge",
|
||||
["pommes", "cerises", "bananes", "fraises", "poires"]
|
||||
)
|
||||
|
||||
basket.description
|
||||
basket.contents
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
def cards_of_suit(cards, suit)
|
||||
cards.select { |card| card.include?(suit) }
|
||||
end
|
||||
|
||||
cards = [
|
||||
"7 of hearts",
|
||||
"king of clubs",
|
||||
"10 of hearts",
|
||||
"ace of spades"
|
||||
]
|
||||
|
||||
puts cards_of_suit(cards, "hearts")
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
class Card
|
||||
attr_reader :value, :suit
|
||||
|
||||
def initialize(value, suit)
|
||||
@value = value
|
||||
@suit = suit
|
||||
end
|
||||
end
|
||||
|
||||
def has_suit?(hand, suit)
|
||||
hand.any? { |card| card.suit == suit }
|
||||
end
|
||||
|
||||
def valid_follow_card?(hand, lead_card, follow_card)
|
||||
if follow_card.suit == lead_card.suit
|
||||
true
|
||||
elsif has_suit?(hand, lead_card.suit)
|
||||
false
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
hand = [
|
||||
Card.new("9", "hearts"),
|
||||
Card.new("jack", "clubs"),
|
||||
Card.new("7", "spades")
|
||||
]
|
||||
|
||||
lead_card = Card.new("king", "hearts")
|
||||
|
||||
puts valid_follow_card?(hand, lead_card, hand[0]) # true
|
||||
puts valid_follow_card?(hand, lead_card, hand[1]) # false
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
class Gecko
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@meals = []
|
||||
end
|
||||
|
||||
def eat(prey)
|
||||
@meals << prey
|
||||
end
|
||||
|
||||
def display_meals
|
||||
puts "#{@name} a mangé :"
|
||||
|
||||
@meals.each do |prey|
|
||||
puts "- #{prey}"
|
||||
end
|
||||
|
||||
puts
|
||||
suffix = @meals.length == 1 ? "proie" : "proies"
|
||||
puts "Total : #{@meals.length} #{suffix}"
|
||||
end
|
||||
end
|
||||
|
||||
delta = Gecko.new("Delta")
|
||||
|
||||
delta.eat("dubia")
|
||||
delta.eat("ver de farine")
|
||||
delta.eat("cloporte")
|
||||
|
||||
delta.display_meals
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
class Gecko
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@meals = []
|
||||
end
|
||||
|
||||
def eat(prey, quantity)
|
||||
@meals << [prey, quantity]
|
||||
end
|
||||
|
||||
def display_meals
|
||||
puts "#{@name} a mangé :"
|
||||
|
||||
total = 0
|
||||
|
||||
@meals.each do |prey, quantity|
|
||||
total += quantity
|
||||
puts "- #{quantity} #{prey}"
|
||||
end
|
||||
|
||||
puts
|
||||
puts "Total : #{total} proies"
|
||||
end
|
||||
end
|
||||
|
||||
delta = Gecko.new("Delta")
|
||||
|
||||
delta.eat("dubia", 2)
|
||||
delta.eat("ver de farine", 3)
|
||||
delta.eat("cloporte", 1)
|
||||
|
||||
delta.display_meals
|
||||
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
class Gecko
|
||||
attr_reader :name
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@meals = []
|
||||
end
|
||||
|
||||
def eat(prey, quantity, calcium)
|
||||
return false unless prey.is_a?(String)
|
||||
|
||||
prey = prey.strip
|
||||
|
||||
return false if prey.empty?
|
||||
return false unless quantity.is_a?(Integer) && quantity.positive?
|
||||
return false unless calcium == true || calcium == false
|
||||
|
||||
@meals << [prey, quantity, calcium]
|
||||
true
|
||||
end
|
||||
|
||||
def display_meals
|
||||
puts "#{@name} a mangé :"
|
||||
|
||||
@meals.each do |prey, quantity, calcium|
|
||||
suffix = calcium ? " (calcium)" : ""
|
||||
puts "- #{quantity} #{prey}#{suffix}"
|
||||
end
|
||||
|
||||
puts
|
||||
puts "Total : #{total_prey} proies"
|
||||
end
|
||||
|
||||
def display_last_meal
|
||||
puts
|
||||
|
||||
if @meals.empty?
|
||||
puts "Aucun repas enregistré."
|
||||
return
|
||||
end
|
||||
|
||||
puts "Dernier repas :"
|
||||
|
||||
prey, quantity, calcium = @meals.last
|
||||
suffix = calcium ? " (calcium)" : ""
|
||||
puts "- #{quantity} #{prey}#{suffix}"
|
||||
end
|
||||
|
||||
def meal_count
|
||||
@meals.length
|
||||
end
|
||||
|
||||
def total_prey
|
||||
total = 0
|
||||
|
||||
@meals.each do |_prey, quantity, _calcium|
|
||||
total += quantity
|
||||
end
|
||||
|
||||
total
|
||||
end
|
||||
|
||||
def ate?(searched_prey)
|
||||
@meals.any? do |prey, _quantity, _calcium|
|
||||
prey == searched_prey
|
||||
end
|
||||
end
|
||||
|
||||
def calcium_meal_count
|
||||
@meals.count do |_prey, _quantity, calcium|
|
||||
calcium
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
delta = Gecko.new("Delta")
|
||||
|
||||
delta.eat("dubia", 2, true)
|
||||
delta.eat("ver de farine", 3, false)
|
||||
delta.eat("cloporte", 1, true)
|
||||
|
||||
delta.display_meals
|
||||
delta.display_last_meal
|
||||
|
||||
puts
|
||||
puts "Delta a fait #{delta.meal_count} repas."
|
||||
puts "Delta a mangé #{delta.total_prey} proies."
|
||||
|
||||
puts
|
||||
puts delta.ate?("dubia")
|
||||
|
||||
puts
|
||||
puts delta.calcium_meal_count
|
||||
|
||||
puts
|
||||
puts delta.name
|
||||
|
||||
puts
|
||||
puts delta.eat("dubia", -2, true) # false
|
||||
puts delta.eat("dubia", 2, true) # true
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
class Hello
|
||||
def hello(name)
|
||||
puts "Hello #{name}!"
|
||||
end
|
||||
|
||||
def hello_everybody(names)
|
||||
names.each do |name|
|
||||
hello(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
greeter = Hello.new
|
||||
|
||||
greeter.hello("Biblo")
|
||||
|
||||
puts
|
||||
|
||||
greeter.hello_everybody(["Pipin", "Merry", "Sam"])
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Règles :
|
||||
# - le mot choisi doit être présent dans le tableau ;
|
||||
# - s'il existe un mot de 5 lettres ou plus, le choix doit faire au moins 5 lettres ;
|
||||
# - sinon, s'il existe un mot commençant par "a", le choix doit commencer par "a" ;
|
||||
# - sinon, n'importe quel mot présent dans le tableau est accepté.
|
||||
|
||||
def valid_word?(words, chosen_word)
|
||||
return false unless words.include?(chosen_word)
|
||||
|
||||
if words.any? { |word| word.length >= 5 }
|
||||
chosen_word.length >= 5
|
||||
elsif words.any? { |word| word.start_with?("a") }
|
||||
chosen_word.start_with?("a")
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
words = ["chat", "arbre", "chien", "rat"]
|
||||
chosen_word = "rat"
|
||||
|
||||
puts valid_word?(words, chosen_word)
|
||||
Reference in new issue
Block a user