Skip to content

Ruby Blender

  • Blog
  • About
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Tag Archive for 'binding'
19Aug09 Ruby Bindings and Scope
ruby
1 Comment

One of the features of Ruby’s Kernel module (which is imported into Object, so it’s available in every class) is the binding.  The binding allows you to keep a pointer to the scope at a particular location in your application and then later evaluate code from that location.

For example (and this came up as the result of a question on the ruby talk  mailing list), one of the things you can do is obtain a list of the local variables of a method:

def my_vars(a)
b=a
c=b+a
puts local_variables
end

 

returns the three variables, a, b, and c.  That’s fine, we’re still within the scope of the method.  But how about outside of our method?  This is where #binding and the Binding class come into play:

def outside(b)
puts eval("local_variables", b)
end

def my_vars(a)
b=a
c=b+a
outside(binding)
end

 

One thing to note: in order to work with the binding, we have to #eval code within its scope.  Thus the call to #eval with the method we are invoking and the binding.  This leads to the question from the mailing list:

we already have #has_block? to see if a block was passed. So how about
a #has_arguments? to query if _any_ arguments have been passed — Intransition

So how would we go about writing a #has_arguments? — here is a first stab:

class Object
def has_arguments?(b)
vars = eval("local_variables",b)
return false if vars.length == 0
vars.each do |v|
return false if eval("#{v}.nil?",b)
end
return true
end
end

class Foo
def initialize(first=nil)
if (has_arguments? binding)
puts "We're defined"
else
puts "Not!"
end
end
end

The only real drawback here is that we’ve got to pass in the binding itself.  Let’s try again:

class Object
def has_arguments?(&b)
vars = eval("local_variables",b.binding)
return false if vars.length == 0
vars.each do |v|
return false if eval("#{v}.nil?",b)
end
return true
end
end

class Foo
def initialize(first=nil)
if (has_arguments? {}) # note the empty block
puts "We're defined"
else
puts "Not!"
end
end
end

In this case, instead of explicitly calling #binding, we’re passing in an empty block — the block has it’s own scope, but it happens to contain the scope of then enclosing method.  It’s still not perfect, however:

  1. You need to pass in an empty block.
  2. It fails if you have local variables other than those passed in as arguments. (Pointed out by Joel VanderWerf)

Ok, it’s got some serious flaws (Charles Nutter posted a different way of solving the problem, which does work for all cases, and doesn’t use #binding).  But it does give an idea of what you can do with #binding.

Enhanced by Zemanta
 
Browse Archives »
  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)
 

Recent Posts

  • Default values for Attributes
  • Arguments with Trollop
  • Database Paranoia — ActiveRecord Callbacks
  • Ruby Bindings and Scope
  • What’s #method_missing missing?

Search

Browse by Category

  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)

Browse by Tag

  • ActiveRecord
  • binding
  • Command-line interface
  • gotcha
  • irb
  • irbc
  • Languages
  • Library
  • Local variable
  • metaprogramming
  • mini-saga
  • paradigm
  • philosophy
  • programming
  • rails
  • Recursion
  • ruby
  • rubygems
  • Ruby on Rails
  • scope
  • utilities

Browse by Month

  • September 2009 (1)
  • August 2009 (4)
  • April 2009 (1)
  • February 2009 (11)
 
 
  • Blog
  • About
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed