What's wrong with that?
> Thanks for any suggestions.
Your post doesn't include any questions, so it's a little hard to know
where you want help.
You have asked it to show the array and it has done so. What are you
trying to do? Possibly you need something to use something like
array.each. Possibly have a look at some basic Ruby tutorials (as
opposed to Rails tutorials).
> using 1.9.1
I presume that is the version of Ruby, I don't know which version of
Rails you are using, but I believe there are problems with ruby 1.9.1
and some versions of rails.
Colin
> Trouble with array syntax below in html.erb file.
> <html>
> <head>
> <title>Ruby Arrays</title>
> </head>
> <body>
> <%= array = ['a','b','c'] %>
> </body>
> </html>
>
> Shows up in browser as this:
> ["a", "b", "c"]
That's perfectly valid. ["a", "b", "c"] is the same as ['a', 'b',
'c']. That is, "a" is the same as 'a', and so on. (Unlike in some
languages, where only one or the other is used at all, or some where
it makes a difference in the type, such as C/C++, where 'a' would be a
character and "a" would be an array containing an 'a' and a null.)
When printing values out, Ruby represents strings using double quotes
instead of single as you used. Ruby doesn't remember (or care) which
you used, once it determines the actual value of the string.
The only time it matters is when *you* are writing a string; single
quotes prevent variables and other "magic strings" taking on their
values. For example, try this in irb:
>> x = 3
=> 3
>> 'x is #{x}'
=> "x is \#{x}"
>> "x is #{x}"
=> "x is 3"
>> y = "foo"
=> "foo"
>> z = 'foo'
=> "foo"
>> y == z
=> true
-Dave
--
Specialization is for insects. -RAH | Have Pun, Will Babble! -me
Programming Blog: http://codosaur.us | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net
* * * * * WATCH THIS SPACE * * * * * | Ruby: http://mars.groupsite.com
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That's not going to work in his .erb file is it, as 'puts' is going to
write to the console.
If the OP actually asks a question, we'll *know* what he wants to do
with his array rather than taking random stabs in the dark.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
There's nothing wrong with your code, it's working perfectly. But if
you want to iterate the array, there are several ways.
Most commonly, you can use ".each":
<% array = ['a','b','c'] %>
<% array.each do |element| %>
<%= element %>
<% end %>
but if the format you've noted is specific ("a,b,c") then you can use
the ".join" method:
<% array = ['a','b','c'] %>
<%= array.join(", ") %>
I would recommend having a play with the Enumerable and Array API pages:
http://ruby-doc.org/core/classes/Enumerable.html
http://ruby-doc.org/core/classes/Array.html
Rails add more functionality to these, but getting a grasp on the Ruby
will help.
I wasn't going to get into array-making idioms - I just stuck to the
OP's method of populating his array :-)
%w( a b c )
--
Greg Donald
destiney.com | gregdonald.com
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Im not getting the output I expect. In the tutorial I am doing. It is
> suppose to just write the values to the browser.
>
> Which would write *a,b,c *to the browser and shouldn't also be showing the
> array brackets or double quotes.
Ah, okay. The solution has been shown.
Meanwhile, it sounds like this tutorial actually has you delve into
Ruby itself rather than only playing on the surface with Rails. I
would suggest that you take a sidetrack and try a Ruby-only tutorial
before you progress much further with the Rails one.
(Actually I suggest that ALL Rails people bother to actually learn
Ruby, so they'll have much more of a clue what's going on under the
hood and how to make it sit up and do neat tricks. But in your case,
it seems like you're hitting real-Ruby questions already, while many
Rails tutorials don't expose much of that.)