ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: "with" statement
by Randy Kramer other posts by this author
Oct 23 2007 12:10PM messages near this date
Re: "with" statement | Re: "with" statement
I may really be misremembering the with statement from Pascal, but, as I 
recall it was more of a namespace / shortcut kind of thing.

For example:

If you have a data structure named Person, which has a bunch of fields within 
it, like FirstName, LastName, Address, ...

You could either assign data to those fields with syntax something like (that 
was a long time ago, and I'm getting old):

Person.FirstName := William
Person.LastName := Smith
...

or you could use the with statement, something like this:

with Person begin
FirstName := William
LastName := Smith
...
end

(Sorry about the syntax errors that I know must be there--like I said, it was 
a long time ago.  Oh, yeah--semicolons!  And keywords in all caps!  Forgetting 
can be good ;-)

I guess my point is, in these discussions about finding a way to simulate the 
Pascal "with" statement in Ruby, it doesn't seem like you (a very generic 
you) are targetting the with functionality from Pascal.

On the other hand, I suspect there must be ways of doing that in Ruby--I just 
can't think of those atm.

Randy Kramer

On Tuesday 23 October 2007 01:48 pm, Pete Elmore wrote:
>  On 21/10/2007, Dan Yoder <dan@[...].com> wrote:
>  > module Kernel
>  >   def with(object,&block)
>  >     object.instance_eval &block
>  >   end
>  > end
>  >
>  > with([1,2,3]) { length } # => 3
>  
>  You could even do something like this:
>  
>  module Kernel
>    def with_block(*args, &block)
>      send(*args) { |obj| obj.instance_eval &block }
>    end
>  end
>  
>  ['asdf', 'jkl', 'semicolon'].with_block(:map) { length } # => [4, 3, 9]
>  
>  That would allow something like this:
>  <% with_block(:form_for, :user, @user) { %>
>    <%= text_field :name %>
>    <%= text_field :email %>
>    <%= password_field :password %>
>  <% } %>
>  
>  It may or may not be useful, but it's fun.
>  
>  
Thread:
Perry Smith
Dan Yoder
Pete Elmore
Randy Kramer
Giles Bowkett
Robert Klemme
Giles Bowkett
Stefan Rusterholz
Yossef Mendelssohn
Perry Smith
David A. Black
Robert Dober
Stephane Wirtel

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved