Skip to content

Instantly share code, notes, and snippets.

View jannatshah's full-sized avatar

Jannat Shah jannatshah

View GitHub Profile
require "date"
# def name_of_method(parameters)
def greeting(name)
return "Hello, #{name}"
end
# call the method giving it an argument
# puts greeting("Arthur")
# 1. pick a price between 1 and 10
number = rand(1..10)
# number = array.sample
puts number
guess = 0
# LOOP HERE (stop if its correct guess)
until guess == number
# ask the user for his guess
puts "Pick a number from 1 to 10"
require "date"
def days_until_xmas()
# PSEUDO CODE
# 1. require date
# 2. set a variable with today's date
todays_date = Date.today
# 3. set another variable with xmas date (of this year)
xmas_date = Date.new(todays_date.year, 12, 25)
if todays_date > xmas_date
@marcoranieri
marcoranieri / array.rb
Created October 9, 2019 10:32
Flow & Array
beatles = ["john", "ringo", "seb"]
#index 0 1 2 (length - 1)
#index -3 -2 -1
p beatles # puts beatles.inspect
# C R U D
# Create
my_array = ["Ben", "Phelim", "Arthur"]
# indexes 0 1 2
my_array.length # return the number of element in the array
# CRUD
# Read
my_array[0] # first element of array
def acronymize(string)
string.split(" ").map { |word| word[0].upcase }.join
end
cities = ["london", "paris", "berlin"]
# 0 1 2
# CREATE
# cities.push("olso")
cities << "madrid"
# READ
# p cities.last
p cities[3]
def frequencies(text)
# intro downcase the string, and create an empty hash
frequency = {}
text = text.downcase
# 1. split the string into an array of words
words = text.split
# words => ["the", "lazy", ...]
# 2. iterate over the array
words.each do |word|
@marcoranieri
marcoranieri / api_demo.rb
Created October 15, 2019 10:13
parsing&storing
require 'json'
require 'open-uri'
puts "Give username:"
print "> "
username = gets.chomp
# TODO - Let's fetch name and bio from a given GitHub username
url = "https://api.github.com/users/#{username}"
@marcoranieri
marcoranieri / interface.rb
Created October 15, 2019 16:46
IMDB_Scraper
require "yaml"
require_relative "scraper"
# fetch array of urls
puts "Fetching URLs"
urls = fetch_movie_urls
# return an array of hashes of movie details
movies = urls.map do |url|
puts "Scraping #{url}"