"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.

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| %>

<%= h relationship.send( attribute.to_sym ) %> <% end %> <%h h relationship.link_type.pcln %>

On 10/21/07, Perry S. [email protected] wrote:

}

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

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

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

Hi –

On Mon, 22 Oct 2007, Perry S. wrote:

}
You can use instance_eval:

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

HI

David

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 Y.
http://dev.zeraweb.com/
Ruby And JavaScript Consulting

Dan Y. 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 Y.
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…

Dan Y. 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 Y.
http://dev.zeraweb.com/
Ruby And JavaScript Consulting

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

Regards
Stefan

On Oct 22, 7:48 am, Stefan R. [email protected] wrote:

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

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

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.

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 B.

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

On 21/10/2007, Dan Y. [email protected] 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.

2007/10/22, Giles B. [email protected]:

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

?

Cheers

robert

On 10/23/07, Robert K. [email protected] wrote:

How is this more compact than

[1,2,3].length

?

Check the rest of the thread.


Giles B.

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

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 K.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs