Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
define_method and blocks
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
  2 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
 
David Carlton  
View profile  
 More options Jul 21 2007, 8:30 am
Newsgroups: comp.lang.ruby
From: David Carlton <carl...@bactrian.org>
Date: Sat, 21 Jul 2007 21:30:02 +0900
Local: Sat, Jul 21 2007 8:30 am
Subject: define_method and blocks
I'm trying to programmatically add methods taking a block to a class.
Specifically I'm trying to write a method 'define_inline_element' that
will let me type

  define_inline_element :title

and create a method called 'title' in my class, with appropriate
behavior, taking a block.

My first stab was this:

  def self.define_inline_element(element)
    define_method(element) do |&block|
      inline_element(element.to_s, &block)
    end
  end

but |&block| was syntactically invalid.  Then I tried this:

  def self.define_inline_element(element)
    define_method(element) do
      inline_element(element.to_s) { yield }
    end
  end

but when I invoked one of my generated methods with a block, the block
got thrown away and the yield call failed.

So now I've resorted to eval:

  def self.define_inline_element(element)
    module_eval %Q{def #{element}(&block)
                     inline_element("#{element}", &block)
                   end}
  end

which works, but I don't like eval as much as blocks.

Anything I'm missing?  I'm using Ruby 1.8.2, if that matters.  (Yes, I
know I'm a few versions behind.)

David Carlton
carl...@bactrian.org


 
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.
Logan Capaldo  
View profile  
 More options Jul 21 2007, 8:57 am
From: "Logan Capaldo" <logancapa...@gmail.com>
Date: Sat, 21 Jul 2007 21:57:03 +0900
Local: Sat, Jul 21 2007 8:57 am
Subject: Re: define_method and blocks

On 7/21/07, David Carlton <carl...@bactrian.org> wrote:

You're not really missing anything. This is an unfortunate oversight that's
been fixed in 1.9

meth { |&block| } is valid syntax in 1.9


 
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 »