Skip to content

Instantly share code, notes, and snippets.

View gbrl's full-sized avatar

Gabriel Rambert gbrl

  • Earth
View GitHub Profile
@gbrl
gbrl / Gemfile
Created October 15, 2016 05:07 — forked from slothelle/Gemfile
Deploying Rails 4 apps with Resque and Redis to Heroku using Unicorn with a Procfile.
# and whatever other gems you need
gem 'resque', '~> 1.24.1'
gem 'unicorn', '~> 4.6.2'

Classical Inheritance in Ruby

For programmers who are new to Object Oriented Programming, the ideas behind classical inheritance can take some getting used to. This article walks you through the syntax of defining class inheritance in Ruby with explanations of each OOP feature along the way. You will have created many inheriting classes and used them in Ruby code by the end of this exercise.

Create a new Ruby code file and copy the code examples into it as you go. Run the file with the provided driver code and read the output. Try writing your own driver code to try things out new concepts as they're introduced.

require 'pp'
# 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)
!!(string =~ /[ ]\d{3}-\d{3}-\d{3}$/)
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
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 benchmark
start = Time.now
yield
running_time = Time.now - start
end
# Be careful, pasting this into IRB will take a long time to print.
# It's a loooong string. :)
long_string = "apple"*100000000
@states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}
@states[:OH] = 'Ohio'
@states[:NC] = 'North Carolina'
@gbrl
gbrl / sort.rb
Last active April 26, 2016 22:19 — forked from davidvandusen/sort.rb
# Sort the array from lowest to highest
def sort(arr)
a_length = arr.length
return arr if a_length == 1
return [] if a_length == 0
keep_going = true
while keep_going do
flagged = false
arr.each_with_index do |num,index|
unless (index + 1 == arr.length ) # Can't compare last number with next one since it doesn't exist.
@gbrl
gbrl / renter.rb
Last active April 26, 2016 23:32 — forked from davidvandusen/renter.rb
# must be baller and either furnished or rent cheaper than 2100
def rent?(furnished, rent, baller)
puts "Furnished is #{furnished}, rent is #{rent}, baller status is #{baller}"
if baller && (furnished || rent < 2100)
puts "Yeah, let's do it. \n"
return true
else
puts "Nah, I don't want it. \n"
return false
end