Skip to content

Instantly share code, notes, and snippets.

View laurenachoo's full-sized avatar

Lauren laurenachoo

  • Vancouver, BC Canada
View GitHub Profile
class Animal
def initialize(name, food)
@name = name
@legs = 4
@food = food
end
def breathe
puts "This animal breathes."
end
@laurenachoo
laurenachoo / hamlet_lines.rb
Created May 4, 2015 20:42
File I/O Exercise
require 'open-uri'
url = "http://ruby.bastardsbook.com/files/fundamentals/hamlet.txt"
local_fname = "hamlet.txt"
File.open(local_fname, "w") {|file| file.write(open(url).read)}
File.open(local_fname, "r") do |file|
file.readlines.each_with_index do |line, idx|
puts line if idx % 42 == 41
end
class BottleConsumer
attr_accessor :bottles, :caps, :dollars, :empty, :bottles_returned, :caps_returned
def initialize(dollars)
@bottles = 0
@caps = 0
@empty = 0
@dollars = dollars
@caps_returned = 0
@bottles_returned = 0
@laurenachoo
laurenachoo / candidates.rb
Last active August 29, 2015 14:20
Candidates
require 'active_support/all'
@candidates = [
{
id: 5,
years_of_experience: 4,
github_points: 293,
languages: ['C', 'Ruby', 'Python', 'Clojure'],
date_applied: 5.days.ago.to_date,
age: 26
# Determine whether a string contains a SIN (Social Insurance Number).
# A SIN is 9 digits and we are assuming that they must have dashes in them
def has_sin?(string)
!!/\b\d{3}-\d{3}-\d{3}\b/.match(string)
end
puts "has_sin? returns true if it has what looks like a SIN"
puts has_sin?("please don't share this: 234-604-142") == true
puts "has_sin? returns false if it doesn't have a SIN"
def benchmark
start_time = Time.now
yield
Time.now - start_time
end
long_string = "apple"*100000000
running_time = benchmark do
long_string.reverse
end
@states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}
@states.store("WV","Charleston")
@states.store("MN", "Augusta")
@states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}
@states.store("WV","Charleston")
@states.store("MN", "Augusta")
def count_letters(strings)
count = Hash.new(0)
strings.split('').each_with_index do |string, index|
count[string] = index
end
count
end
puts count_letters("lighthouse in the house...")
def count_letters(strings)
count = Hash.new(0)
strings.split('').each do |string|
count[string] += 1
end
count
end
puts count_letters("lighthouse in the house...")