Skip to content

Instantly share code, notes, and snippets.

View adrimolano's full-sized avatar

Adriana Molano adrimolano

  • Delmar International Inc.
  • Montreal
View GitHub Profile

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.

@adrimolano
adrimolano / gist:b7db8bc1d73c98d6f08eb6ddccaf08d3
Created April 29, 2016 17:26 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
require 'active_support/all'
@candidates = [
{
id: 1,
years_of_experience: 4,
github_points: 293,
languages: ['C', 'Ruby', 'Python', 'Clojure'],
date_applied: 5.days.ago.to_date,
age: 26
@adrimolano
adrimolano / regular_expressions.rb
Last active April 28, 2016 22:24 — forked from davidvandusen/regular_expressions.rb
regular_expressions.rb
# 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)
if string =~ /(\d{3})-(\d{3})-(\d{3})/
true
else
false
end
end
def benchmark
beginning_time = Time.now
yield
end_time = Time.now
running_time = end_time - beginning_time
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[:IL] = "Illinois"
@states[:NJ] = "New Jersey"
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.values[0]
puts list[:yvr]
def shakil_the_dog
puts "Shakil the Dog is waiting for you. "
puts "-- Type 'woof' to bark and pretend to be a dog."
puts "-- Type 'Shakil STOP!' to make him stop."
puts "-- Type 'meow' to pretend to be a cat."
puts "-- Type whaetver with 'treat' on it."
puts "-- Type 'go away' to make him leave."
loop do
you_say = gets.chomp
you_say.downcase!
# Find the maximum
def maximum(arr)
arr.max
end
# expect it to return 42 below
result = maximum([2, 42, 22, 02])
puts "max of 2, 42, 22, 02 is: #{result}"
# expect it to return nil when empty array is passed in