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 tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 801.814.9084 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 August 2010 08:29, Todd Weeks <tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 31 August 2010 08:29, Todd Weeks <tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.1I 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Aug 31, 3:29 am, Todd Weeks <tcr.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org>> On Aug 31, 3:29 am, Todd Weeks <tcr.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 August 2010 18:38, Armand Macintosh <snip3r.dp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> array = [''a'',''b'',''c''] > array.each do|x| > puts x > end > > results > a > b > cThat''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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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 Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 31 August 2010 08:29, Todd Weeks <tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Warm Regards, Todd Weeks tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 801.814.9084 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 31 August 2010 20:24, Todd Weeks <tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On Tue, Aug 31, 2010 at 2:37 PM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> <% array = [''a'',''b'',''c''] %>%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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 31 August 2010 20:40, Greg Donald <gdonald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Aug 31, 2010 at 2:37 PM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 :-) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Thank you much. I get it now! On Tue, Aug 31, 2010 at 1:37 PM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 31 August 2010 20:24, Todd Weeks <tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Warm Regards, Todd Weeks tcr.todd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 801.814.9084 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.) -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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.