This file contains hidden or 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
# alternative to what is explained in the article Ruby Blocks as Dynamic Callbacks: | |
# http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks | |
class Twitter | |
def tweet(msg, &block) | |
proxy = DSL[block] | |
publish(msg) | |
proxy.respond_with(:success) | |
rescue => e | |
proxy.respond_with(:failure, e.message) |
This file contains hidden or 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
ary = [1,1,1,1,2,3,4,5,5,5,5,5,5,10,10,100] | |
# approach 1 | |
ary.enum_for(:sort).with_object([]) { |(a,b), result| result << a if a == b; a <=> b}.uniq | |
# approach 2 | |
ary.select { |e| ary.count(e) > 1 }.uniq | |
# approach 3 (from @apeiros) | |
ary.group_by {|e| e}.select { |k, v| v.size > 1 }.keys |
This file contains hidden or 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
ruby-1.9.2-p290 :001 > class A | |
ruby-1.9.2-p290 :002?> end | |
=> nil | |
ruby-1.9.2-p290 :003 > A.ancestors | |
=> [A, Object, PP::ObjectMixin, Kernel, BasicObject] | |
ruby-1.9.2-p290 :004 > module M | |
ruby-1.9.2-p290 :005?> end | |
=> nil | |
ruby-1.9.2-p290 :006 > module Kernel | |
ruby-1.9.2-p290 :007?> include M |
This file contains hidden or 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
# First the end result of what we want: | |
class Foo | |
before_hook :whoa | |
before_hook :amazing | |
def test | |
puts "This is kinda cool!" | |
end |
This file contains hidden or 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
ruby-1.9.2-head :001 > h = Hash[*1..10] | |
=> {1=>2, 3=>4, 5=>6, 7=>8, 9=>10} | |
ruby-1.9.2-head :002 > h.select {|k,v| k == 5} | |
=> {5=>6} | |
ruby-1.9.2-head :003 > |
This file contains hidden or 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
module M | |
@lock = Mutex.new | |
class << self | |
attr_reader :lock | |
end | |
def lock | |
M.lock | |
end |
This file contains hidden or 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
# see this blog post for differences between 1.8.7 vs 1.9.1 vs. 1.9.2: http://jfire.posterous.com/constant-lookup-in-ruby | |
module M | |
X = 100 | |
def m1 | |
puts X | |
end | |
end |
This file contains hidden or 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 UserProject < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :project | |
belongs_to :creator, :class => "User" | |
# add whatever else you want here, this is your join model | |
end | |
class User < ActiveRecord::Base | |
has_many :user_projects | |
has_many :projects, :through => :user_projects |
This file contains hidden or 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
dict = {} | |
'aardvark'.chars.inject(dict) { |dict,c| dict[c] = {} } | |
def find(key, dict) | |
current_dict = dict | |
key.chars.each do |c| | |
return nil unless current_dict.has_key?(c) | |
current_dict = current_dict[c] | |
end | |
end |
This file contains hidden or 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
module M | |
def m1 | |
puts "hi from M" | |
end | |
def handle(&block) | |
block.call | |
end | |
def handle2(&block) |