This file contains 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
def timer | |
start_time = Time.now | |
puts 'starting a long algorithm....' | |
yield | |
puts 'process done!' | |
end_time = Time.now | |
return end_time - start_time | |
end | |
# timer do |
This file contains 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
musicians = ['David Gilmour', 'Roger Waters', 'Richard Wright', 'Nick Mason'] | |
upcased_first_names = musicians.map do |musician| | |
first_name = musician.split.first | |
upcased_first_name = first_name.upcase | |
puts "[DEBUG] #{musician}'s first name is #{upcased_first_name}" | |
upcased_first_name | |
end | |
p upcased_first_names |
This file contains 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
musicians = ['David Gilmour', 'Roger Waters', 'Richard Wright', 'Nick Mason'] | |
# EACH | |
musicians.each do |musician| | |
puts "Hello #{musician}" | |
end | |
puts '-------------' | |
# EACH WITH INDEX | |
# musicians.each do |musician| |
This file contains 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
# generer un nombre aleatoire entre 1 et 100 | |
price = rand(1..100) | |
# demander un nombre a l utilisateur | |
# comparer les 2 nombres | |
guess = 0 | |
until price == guess | |
puts "What's your guess?" | |
guess = gets.chomp.to_i | |
# si bonne reponse: indiquer que c'est gagné | |
# si mauvaise reponse |
This file contains 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
# TODO: Implement a method which returns | |
# the number of days until next Xmas | |
require 'date' | |
def compute_result(year, today) | |
xmas = Date.new(year,12,25) | |
xmas - today | |
end | |
def days_until_xmas |
This file contains 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
# interface.rb | |
# Pseudo-code: | |
# 1. Print welcome and the horses names | |
# display a welcome message | |
puts "Welcome to the horse race!" | |
# store horses names in an array | |
horses = [ | |
'Grand Cardinal', | |
'Jolly Jumper', |
This file contains 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
require_relative 'calculator' | |
# Calculator (addition) | |
# | |
# ask user to enter a number | |
puts "First number?" | |
# store first number | |
first_number = gets.chomp | |
# check first number class | |
first_number = checker_to_f(first_number) | |
# ask user for operation |
This file contains 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
def calculator(number_one, number_two, operation) | |
# check which operator | |
case operation | |
when '+' | |
# compute the right operation | |
result = number_one + number_two | |
when '-' | |
result = number_one - number_two | |
when '*' | |
result = number_one * number_two |
This file contains 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
# compute computer choice | |
random_price = (1..100).to_a.sample | |
guess_price = 0 # initialization | |
until guess_price == random_price | |
# ask user input and put into an integer | |
puts " What price is the object?" | |
guess_price = gets.chomp.to_i | |
# compare the 2 entries | |
if guess_price == random_price |
This file contains 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
require 'date' | |
def compute_diff(date_today, year) | |
date_christmas = Date.new(year, 12, 25) | |
return (date_christmas - date_today).to_i | |
end | |
# puts compute_diff(Date.today, 2019) | |
def days_to_xmas |