Mixing html tags and Ruby blocks

I'm trying to get the following idea to work:

<%= @column_names.each { |t| <td> t.downcase </td> <td>
@listing.send(t.downcase)</td> } %>

Essentially I want to display a form that has a left and right TD, with
the left TD being a column name and the right TD being the column data.

If there's a better way (the Rails way ?), I'm all ears (and half elf
too.)

Matt

And while were at it, could someone explain why the difference in Ruby /
ERB?

a = [ "a", "b", "c" ]
a.each {|x|} => no result

vs.

<% arr=["a", "b", "c" ] %>
<%= arr.each {|x|} %> => results in 'abc'

I thought I had a solution worked out, but the arr.each is adding stuff
i can't use.

Matt

matt wrote:

And while were at it, could someone explain why the difference in Ruby /
ERB?

a = [ "a", "b", "c" ]
a.each {|x|} => no result

If you are doing this in Ruby, then it won't print anything. Because you are not printing anything!!

vs.

<% arr=["a", "b", "c" ] %>
<%= arr.each {|x|} %> => results in 'abc'

ERB is meant to generate HTML/Text, when used within <%= %>, so above code works.

I suggest, you read some Ruby books. PickAxe2 is a good book, but it assumes some previous programming experience so you can read David Black's Ruby for Rails. Don't consider this reply as an arrogant reply. But you really need to read stuff, you can't push everything to the mailing list.

I thought I had a solution worked out, but the arr.each is adding stuff
i can't use.

Matt

I'm trying to get the following idea to work:

<%= @column_names.each { |t| <td> t.downcase </td> <td>
@listing.send(t.downcase)</td> } %>

Do a favor, do not write code like this in your views.

Well actually I've read the PickAxe, the new Ecomm book, Agile, numerous
Tutorials and even watched a few screencasts. My previous experience is
in Aerospace/Defense writing specialized C++ and flex/bison programs.
I'm not a programming Noob.

Actually I thought my question was quite intelligent. There is a
definite difference in the way that ERB / Ruby handles this simplistic
case, and none of the books I've read highlighted that as a major
sticking point.

Your reply is only arrogant because you really didn't answer the
question. Your reply is a lot like the books on this subject...empty.

So how about finishing up your reply on why the ERB solution is printing
out the "abc" when I haven't given it a puts or print call

Matt

Dude, ERB only print something if you add '=' signal ...

<%=

You should stop reading books in a vertical way, since the difference
between <% %> and <%= %> is explained in Chapter 4.

Is not the reading quantity that matters...

Sorry, I forgot to tell the book name, chapter 4 in Agile Web
Development with Rails.

You could try:

<% @column_names.each do |t| %>
   <td><%=h t.downcase %></td>
   <td><%=h @listing.send(t.downcase) %></td>
<% end %>

If it's an ActiveRecord object you're talking about, "@listing[t]" will be neater than the send.

Cheers,

Pete Yandell

And while were at it, could someone explain why the difference in Ruby /
ERB?

a = [ "a", "b", "c" ]
a.each {|x|} => no result

In irb:

irb(main):001:0> a = [ "a", "b", "c" ]
=> ["a", "b", "c"]
irb(main):002:0> a.each {|x|}
=> ["a", "b", "c"]

So it doesn't give you no result. The "each" method returns whatever it's being asked to iterate over, which in this case is an array. Of course using each like this is pointless as you're iterating over the array, performing a no-op on each element, and then returning the original array.

<% arr=["a", "b", "c" ] %>
<%= arr.each {|x|} %> => results in 'abc'

Exactly the same thing happens here, except erb then takes the returned array, sticks the elements together as a string and spits out the result.

If you want to iterate over the array without printing the result, use "<% ... %>" rather than "<%= ... %>".

Cheers,

Pete Yandell

Hi --

Well actually I've read the PickAxe, the new Ecomm book, Agile, numerous
Tutorials and even watched a few screencasts. My previous experience is
in Aerospace/Defense writing specialized C++ and flex/bison programs.
I'm not a programming Noob.

Just to clarify: "Ruby for Rails" isn't specifically for people who
are new to programming. It's for Rails developers who want to know
what they're actually doing :slight_smile: I think a new programmer with a sense
of adventure, and a willingness to dig deeper as and when needed,
will do very well with it. But that's not the only or even the
principal readership.

> And while were at it, could someone explain why the difference in Ruby /
> ERB?
>
> a = [ "a", "b", "c" ]
> a.each {|x|} => no result
>
> vs.
>
> <% arr=["a", "b", "c" ] %>
> <%= arr.each {|x|} %> => results in 'abc'

The ingredients you need to understand this are:

   1. <%= stuff %> interpolates a string representation of stuff
       into the ERb output stream.
   2. each is not the same as map/collect.
   3. each returns its receiver.

The default string representation of an array is a string-based join.
For ["a", "b", "c"] it's "abc". So, by doing:

   <%= arr.each {|x| <this part is just side effect> } %>

you're telling ERb to interpolate "abc" into the output stream.

Your reply is only arrogant because you really didn't answer the
question. Your reply is a lot like the books on this subject...empty.

Let's be fair. The Pickaxe documents the return value of Array#each,
and that documentation has made its way into ri:

      array.each {|item| block } -> array

"Ruby for Rails" talks at some length (page 244) about the return
value of each, particularly in comparison with the return value of
map. It also talks about ERb and the <%= %> delimiter, around page
30. And AWDWR, of course, talks a lot about ERb.

It's perfectly reasonable to ask about what's happening, but no one's
hiding all this stuff from you :slight_smile:

David

OK. First off, I want to apologize for my irresponsible response. It
wasn't fair of me to be so harsh for being told to go read more. I was
tired, and should have left the keyboard alone, but that isn't your
problem, it's mine.

Next, my original question sparked a secondary question while I was
figuring out the solution on my own. The secondary question being the
ERB vs Ruby question

That question was one of 'principle' and 'theory', not of practicality.
I was trying to understand the 'why', not the 'I broke this, help me fix
it' type of question.

David summed it up well, that 'each' returns it's reciever, and his book
(which I just got ahold of this morning for reference), summed up the
same thing. And the output of the reciever in a ruby script prints
nothing, while the output of ERB is to spit out the text of the
reciever.

I agree that there is a lot of documentation out there to read, but for
me, there is still a lot that is uncorrellated (like each/erb) that I
think is fair game to ask as a question.

The original responder said that I needed to read more and not ask
questions that are readily available. I don't believe that the answer to
this question was 'readily available'

David, you called me on the carpet and you are right. The books do
cover ERB, however, as I read them, they didn't seem to answer my
question, because they didn't cover or warn about printing the return
value of something like each.

Again, I apologize for running astray with my response, and I hope you
can forgive me for such a stretch of temp insanity.

And thank you everyone for being genuinely concerned enough to respond,
and clear this up.

Matt

Thanks. That's what I needed.

But I couldn't get the @listing[t] to work. No errors, but it didn't
display anything either.

Thanks again

Matt