"with" statement

Pascal has a with statement. (I'm showing my age). It would be nice
instead of:

    <td><%= h relationship.parent_id %></td>
    <td><%= h relationship.link_type.pcln %></td>
    <td><%= h relationship.child_id %></td>
    <td><%= h relationship.active %></td>
    <td><%= h relationship.notes %></td>
    <td><%= h relationship.created_at %></td>
    <td><%= h relationship.updated_at %></td>

I could do:

    with(relationship) {
      <td><%= h parent_id %></td>
      <td><%= h link_type.pcln %></td>
      <td><%= h child_id %></td>
      <td><%= h active %></td>
      <td><%= h notes %></td>
      <td><%= h created_at %></td>
      <td><%= h updated_at %></td>
    }

I bet there is a clever way to do this -- I just don't know now.

···

--
Posted via http://www.ruby-forum.com/.

I don't approve this solution, but why can't you use it ?

<% %w{parent_id child_id active notes created_at updated_at}.each do |
attribute> %>
  <td><%= h relationship.send( attribute.to_sym ) %></td>
<% end %>
  <td><%h h relationship.link_type.pcln %></td>

You might want to have a look at instance_eval, I however feel that
the with statement was rather a weakness of Pascal.

HTH
Robert

···

On 10/21/07, Perry Smith <pedz@easesoftware.com> wrote:

Pascal has a with statement. (I'm showing my age). It would be nice
instead of:

    <td><%= h relationship.parent_id %></td>
    <td><%= h relationship.link_type.pcln %></td>
    <td><%= h relationship.child_id %></td>
    <td><%= h relationship.active %></td>
    <td><%= h relationship.notes %></td>
    <td><%= h relationship.created_at %></td>
    <td><%= h relationship.updated_at %></td>

I could do:

    with(relationship) {
      <td><%= h parent_id %></td>
      <td><%= h link_type.pcln %></td>
      <td><%= h child_id %></td>
      <td><%= h active %></td>
      <td><%= h notes %></td>
      <td><%= h created_at %></td>
      <td><%= h updated_at %></td>
    }

I bet there is a clever way to do this -- I just don't know now.
--
Posted via http://www.ruby-forum.com/\.

--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

Hi --

Pascal has a with statement. (I'm showing my age). It would be nice
instead of:

   <td><%= h relationship.parent_id %></td>
   <td><%= h relationship.link_type.pcln %></td>
   <td><%= h relationship.child_id %></td>
   <td><%= h relationship.active %></td>
   <td><%= h relationship.notes %></td>
   <td><%= h relationship.created_at %></td>
   <td><%= h relationship.updated_at %></td>

I could do:

   with(relationship) {
     <td><%= h parent_id %></td>
     <td><%= h link_type.pcln %></td>
     <td><%= h child_id %></td>
     <td><%= h active %></td>
     <td><%= h notes %></td>
     <td><%= h created_at %></td>
     <td><%= h updated_at %></td>
   }

You can use instance_eval:

david-a-blacks-computer:~ dblack$ erb
<% a = "hi" %>
<% a.instance_eval do %>
   <%= upcase %>
<% end %>
^D

   HI

David

···

On Mon, 22 Oct 2007, Perry Smith wrote:

--
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

Robert and David are right - you can use instance_eval. If you really
want with, it is very easy to add:

module Kernel
  def with(object,&block)
    object.instance_eval &block
  end
end

with([1,2,3]) { length } # => 3

···

---
Dan Yoder
http://dev.zeraweb.com/
Ruby And JavaScript Consulting

On Oct 21, 8:15 am, Perry Smith <p...@easesoftware.com> wrote:

Pascal has a with statement. (I'm showing my age). It would be nice
instead of:

    <td><%= h relationship.parent_id %></td>
    <td><%= h relationship.link_type.pcln %></td>
    <td><%= h relationship.child_id %></td>
    <td><%= h relationship.active %></td>
    <td><%= h relationship.notes %></td>
    <td><%= h relationship.created_at %></td>
    <td><%= h relationship.updated_at %></td>

I could do:

    with(relationship) {
      <td><%= h parent_id %></td>
      <td><%= h link_type.pcln %></td>
      <td><%= h child_id %></td>
      <td><%= h active %></td>
      <td><%= h notes %></td>
      <td><%= h created_at %></td>
      <td><%= h updated_at %></td>
    }

I bet there is a clever way to do this -- I just don't know now.
--
Posted viahttp://www.ruby-forum.com/.

Dan Yoder wrote:

Robert and David are right - you can use instance_eval. If you really
want with, it is very easy to add:

module Kernel
  def with(object,&block)
    object.instance_eval &block
  end
end

with([1,2,3]) { length } # => 3

---
Dan Yoder
http://dev.zeraweb.com/
Ruby And JavaScript Consulting

Yippie!!! I figured there was a way.

I think I also want for_with -- but I can do that (I hope).

Thank you to all...

···

--
Posted via http://www.ruby-forum.com/\.

Dan Yoder wrote:

Robert and David are right - you can use instance_eval. If you really
want with, it is very easy to add:

module Kernel
  def with(object,&block)
    object.instance_eval &block
  end
end

with([1,2,3]) { length } # => 3

---
Dan Yoder
http://dev.zeraweb.com/
Ruby And JavaScript Consulting

Why not just alias instance_eval? Would achieve the same and is less
overhead.

Regards
Stefan

···

--
Posted via http://www.ruby-forum.com/\.

module Kernel
  def with(object,&block)
    object.instance_eval &block
  end
end

with([1,2,3]) { length } # => 3

This is really neat and compact. I love it. I think using eval that
much probably results in performance costs, but I'm definitely going
to add this to my .irbrc.

···

--
Giles Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/

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.

···

On 21/10/2007, Dan Yoder <dan@zeraweb.com> wrote:

module Kernel
  def with(object,&block)
    object.instance_eval &block
  end
end

with([1,2,3]) { length } # => 3

Probably because it wouldn't read right anymore.

"With [this object] do [some stuff]" reads naturally, whereas "[this
object] with [some stuff]" is somewhat strange.

···

On Oct 22, 7:48 am, Stefan Rusterholz <apei...@gmx.net> wrote:

Dan Yoder wrote:
> Robert and David are right - you can use instance_eval. If you really
> want with, it is very easy to add:

> module Kernel
> def with(object,&block)
> object.instance_eval &block
> end
> end

> with([1,2,3]) { length } # => 3

> ---
> Dan Yoder
>http://dev.zeraweb.com/
> Ruby And JavaScript Consulting

Why not just alias instance_eval? Would achieve the same and is less
overhead.

Regards
Stefan
--
Posted viahttp://www.ruby-forum.com/.

--
-yossef

How is this more compact than

[1,2,3].length

?

Cheers

robert

···

2007/10/22, Giles Bowkett <gilesb@gmail.com>:

> module Kernel
> def with(object,&block)
> object.instance_eval &block
> end
> end
>
> with([1,2,3]) { length } # => 3

This is really neat and compact.

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 :wink:

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@zeraweb.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.

Check the rest of the thread.

···

On 10/23/07, Robert Klemme <shortcutter@googlemail.com> wrote:

2007/10/22, Giles Bowkett <gilesb@gmail.com>:
> > module Kernel
> > def with(object,&block)
> > object.instance_eval &block
> > end
> > end
> >
> > with([1,2,3]) { length } # => 3
>
> This is really neat and compact.

How is this more compact than

[1,2,3].length

?

--
Giles Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/