Skip to content

Instantly share code, notes, and snippets.

class User
include Mongoid::Document
include Mongoid::Timestamps::Created
field :language
has_many :profiles
scope :english, ->() do
where(:language => "english")
def create_proc(cofactor)
proc = proc { |arg| arg * cofactor }
end
proc1 = create_proc(3)
proc2 = create_proc(5)
proc1.call(2)
=> 6
proc2.call(2)
@DanielVartanov
DanielVartanov / sample.c
Created November 12, 2012 04:10
gcc sample.c -o sample -lunwind
#include <stdio.h>
#define UNW_LOCAL_ONLY
#include <libunwind.h>
int print_backtrace() {
unw_context_t unwind_context;
unw_cursor_t unwind_cursor;
unw_getcontext(&unwind_context);
describe FFI::Struct, ".layout" do
context 'when class name is blank' do
it 'should still work properly' do
test_struct = Class.new(FFI::Struct)
test_struct.layout :i, :int
s = test_struct.new
s[:i] = 0x12
LibTest.ptr_ret_int32_t(s, 0).should == 0x12
end
describe FFI::Struct, ".layout" do
context 'when derived class is not assigned to any constant' do
it 'should be able to use built-in types' do
end
end
context 'when derived class is assigned to a constant' do
it 'should be able to use built-in types' do
require 'mongoid'
class Item; include Mongoid::Document; end
def clean_object(obj)
obj.instance_variables.each do |ivar|
obj.send(:remove_instance_variable, ivar) unless ivar == :@attributes
end
end
A = Class.new
A.instance_eval { def defined_in_instance_eval; :instance_eval; end }
A.class_eval { def defined_in_class_eval; :class_eval; end }
A.new.defined_in_class_eval # => :class_eval
A.defined_in_instance_eval # => :instance_eval
@DanielVartanov
DanielVartanov / block_syntax.rb
Created August 3, 2013 05:43
This is a way of telling if a caller used `{ }` or `do end` to write a block.
def zig(*args)
puts 'You wrote `do end`' if block_given?
end
def zag
puts 'You wrote `{ }`' if block_given?
end
zig zag { } # => You wrote `{ }`
zig zag do end # => You wrote `do end`
M = Module.new
A = Class.new { include M }
M1 = Module.new
M.class_eval { include M1 }
# M1 is not in a list of ancestors (bug?!)
p A.ancestors # => [A, M, Object, Kernel, BasicObject]
class Array
module ToProc
def to_proc(*args)
lambda do |*args|
object = args.shift
each { |message| object = object.__send__ message }
object
end