Skip to content

Instantly share code, notes, and snippets.

View jasiek's full-sized avatar

Jan Szumiec jasiek

View GitHub Profile
diff --git a/app/models/sample.rb b/app/models/sample.rb
new file mode 100644
index 0000000..7d523a9
--- /dev/null
+++ b/app/models/sample.rb
@@ -0,0 +1,3 @@
+class Sample < ActiveRecord::Base
+ validates_presence_of :value
+end
diff --git a/db/migrate/20090522160717_create_samples.rb b/db/migrate/20090522160717_create_samples.rb
@jasiek
jasiek / gist:1295394
Created October 18, 2011 13:19
Ruby pre-initialized struct
class Basket < Struct.new(:entries)
def self.new(*args)
b = super(*args)
b.entries ||= []
b
end
def total
entries.inject(0.0) do |sum, entry|
(?<group-name>regular-expression){0}
\g<group-name>
(?<ltr>[ABCEGHJKLMNPRSTVXYWZ]){0}
(?<num>[0-9]){0}
(?<fsa>\g<ltr>\g<num>\g<ltr>){0}
(?<ldu>\g<num>\g<ltr>\g<num>){0}
(?<cpc>\g<fsa> \g<ldu>){0}
^\d{5}|\g<cpc>$
ruby-1.9.3-p0 :001 > def f(x, y=1)
ruby-1.9.3-p0 :002?> method(__method__).parameters
ruby-1.9.3-p0 :003?> end
=> nil
ruby-1.9.3-p0 :004 > f(1, 2)
=> [[:req, :x], [:opt, :y]]
module M
def hello
1
end
end
module N
include M
@jasiek
jasiek / gist:1711082
Created January 31, 2012 15:28 — forked from LTe/gist:1710563
module M
def method
puts "hello"
end
end
def execute(current_module, &block)
Object.new.tap do |o|
o.extend(current_module)
o.instance_eval &block
p = lambda { |x|
x * 2
}
p === 7 #=> 14
class Deferrable < SimpleDelegator
# Public: Create a side thread that wraps whatever you need to have available in the future.
def initialize(&blk)
super(Thread.new(&blk))
end
# Internal: We need the result of the computation of this thread NOW, so block on it and return
# the value.
def __getobj__