can I tighten this up with block or eval?

Discussion in 'Ruby' started by matt neuburg, Mar 15, 2009.

  1. matt neuburg

    matt neuburg Guest

    I have here a class called ClassMaker whose greeter method returns an
    instance of anonymous class containing a hello method:

    class ClassMaker
    def initialize(s); @howdy = s; end
    def greeter
    c = Class.new do
    def initialize(s); @hello = s; end
    def hello
    puts @hello # this is the nub of the matter
    end
    end
    c.new(@howdy)
    end
    end

    ClassMaker.new("gday").greeter.hello

    I would like the anonymous class's hello method to access whatever the
    value of my ClassMaker instance's @howdy ivar may be at the moment
    greeter is called. I am presently doing this, as you can see, by giving
    the anonymous class an initialize method and passing the @howdy ivar's
    value into an ivar of the anonymous class's instance.

    This works but seems heavy-handed. I feel I should be able to do it
    without the anonymous class having any initialize method at all. The
    whole situation has "closure" written all over it. So I tried to define
    the hello method to accomplish the same thing using evals and blocks and
    other tricky stuff, and became hopelessly confused. Could someone
    straighten me out? Thx - m.

    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt neuburg, Mar 15, 2009
    #1
    1. Advertising

  2. matt neuburg

    Gary Wright Guest

    On Mar 15, 2009, at 1:17 PM, matt neuburg wrote:
    > This works but seems heavy-handed. I feel I should be able to do it
    > without the anonymous class having any initialize method at all. The
    > whole situation has "closure" written all over it. So I tried to
    > define
    > the hello method to accomplish the same thing using evals and blocks
    > and
    > other tricky stuff, and became hopelessly confused. Could someone
    > straighten me out? Thx - m.


    Here is one possibility. Note, I changed ClassMaker to a module
    since it is really just a factory for creating classes and doesn't
    really need to create intermediate instances of ClassMaker to
    accomplish your goal.

    module ClassMaker
    def self.greeter(greeting)
    Class.new do
    define_method :hello do
    puts greeting # this is the nub of the matter
    end
    end
    end
    end

    ClassMaker.greeter("gday").new.hello
    ClassMaker.greeter("aloha").new.hello


    Gary Wright
    Gary Wright, Mar 15, 2009
    #2
    1. Advertising

  3. matt neuburg

    matt neuburg Guest

    Gary Wright <> wrote:

    > On Mar 15, 2009, at 1:17 PM, matt neuburg wrote:
    > > This works but seems heavy-handed. I feel I should be able to do it
    > > without the anonymous class having any initialize method at all. The
    > > whole situation has "closure" written all over it. So I tried to
    > > define
    > > the hello method to accomplish the same thing using evals and blocks
    > > and
    > > other tricky stuff, and became hopelessly confused. Could someone
    > > straighten me out? Thx - m.

    >
    > Here is one possibility. Note, I changed ClassMaker to a module
    > since it is really just a factory for creating classes and doesn't
    > really need to create intermediate instances of ClassMaker to
    > accomplish your goal.
    >
    > module ClassMaker
    > def self.greeter(greeting)
    > Class.new do
    > define_method :hello do
    > puts greeting # this is the nub of the matter
    > end
    > end
    > end
    > end
    > ClassMaker.greeter("gday").new.hello


    Actually, I have to keep the architecture I've got - greeter is going to
    be called without a parameter, that's just how it is. (The problem as I
    posed it is just a schematic reduction of a vast existing architecture
    that does something else entirely.)

    However, your solution shows me exactly the trick I was missing: I need
    to turn @howdy into a local so that a block can see it:

    class ClassMaker
    def initialize(s); @howdy = s; end
    def greeter
    greeting = @howdy # the magic step!
    Class.new do
    define_method :hello do
    puts greeting
    end
    end.new
    end
    end
    ClassMaker.new("gday").greeter.hello

    Thanks! It's nice to stop banging myself over the head with a hammer...
    :)

    m.

    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt neuburg, Mar 15, 2009
    #3
    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. morrell
    Replies:
    1
    Views:
    936
    roy axenov
    Oct 10, 2006
  2. Replies:
    13
    Views:
    625
    Chris Smith
    Aug 16, 2007
  3. Dabbler

    tighten password policy

    Dabbler, Aug 2, 2006, in forum: ASP .Net Security
    Replies:
    1
    Views:
    144
    Consultant
    Aug 2, 2006
  4. Steve V
    Replies:
    6
    Views:
    234
    Steve V
    Apr 20, 2005
  5. yong

    Can I put a class in eval block

    yong, Jan 16, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    97
Loading...

Share This Page