Ruby on Rails Tuesday, August 31, 2010

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 <googlegroups2dave@davearonson.com>
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

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment