newbie problem

5 views
Skip to first unread message

Todd Weeks

unread,
Aug 31, 2010, 3:29:20 AM8/31/10
to rubyonra...@googlegroups.com
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"]

using 1.9.1
Thanks for any suggestions.

--
Warm Regards,
Todd Weeks

Michael Pavling

unread,
Aug 31, 2010, 3:48:31 AM8/31/10
to rubyonra...@googlegroups.com
On 31 August 2010 08:29, Todd Weeks <tcr....@gmail.com> 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.

Colin Law

unread,
Aug 31, 2010, 4:34:24 AM8/31/10
to rubyonra...@googlegroups.com
On 31 August 2010 08:29, Todd Weeks <tcr....@gmail.com> wrote:
> 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

Dave Aronson

unread,
Aug 31, 2010, 10:28:08 AM8/31/10
to rubyonrails-talk
On Aug 31, 3:29 am, Todd Weeks <tcr.t...@gmail.com> wrote:

> 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

Armand Macintosh

unread,
Aug 31, 2010, 1:38:09 PM8/31/10
to rubyonra...@googlegroups.com
May be He wants to access the content of the array like this:

array[0] #1
array[1] #2
array[2] #3

Here's how to access the content of the array using .each:


array = ['a','b','c']
array.each do|x|
  puts x
end

results
a
b
c

2010/8/31 Dave Aronson <googlegr...@davearonson.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.


Michael Pavling

unread,
Aug 31, 2010, 1:44:06 PM8/31/10
to rubyonra...@googlegroups.com
On 31 August 2010 18:38, Armand Macintosh <snip...@gmail.com> wrote:
> array = ['a','b','c']
> array.each do|x|
>   puts x
> end
>
> results
> a
> b
> c

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.

Todd Weeks

unread,
Aug 31, 2010, 3:24:13 PM8/31/10
to rubyonra...@googlegroups.com
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!


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

Michael Pavling

unread,
Aug 31, 2010, 3:37:01 PM8/31/10
to rubyonra...@googlegroups.com
On 31 August 2010 20:24, Todd Weeks <tcr....@gmail.com> 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.

Michael Pavling

unread,
Aug 31, 2010, 3:44:39 PM8/31/10
to rubyonra...@googlegroups.com
On 31 August 2010 20:40, Greg Donald <gdo...@gmail.com> wrote:

> On Tue, Aug 31, 2010 at 2:37 PM, Michael Pavling <pav...@gmail.com> 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 :-)

Greg Donald

unread,
Aug 31, 2010, 3:40:31 PM8/31/10
to rubyonra...@googlegroups.com
On Tue, Aug 31, 2010 at 2:37 PM, Michael Pavling <pav...@gmail.com> wrote:
> <% array = ['a','b','c']  %>

%w( a b c )

--
Greg Donald
destiney.com | gregdonald.com

Todd Weeks

unread,
Aug 31, 2010, 4:02:52 PM8/31/10
to rubyonra...@googlegroups.com
Thank you much. I get it now!

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

Dave Aronson

unread,
Sep 1, 2010, 8:32:42 AM9/1/10
to rubyonrails-talk
Todd Weeks 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.

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

Reply all
Reply to author
Forward
0 new messages