From: Corey Konrad <0011@...> Date: 2007-03-16T10:15:56+09:00 Subject: Re: to_a Ash wrote: > On Mar 15, 8:33 pm, Corey Konrad <0...@hush.com> wrote: >> > The issue isn't with the parentheses, but with the behavior of the >> > array = 1..8 >> -- >> Posted viahttp://www.ruby-forum.com/. > > You're getting closer. It's correct that #to_a does not transform the > range into an array. In Ruby, methods that change the object they're > called on are usually marked with a !, like #sort! on Array (sorts the > array in place) or #capitalize! on String (uppercases the first letter > in place). Otherwise you can usually expect to get a new object back. > > Here's a snippet that uses () to build the array but still doesn't > work: > > array = (1..8) # Create a Range object, give it the name "array" > array.to_a # Call the method "to_a" on the Range, get back an > Array, but > # don't put it anywhere > puts array[1] # Message not understood, because "array" is the > original Range. > > It just occurred to me that you might also be confused about what's > happening in the > first line of your working example. > > array = (1..8).to_a > > The assignment to "array" is done _last_. So this reads as "Create a > new Range from 1 to 8, send it the message 'to_a', then store the > result of all of that in 'array'", not "Create a new Range from 1 to > 8, store that in 'array', then send it the message 'to_a'". > > Just out of curiosity, is this your first programming language, or if > not what is your background? The issues you're encountering are > things that you'll run into in many languages, Ruby just isn't quite > as verbose in having them :-) My background i have a degree in computer information systems and have been studying things like this for going on 4 years now, i got a 3.12 gpa in school and i have studied the syntax of many languages but for some reason i just cant ever seem to get anywhere with it, its getting really frustrating and i am starting to think that maybe i am mentally retarded literally. -- Posted via http://www.ruby-forum.com/.