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.

<%= 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.

Trying to learn how to work with arrays. Using RubyMine. 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"]

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

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!

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:

Rails add more functionality to these, but getting a grasp on the Ruby will help.

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