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 User | |
include Mongoid::Document | |
include Mongoid::Timestamps::Created | |
field :language | |
has_many :profiles | |
scope :english, ->() do | |
where(:language => "english") |
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
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) |
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
#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); |
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
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 |
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
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 | |
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
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 |
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
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 |
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
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` |
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
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] |
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 Array | |
module ToProc | |
def to_proc(*args) | |
lambda do |*args| | |
object = args.shift | |
each { |message| object = object.__send__ message } | |
object | |
end |