Comment on this RCR (edit wiki page) | RCRchive home

RCR 251: Add Binding#eval

submitted by batkins on Mon May 10 2004 03:56:03 PM -0700

Status: accepted


Abstract

Allows Ruby programmers to eval code within a Binding object in a more object-oriented, Rubyesque fashion.

Problem

In order to evaluate a bit of code within Binding b, you call Kernel#eval, like so: eval "myvar = 34", b This sets myvar to 32 within b. However, this isn't very object-oriented.

Proposal

I propose to create a Binding#eval method and deprecate and/or remove the binding argument from Kernel#eval. This will allow the above code to be written as: b.eval "myvar = 34"

Analysis

Makes eval'ing within a Binding more fitting with the Ruby Way.

Implementation

class Binding def eval str Kernel.eval str, self end end There should also be a block version.

Vote for this RCR

Strongly opposed [0]
Opposed [0]
Neutral [0]
In favor [11]
Strongly advocate [2]

Change the status of this RCR to:

accepted

rejected

withdrawn


I would really like to see the block version, that's what I ever missed in eval.

But that's probably not easy with the interpreter, because of two bindings inclolved, the argument and the one from the block.

Also on my wishlist would be Kernel.caller to return bindings.

Useful for debugging, I think and would allow some hacking with dynamic scoping of variables (method_missing looks up the caller chain and returns a local variable, can be abused, but sometimes useful like in emacs).

@batkins you can use the HTML pre tags for code formatting

-- Matthias Georgi


My rcr.rb file contains:

 # By default class Binding has no methods at all !
 class Binding
  # Evaluate a Ruby source code string in the binding context
  def eval( str )
    Kernel.eval( str, self)
  end
  # Returns the self in the binding context
  def self()
    eval( "self")
  end
  # Returns the local variables defined in the binding context
  def local_variables()
    eval( "local_variables")
  end
  # Returns the Method that was active, if any, when the binding was created
  #def method() ...???...
  # Returns the Proc that was active, if any, when the binding was created
  #def proc() ... ??? ...
  # Returns the call stack, same format as Kernel##caller()
  def caller( skip = 0 )
    eval( "caller( #{skip})")
  end
  # Returns the value of some variable
  def []( x )
    eval( x.to_s())
  end
  # Set the value of some lvalue
  def []=( l, v )
    eval( "proc {|v| #{l} = v").call( v)
  end
  # Returns the nature of something, nil if that thing is not defined.
  def defined?( x )
    eval( "defined? #{x}")
  end
 end

-- JeanHuguesRobert

Add comments here


Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog