Skip to content

Commit

Permalink
Allow lambda procs with 0 arity as virtual row blocks on ruby 1.9
Browse files Browse the repository at this point in the history
Previously, such blocks did not work, because the blocks would be
instance_evaled, which passes an argument, and lambda procs with
0 arity would raise an error in that circumstance on ruby 1.9+.

Now that Sequel no longer supports ruby 1.8.6, switch from
instance_eval to instance_exec, making such procs work.

For compatibility with ruby 1.8.7, make Sequel::BasicObject
respond to instance_exec on ruby <1.9.
  • Loading branch information
jeremyevans committed Jan 8, 2013
1 parent 08a27f6 commit 1842d05
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD

* Allow lambda procs with 0 arity as virtual row blocks on ruby 1.9 (jeremyevans)

* Handle schema-qualified row_types in the pg_array integration in the pg_row extension (jeremyevans) (#595)

* Support default_schema when reseting primary key sequences on PostgreSQL (jeremyevans) (#596)
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/core.rb
Expand Up @@ -390,7 +390,7 @@ def self.virtual_row(&block)
vr = SQL::VirtualRow.new
case block.arity
when -1, 0
vr.instance_eval(&block)
vr.instance_exec(&block)
else
block.call(vr)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/sql.rb
Expand Up @@ -6,7 +6,7 @@ module Sequel
class BasicObject
# The instance methods to not remove from the class when removing
# other methods.
KEEP_METHODS = %w"__id__ __send__ __metaclass__ instance_eval == equal? initialize method_missing"
KEEP_METHODS = %w"__id__ __send__ __metaclass__ instance_eval instance_exec == equal? initialize method_missing"

# Remove all but the most basic instance methods from the class. A separate
# method so that it can be called again if necessary if you load libraries
Expand Down
5 changes: 5 additions & 0 deletions spec/core/expression_filters_spec.rb
Expand Up @@ -594,6 +594,11 @@ def l(arg, should)
l(Sequel.expr(proc{|v| @o}) + 1, "(foo + 1)")
end

it "Sequel.expr should handle lambda proc virtual rows" do
l(Sequel.expr(&lambda{1}), "1")
l(Sequel.expr(&lambda{|| 1}), "1")
end

it "Sequel.expr should raise an error if given an argument and a block" do
proc{Sequel.expr(nil){}}.should raise_error(Sequel::Error)
end
Expand Down

0 comments on commit 1842d05

Please sign in to comment.