puts "#{self}!!"; end end end Module.new { using Foo }.module_eval { 'hello'.say } #=> hello!! 'world'.say #=> NoMethodError: undefined method `say' for "world":String
nil, *rest, &block) if block col, op, val = Module.new { using ActiveRecord::Refinements::WhereBlockSyntax }.module_eval &block arel_node = case op when :!= table[col].not_eq val when :=~ table[col].matches val when :> table[col].gt val when ... # (snip) end clone.tap do |relation| relation.where_values += build_where(arel_node) end else super ennnnd
{ User.where { :name != 'nobu' }.to_sql } it { should =~ /WHERE \("users"."name" != 'nobu'\)/ } end describe '#>=' do subject { User.where { :age >= 18 }.to_sql } it { should =~ /WHERE \("users"."age" >= 18\)/ } end describe '#=~' do subject { User.where { :name =~ 'tender%' }.to_sql } it { should =~ /WHERE \("users"."name" LIKE 'tender%'\)/ } end context 'outside of where block' do it { expect { :omg > 1 }.to raise_error ArgumentError } end end
plus #{other}" end end end class MyArray def initialize @ary = ['foo', 'bar', 'baz'] end def inject(accum, &block) @ary.each do |str| accum = WeirdPlus.module_exec(str, accum, &block) end accum end end def add_all(str_ary) str_ary.inject('') do |str, accum| accum + str end end add_all(MyArray.new) #=> " plus foo plus bar plus baz"
at top level. refined methods are called only after "using". or within blocks given to "refine". if you pass the proc to "refine" e.g. refine(C,&b) refined methods may not be called from b. It's implementation dependent. refinements are not available in subclasses, nor in reopened classes/modules. refinements are not available from module_eval/class_eval.
{:adapter => 'sqlite3', :database => ':memory:'}} ActiveRecord::Base.establish_connection('test') class User < ActiveRecord::Base validates_presence_of :name end class CreateAllTables < ActiveRecord::Migration def self.up create_table(:users) {|t| t.column :name, :string} end end CreateAllTables.up # p User.new.save_without_validation p User.new.save_without_dirty p User.new.save
end module Bar def foo super puts 'bar' end end module Baz def foo super puts 'baz' end end class A prepend Bar prepend Baz end A.new.foo #=> foo bar baz