Skip to content

Instantly share code, notes, and snippets.

class Vehicle
def initialize(args)
@color = args[:color]
@wheels = args[:wheels]
@gas_mileage = [true,false]
end
def drive
@status = :driving
end
def brake
@abrahamsangha
abrahamsangha / array_mode.rb
Created June 7, 2013 20:03
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values. If there's only one most-frequent value, it returns a single-element Array.
def mode(array)
hash = Hash.new(0)
array.each { |elem| hash[elem] += 1 }
hash.keep_if { |k, v| v == hash.values.max }.keys
end
@abrahamsangha
abrahamsangha / method_missing.rb
Last active December 18, 2015 03:19
The instructions were to fix this bad code with a method_missing method.
class Person
def initialize(name, age, incoming_race)
@name = name
@age = age
self.race = incoming_race
end
def nam
@name.split.map(&:capitalize).join(" ")
end

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
@abrahamsangha
abrahamsangha / fizzblam.rb
Last active December 18, 2015 02:09
The challenge was to create a method that returned an array of numbers from 1 to 1000 but with each number that included 5 as a factor replaced with "Fizz", each number that included 7 as a factor replaced with "Blam" and each number that included both 5 and 7 as factors replaced with "FizzBlam". The additional challenge was to do it in one line…
def fizzblam
arr = (1..1000).to_a.map { |elem| [("Fizz" if (elem % 5).zero?), ("Buzz" if (elem % 7).zero?), (elem unless ((elem % 5).zero?) || ((elem % 7).zero?))].compact.join }
end
#another way to do it using string interpolation
def fizzblam
arr = (1..1000).to_a.map { |elem| "#{'Fizz' if elem % 3 == 0}#{'Buzz' if elem % 5 == 0}#{elem unless elem % 3 == 0 || elem % 5 == 0}"
end
def addition item
@list[item] ? @list[item] += 1 : @list[item] = 1
@list
end
@abrahamsangha
abrahamsangha / reverse_words
Created May 9, 2013 12:51
a method reverse_words which takes a sentence as a string and reverse each word in it.
def reverse_words(str)
str.split.each { |word| word.reverse!}.join(' ')
end
@abrahamsangha
abrahamsangha / separate_comma
Created May 9, 2013 12:39
method separate_comma which takes an integer as its input and returns a comma-separated integer as a string
def separate_comma(number)
number.to_s.reverse.gsub(/(\d{3})(?=\d)(?!\d*\.)/, '\1,').reverse
end
@abrahamsangha
abrahamsangha / regexpssn
Created May 9, 2013 11:50
Using Regexp to find Social Security Numbers
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
string.match(/\d{3}\-\d\d\-\d{4}/)
end
# Return the Social Security number from a string.
def grab_ssn(string)
string[/\d{3}\-\d\d\-\d{4}/]
end
@abrahamsangha
abrahamsangha / Die
Created May 9, 2013 11:46
Implement a basic Die class which can be initialized with some number of sides. We can then roll the die, returning a random number.
class Die
def initialize(sides)
raise ArgumentError.new("Need at least one side") unless sides >= 1
@sides = sides
end
def sides
@sides
end