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.
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.
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 K.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.