Blocks, eval and named parameters

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Blocks, eval and named parameters

    Hello,

    So I was reading the brief discussion on named parameters in the
    gigantic troll-fueled thread (forward
    note, the hash option wasn't discussed, and I do know about it, so I
    don't need to be lectured :) ), and I
    saw the example of named parameters in a constructor like so:

    class Foo
    attr_accessor :foo, :bar
    def initialize
    ...
    yield self if block_given?
    end
    end

    Foo.new { |obj| obj.foo = 5; obj.bar = 6 }

    And someone liked it. However, this isn't really any better than:

    obj = Foo.new
    obj.foo = 5
    obj.foo = 6

    Unless you make the accessors private, in which case I think you need to
    instance_eval the block.

    So, I got to thinking of a way to possibly use a block to assign to
    local variables. For example,
    thinking of the up-and-coming scoping rule changes, maybe one could do:

    class Foo
    def initialize(&blk)
    if blk
    instance_eval &blk
    end

    @height = height
    end
    end

    Foo.new do height = 5 end

    However, then I thought, perhaps this already works like so:

    class Foo
    def initialize(height=nil, &blk)
    if blk then instance_eval &blk end
    @height = height
    end
    end

    Since height will be defined as a local variable, and should propogate
    out. However, instance_eval
    doesn't work this way, so I doubt that it will work with the new scoping
    rules.

    So, finally for my question:

    instance_eval, class_eval and module_eval all optionally take code
    blocks to evaluate in a certain
    context. However, eval itself only takes a string. Is there, or will
    there ever be a way to evaluate
    a code block with an arbitrary binding object/in an arbitrary context,
    so that something like the
    above will work? I suppose a block used in such a way is no longer a
    closure, but it could be
    an interesting addition to Proc's (note, I'm not sure how much utility
    it would have outside this
    example).

    Until then, I think the closest I can come to named parameters using
    blocks is:

    class Foo
    def initialize(&blk)
    if blk
    bnd = blk.call
    @height = eval "height", bnd
    end
    end
    end

    Foo.new do height = 6; binding end

    Then again, writing to allow:

    Foo.new(:height => 6)

    Isn't terribly hard, looks fine, and allows you to still pass a block
    for other purposes if you need it,
    so I don't think things should be changed merely for this example's sake.

    Anyhow, any thoughts on all this? Thanks in advance for the stuff I'll
    probably learn from any discussion
    that ensues.

    - Dan


    Dan Doel Guest

  2. Similar Questions and Discussions

    1. #36798 [Opn->Csd]: mysql error when using named parameters in a query with high ascii
      ID: 36798 Updated by: iliaa@php.net Reported By: albert at jool dot nl -Status: Open +Status: Closed...
    2. variable named field calculated from variable named fields
      I have two form fields per line defined in a cfloop thusly: <cfloop index="i" from= "1" to= "100" Step="1"> <cfif isdefined("form.item_number_"...
    3. create parameters without creating parameters
      cant you create ado command parameteres without creating a parameter object? i have a function that takes the name of a stored proc, and two...
    4. Do BEGIN blocks and END blocks have priority?
      If I create code with: BEGIN { # something BEGIN { # something else } }
    5. eval parameters one liner
      I know I saw something like this in the past I just can't find it. Anyone got any ideas? perl -e 'print eval { @ARGV }, "\n"' 5 + 5 it should...
  3. #2

    Default Re: Blocks, eval and named parameters

    > -----Original Message-----
    > From: Dan Doel [mailto:djd15@po.cwru.edu]
    > Sent: Wednesday, August 27, 2003 1:46 AM
    > To: [email]ruby-talk@ruby-lang.org[/email]
    > Subject: Blocks, eval and named parameters
    <snip>
    >
    > Anyhow, any thoughts on all this? Thanks in advance for the
    > stuff I'll
    > probably learn from any discussion
    > that ensues.
    >
    > - Dan
    It's been discussed on RubyGarden to some degree. See

    [url]http://www.rubygarden.org/ruby?KeywordArguments[/url]

    Regards,

    Dan

    Berger, Daniel Guest

  4. #3

    Default Re: Blocks, eval and named parameters

    Berger, Daniel wrote:
    >It's been discussed on RubyGarden to some degree. See
    >
    >[url]http://www.rubygarden.org/ruby?KeywordArguments[/url]
    >
    Thanks for the link, but it doesn't seem to answer my question at all
    (which was about
    passing a block to #eval and such). :)

    I guess I should ramble a bit less in my posts.

    - Dan


    Dan Doel Guest

  5. #4

    Default Re: Blocks, eval and named parameters

    On Thu, Aug 28, 2003 at 05:13:37PM +0900, Dan Doel wrote:
    > Berger, Daniel wrote:
    >
    > >It's been discussed on RubyGarden to some degree. See
    > >
    > >[url]http://www.rubygarden.org/ruby?KeywordArguments[/url]
    > >
    > Thanks for the link, but it doesn't seem to answer my question at all
    > (which was about
    > passing a block to #eval and such). :)
    Hal introduced the following technique in his presentation at the
    Euruko
    ([url]http://hypermetrics.com/rubyhacker/euruko03/rubyesque/slide7.html[/url])

    class A
    attr_writer :foo
    def initialize
    yield self
    class << self; private :foo; end
    end
    end

    to avoid leaving accessors after instantiation.

    You could generalize this as follows:

    def foo
    s = Struct.new(:param1, :param2, :etc)
    yield s
    # do things with s.param1, s.param2, s.etc
    end

    foo do |p|
    p.param1 = :bla
    p.param2 = "foo"
    p.etc = ['bar', 'baz']
    end

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    Sorry. I just realized this sentance makes no sense :)
    -- Ian Main

    Mauricio Fernández Guest

  6. #5

    Default Re: Blocks, eval and named parameters

    What a thoroughly useful set of slides for a newbie. And, as a bonus, an
    introduction to his writing style...now I know I want the book :-)

    - dan


    "Mauricio Fernández" <batsman.geo@yahoo.com> wrote in message
    news:20030828093315.GB12453@student.ei.uni-stuttgart.de...
    > Hal introduced the following technique in his presentation at the
    > Euruko
    > ([url]http://hypermetrics.com/rubyhacker/euruko03/rubyesque/slide7.html[/url])
    >

    dhtapp Guest

  7. #6

    Default Re: Blocks, eval and named parameters

    dhtapp wrote:
    > What a thoroughly useful set of slides for a newbie. And, as a bonus, an
    > introduction to his writing style...now I know I want the book :-)
    Thanks. That kind of comliment makes it worthwhile.

    Hal

    > - dan
    >
    >
    > "Mauricio Fernández" <batsman.geo@yahoo.com> wrote in message
    > news:20030828093315.GB12453@student.ei.uni-stuttgart.de...
    >
    >
    >>Hal introduced the following technique in his presentation at the
    >>Euruko
    >>([url]http://hypermetrics.com/rubyhacker/euruko03/rubyesque/slide7.html[/url])
    >>
    >
    >
    >
    >
    >


    Hal Fulton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139