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> %>
<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>
}
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!
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/.
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 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