Skip to content

Instantly share code, notes, and snippets.

@Papillard
Papillard / interface.rb
Created October 24, 2017 17:05
Final Wishly - Reboot Batch #100
require_relative "utilities"
require_relative "scraping"
puts "Welcome on Wishly!"
wishlist = [{name: "Macbook", checked: true}, {name: "Iphone", checked: false}]
while true
display_menu
action = gets.chomp
@Papillard
Papillard / esty_scraper.rb
Last active January 11, 2018 10:42
Scraping Etsy - Reboot batch #100
require 'open-uri'
require 'nokogiri'
puts "Quelle catégorie t'intéresse?"
category = gets.chomp
url = "https://www.etsy.com/search?q=#{category}"
file = open(url)
html_text = file.read
@Papillard
Papillard / interface.rb
Last active October 23, 2017 16:02
InstaCart - Reboot batch #100
cart = {}
items = {
"pomme" => {price: 3.5, stock: 100},
"orange" => {price: 1.5, stock: 20},
"quinoa" => {price: 5.0, stock: 30},
"pain" => {price: 1.2, stock: 10}
}
# Boucle de prise de commande
@Papillard
Papillard / interface.rb
Created October 23, 2017 10:54
PMU - Reboot batch #100
puts "Bonjour, bienvenue à Longchamp"
horses = ["Canaçon", "Sonic", "Belle des vents", "Bad boy"]
while true
puts "Voici les chevaux sur lesquels tu peux parier"
horses.each_with_index do |horse, index|
puts "Cheval numéro #{index + 1} - #{horse}"
@Papillard
Papillard / calculator.rb
Last active October 23, 2017 11:16
Calculator - Reboot batch #100
def calculate(first, second, operation)
if operation == "+"
result = first + second
elsif operation == "-"
result = first - second
elsif operation == "*"
result = first * second
elsif operation == "/"
result = first / second
else
@Papillard
Papillard / scraping_kitchenstories.rb
Created July 18, 2017 08:33
Scraping recipes from Kitchen Stories
require 'open-uri'
require 'nokogiri'
# Let's scrape recipes from https://kitchenstories.io/en/search?search=strawberry
puts "What's ingredient do you want recipes for?"
ingredient = gets.chomp
url = "https://kitchenstories.io/en/search?search=#{ingredient}"
# Get the HTML file from the URL
@Papillard
Papillard / regexp.rb
Created July 17, 2017 08:29
Playing with Regexp in ruby
# Validate data with =~ (to check it matches a regexp)
welcome = "hello guys!"
if text =~ /l{3}/
puts "This is nice welcome message"
end
email = "[email protected]"
if email =~ /^\w+@\w+\.\w+$/
@Papillard
Papillard / blocks.rb
Created July 13, 2017 09:10
Playing with blocks
def greet(first_name, last_name)
full_name = "#{first_name.capitalize} #{last_name.capitalize}"
yield(full_name)
end
greet("alice", "clavel") do |name|
puts "Hallo freuilein #{name}"
end
greet("alice", "clavel") do |name|
@Papillard
Papillard / iterators.rb
Created July 13, 2017 09:08
Playing with iterators
musicians = ["Jimi Hendrix", "Bob Dylan", "Michael Jackson", "Barry White", "Justin Bieber"]
# each: to iterate on elemens
musicians.each do |musician|
puts "Hello #{musician}"
end
# each_with_index: iterate on element + index
musicians.each_with_index do |musician, index|
puts "Musician N#{index} - #{musician}"
@Papillard
Papillard / looping_basics.rb
Created July 12, 2017 08:48
Basic loops with while and for
# While loop example
price = rand(1..5)
choice = nil
while choice != price
puts "What's your guess?"
choice = gets.chomp.to_i
end
puts "You won"