This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def palindrome? string | |
string.gsub(/\W/,"").downcase.reverse == string.gsub(/\W/,"").downcase | |
end | |
def count_words string | |
result = {} | |
string.downcase.split(/\W+/).each do |word| | |
result[word] = result[word] ? result[word] + 1 : 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WrongNumberOfPlayersError < StandardError ; end | |
class NoSuchStrategyError < StandardError ; end | |
def rps_result( first_strategy, second_strategy ) | |
worst_than = { "r"=>"s", "s"=>"p", "p"=>"r" } # Hash of rock/paper/scissors rules | |
worst_than[second_strategy[1].downcase] == first_strategy[1].downcase ? second_strategy : first_strategy | |
end | |
def rps_game_winner( game ) | |
raise WrongNumberOfPlayersError unless game.length == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::Base | |
def method_missing(meth, *args, &block) | |
if meth.to_s =~ /^find_by_(.+)$/ | |
run_find_by_method($1, *args, &block) | |
else | |
super # You *must* call super if you don't handle the | |
# method, otherwise you'll mess up Ruby's method | |
# lookup. | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Numeric | |
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1.0} | |
def method_missing( method_id, *args, &block ) | |
singular_ccy = method_id.to_s.gsub( /s$/, '') | |
if @@currencies.has_key?( singular_ccy ) | |
self * @@currencies[singular_ccy] # Convert into dollars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def method_missing( method, *args, &block ) | |
method.to_s == "palindrome?" ? self.gsub(/\W/,"").downcase.reverse == self.gsub(/\W/,"").downcase : super | |
end | |
end | |
module Enumerable | |
def method_missing( method, *args, &block ) | |
method.to_s == "palindrome?" ? self.to_a.reverse == self.to_a : super | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Class | |
def attr_accessor_with_history attr_name | |
attr_name = attr_name.to_s # make sure it's a string | |
attr_reader attr_name # create the attribute's getter | |
attr_reader attr_name+"_history" # create bar_history getter | |
class_eval %Q{ | |
def #{attr_name}=(value) | |
@#{attr_name}_history.nil? ? @#{attr_name}_history = [#{attr_name}, value] : @#{attr_name}_history << value | |
@#{attr_name} = value | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dessert | |
attr_accessor :name, :calories | |
def initialize(name, calories) | |
@name, @calories = name, calories | |
end | |
def healthy? | |
@calories < 200 | |
end | |
def delicious? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def combine_anagrams words | |
hash = Hash.new | |
words.each do |word| | |
sorted_word = word.downcase.chars.sort | |
hash.has_key?(sorted_word) ? hash[sorted_word].push(word) : hash[sorted_word] = [word] | |
end | |
hash.values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CartesianProduct | |
include Enumerable | |
def initialize a,b | |
@a,@b = a,b | |
end | |
def each | |
@a.each do |a_element| | |
@b.each do |b_element| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Model | |
class User < ActiveRecord::Base | |
has_many :followings | |
def follow(user) | |
self.followings.create(:followed_user => user) unless self.followings.where(:followed_user_id => user.id).present? | |
end | |
end | |
# Controller |
OlderNewer