Skip to content

Instantly share code, notes, and snippets.

@Papillard
Papillard / deal_with_string.rb
Last active December 19, 2015 06:19
nice little functions to get familiar with string & regexp..
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
@Papillard
Papillard / rock_paper_scissors.rb
Last active December 19, 2015 06:19
rock! scissors! you loose fat buddy.
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
@Papillard
Papillard / method_missing.rb
Last active December 19, 2015 07:19
insightful method missing snippets... eg going into ActiveRecord machinery to understand "magical" find_by_whatever methods
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
@Papillard
Papillard / converter.rb
Last active December 19, 2015 07:29
Use method_missing hook to define custom methods on Numeric => ex: 3.rupees.in(:euros)
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
@Papillard
Papillard / meta_palindrome
Last active December 19, 2015 21:29
Extending palindrome? on String & Enumerable
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
@Papillard
Papillard / attr_history.rb
Last active December 19, 2015 21:29
Define method to keep track on attribute history
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
@Papillard
Papillard / OO_dessert.rb
Created July 17, 2013 14:06
simple taste of inheritance with desserts..
class Dessert
attr_accessor :name, :calories
def initialize(name, calories)
@name, @calories = name, calories
end
def healthy?
@calories < 200
end
def delicious?
@Papillard
Papillard / anagrams.rb
Created July 17, 2013 14:09
playing with anagrams..
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
@Papillard
Papillard / cartesian_product.rb
Created July 17, 2013 14:10
yielding cartesian product elements..
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|
@Papillard
Papillard / good_skinny_controller.rb
Created July 17, 2013 14:16
Refactor the code to delegate logic to the model
# 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