Google Groups Home
Help | Sign in
how to do instance_eval with arguments
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jim Weirich  
View profile  
 More options Feb 8 2006, 5:33 pm
Newsgroups: comp.lang.ruby
From: Jim Weirich <j...@weirichhouse.org>
Date: Thu, 9 Feb 2006 07:33:53 +0900
Local: Wed, Feb 8 2006 5:33 pm
Subject: how to do instance_eval with arguments
I'm prototyping a DSL and came across a situtation where I have a lambda
that I would like to give to instance_eval, but the lambda takes
arguments.  Instance_eval will not supply any arguments when evaluating
the lambda and I don't see a straightforward way around this.

Any suggestions?

Here's a test case ...

class Dummy
  def f
    :dummy_value
  end
end

def instance_eval_with_args(obj, *args, &block)
  # Magic goes here to evaluate +block+ in the scope of
  # +obj+, yet pass a list of argument values to the block.
end

class TestInstanceEvalWithArgs < Test::Unit::TestCase
  def test_instance_eval_with_args
    # Create a block that returns the value of an argument and a value
    # of a method call to +self+.  This is the basic functionality I
need.
    block = lambda { |a| [a, f] }

    assert_equal [:arg_value, :dummy_value],
      instance_eval_with_args(Dummy.new, :arg_value, &block)
  end
end

--
-- Jim Weirich

--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Yukihiro Matsumoto  
View profile  
 More options Feb 8 2006, 6:00 pm
Newsgroups: comp.lang.ruby
From: Yukihiro Matsumoto <m...@ruby-lang.org>
Date: Thu, 9 Feb 2006 08:00:51 +0900
Local: Wed, Feb 8 2006 6:00 pm
Subject: Re: how to do instance_eval with arguments
Hi,

In message "Re: how to do instance_eval with arguments"
    on Thu, 9 Feb 2006 07:33:53 +0900, Jim Weirich <j...@weirichhouse.org> writes:
|
|I'm prototyping a DSL and came across a situtation where I have a lambda
|that I would like to give to instance_eval, but the lambda takes
|arguments.  Instance_eval will not supply any arguments when evaluating
|the lambda and I don't see a straightforward way around this.
|
|Any suggestions?

CVS HEAD (1.9) has instance_exec method that works exactly what you
want.

                                                        matz.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Weirich  
View profile  
 More options Feb 8 2006, 7:46 pm
Newsgroups: comp.lang.ruby
From: Jim Weirich <j...@weirichhouse.org>
Date: Thu, 9 Feb 2006 09:46:16 +0900
Local: Wed, Feb 8 2006 7:46 pm
Subject: Re: how to do instance_eval with arguments

Yukihiro Matsumoto wrote:
> <j...@weirichhouse.org> writes:
> |
> |I'm prototyping a DSL and came across a situtation where I have a lambda
> |that I would like to give to instance_eval, but the lambda takes
> |arguments.  Instance_eval will not supply any arguments when evaluating
> |the lambda and I don't see a straightforward way around this.
> |
> |Any suggestions?

> CVS HEAD (1.9) has instance_exec method that works exactly what you
> want.

Excellent!  I think Matz's time machine is every bit as good as
Guido's[1].

--
-- Jim Weirich

[1] http://www.catb.org/jargon/html/G/Guido.html

--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Weirich  
View profile  
 More options Feb 8 2006, 7:50 pm
Newsgroups: comp.lang.ruby
From: Jim Weirich <j...@weirichhouse.org>
Date: Thu, 9 Feb 2006 09:50:09 +0900
Local: Wed, Feb 8 2006 7:50 pm
Subject: Re: how to do instance_eval with arguments
Mauricio Fernandez wrote:
> On Thu, Feb 09, 2006 at 07:33:53AM +0900, Jim Weirich wrote:
>> Any suggestions?

[...]

> One way to implement it in 1.8:

[... solution elided ...]

Thanks!  That should be good enough for my prototype ... at least until
1.9 features become standard.

--
-- Jim Weirich

--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google