Ask a Question related to Ruby, Design and Development.
-
Dan Doel #1
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
-
#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... -
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_"... -
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... -
Do BEGIN blocks and END blocks have priority?
If I create code with: BEGIN { # something BEGIN { # something else } } -
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... -
Berger, Daniel #2
Re: Blocks, eval and named parameters
> -----Original Message-----
<snip>> 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
It's been discussed on RubyGarden to some degree. See>
> Anyhow, any thoughts on all this? Thanks in advance for the
> stuff I'll
> probably learn from any discussion
> that ensues.
>
> - Dan
[url]http://www.rubygarden.org/ruby?KeywordArguments[/url]
Regards,
Dan
Berger, Daniel Guest
-
Dan Doel #3
Re: Blocks, eval and named parameters
Berger, Daniel wrote:
Thanks for the link, but it doesn't seem to answer my question at all>It's been discussed on RubyGarden to some degree. See
>
>[url]http://www.rubygarden.org/ruby?KeywordArguments[/url]
>
(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
-
Mauricio Fernández #4
Re: Blocks, eval and named parameters
On Thu, Aug 28, 2003 at 05:13:37PM +0900, Dan Doel wrote:
Hal introduced the following technique in his presentation at the> Berger, Daniel wrote:
>> Thanks for the link, but it doesn't seem to answer my question at all> >It's been discussed on RubyGarden to some degree. See
> >
> >[url]http://www.rubygarden.org/ruby?KeywordArguments[/url]
> >
> (which was about
> passing a block to #eval and such). :)
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
-
dhtapp #5
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
-
Hal Fulton #6
Re: Blocks, eval and named parameters
dhtapp wrote:
Thanks. That kind of comliment makes it worthwhile.> 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 :-)
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



Reply With Quote

