Skip to content

Instantly share code, notes, and snippets.

Joshua-Strubs-MacBook-Pro:~ joshuastrub$ cd lighthouse
Joshua-Strubs-MacBook-Pro:lighthouse joshuastrub$ irb
irb(main):001:0> def say_hi(name)
irb(main):002:1> "Hi, #{name}"
irb(main):003:1> end
=> nil
irb(main):004:0> say_hi("Josh")
=> "Hi, Josh"
irb(main):005:0> my_array = [5, 3, 5]
=> [5, 3, 5]
# Sort the array from lowest to highest
def bubble_sort(array)
n = array.length
swapped = false
(n-1).times do |i|
if array[i] > array[i + 1]
array[i], array[i + 1] = array[i + 1], array[i]
swapped = true
end
def rent?(baller, furnished, rent)
baller && (furnished || rent < 2100)
end
puts rent?(true, true, 2200)
def fizzbuzz(x,y)
(x..y).each do |i|
case
when i % 15 == 0 then puts "FizzBuzz"
when i % 3 == 0 then puts "Fizz"
when i % 5 == 0 then puts "Buzz"
when i % 7 == 0 then puts "Fizzzzzzz"
else
end
end
list = {'yvr' => 'Vancouver', 'yba' => 'Banff', 'yyz' => 'Toronto', 'yxx' => 'Abbotsford', 'ybw' => 'Calgary'}
# Why is it returning nil instead of first element of the list above
puts list.first
list = {'yvr' => 'Vancouver', 'yba' => 'Banff', 'yyz' => 'Toronto', 'yxx' => 'Abbotsford', 'ybw' => 'Calgary'}
# Why is it returning nil instead of first element of the list above
puts list.first
@states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}
@states[:NC] = 'North Carolina'
@states[:CO] = 'Colorado'
# 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"
@joshuastr
joshuastr / candidates.rb
Created April 28, 2016 23:40
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
def count_letters(str)
data = {}
str.split('').each do |l|
puts l
next if l == ' '
if data[l].nil?
data[l] = 1
else
data[l] += 1
end