"with" statement

P

Perry Smith

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

Stéphane Wirtel

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

Robert Dober

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.

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
 
D

David A. Black

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

--
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!
 
D

Dan Yoder

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
 
P

Perry Smith

Dan said:
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

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

Stefan Rusterholz

Dan said:
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

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

Regards
Stefan
 
Y

Yossef Mendelssohn

Dan said:
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

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

Regards
Stefan

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

Pete Elmore

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

Randy Kramer

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top