Newbie problem

Trying to learn how to work with arrays. Using RubyMine.
Trouble with array syntax below in html.erb file.

Ruby Arrays <%= array = ['a','b','c'] %>

Shows up in browser as this:
[“a”, “b”, “c”]

using 1.9.1
Thanks for any suggestions.


Warm Regards,
Todd W.
[email protected]
801.814.9084

On 31 August 2010 08:29, Todd W. [email protected] wrote:

<%= array = [‘a’,‘b’,‘c’] %>
Shows up in browser as this:
[“a”, “b”, “c”]

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.

On 31 August 2010 08:29, Todd W. [email protected] wrote:

Shows up in browser as this:
[“a”, “b”, “c”]

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

On 31 August 2010 20:24, Todd W. [email protected] wrote:

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.
Then I would be able to continue to call the values of the array and write
the results to a browser using ruby code within the erb file.
My question: Whats wrong with the code I’m writing?

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.

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.
Then I would be able to continue to call the values of the array and
write
the results to a browser using ruby code within the erb file.
My question: Whats wrong with the code I’m writing?
Thanks for your feedback!

On Tue, Aug 31, 2010 at 1:48 AM, Michael P. [email protected]
wrote:

where you want help.


Warm Regards,
Todd W.
[email protected]
801.814.9084

On 31 August 2010 20:40, Greg D. [email protected] wrote:

On Tue, Aug 31, 2010 at 2:37 PM, Michael P. [email protected] wrote:

<% array = [‘a’,‘b’,‘c’] %>

%w( a b c )

I wasn’t going to get into array-making idioms - I just stuck to the
OP’s method of populating his array :slight_smile:

Thank you much. I get it now!

On Tue, Aug 31, 2010 at 1:37 PM, Michael P. [email protected]
wrote:

There’s nothing wrong with your code, it’s working perfectly. But if


Warm Regards,
Todd W.
[email protected]
801.814.9084

On Tue, Aug 31, 2010 at 2:37 PM, Michael P. [email protected]
wrote:

<% array = [‘a’,‘b’,‘c’] %>

%w( a b c )


Greg D.
destiney.com | gregdonald.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs