What's the difference here? (class extension on run-time)

Discussion in 'Ruby' started by Joshua Muheim, Mar 20, 2007.

  1. Hi all

    I'm not very experienced with Ruby yet. I have the following code (from
    a Ruby on Rails project) that I have adapted from a book and modified a
    bit:

    class IncenseFormBuilder < ActionView::Helpers::FormBuilder
    private
    def self.create_tagged_field(method_name)
    define_method(method_name) do |label, *args|
    html_surround(label, super) # Provides the HTML code that
    surrounds the form label and the method (super) that generates the input
    tag(s)
    end
    end

    field_helpers.reject{|h| h == 'hidden_field'}.concat([:select]).each
    do |method_name|
    create_tagged_field(method_name)
    end
    end
    end

    This code works as expected: it defines some methods on run-time and I
    have those neat methods available in my views.

    I wanted to edit the code in the following way to make it shorter:

    class IncenseFormBuilder < ActionView::Helpers::FormBuilder
    private
    field_helpers.reject{|h| h == 'hidden_field'}.concat([:select]).each
    do |method_name|
    define_method(method_name) do |label, *args|
    html_surround(label, super) # Provides the HTML code that
    surrounds the form label and the method (super) that generates the input
    tag(s)
    end
    end
    end
    end

    But now I get a strange "private method `text_field' called for
    #<IncenseFormBuilder:0x22fd6b0>" error although the code seems to do
    quite the same as before? What have i missed?

    Thanks for help,
    Joshua

    --
    Posted via http://www.ruby-forum.com/.
     
    Joshua Muheim, Mar 20, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    3,117
  2. Florian Weimer
    Replies:
    2
    Views:
    391
    Florian Weimer
    Jan 9, 2007
  3. flamesrock
    Replies:
    8
    Views:
    469
    Hendrik van Rooyen
    Nov 24, 2006
  4. Pierre Yves
    Replies:
    2
    Views:
    486
    Pierre Yves
    Jan 10, 2008
  5. Ruwan Budha
    Replies:
    4
    Views:
    1,108
    Kirk Haines
    Mar 9, 2011
Loading...

Share This Page