From: Michael Morin Date: 2008-09-01T00:12:33+09:00 Subject: Re: simple, array question! hwang is wrote: > Stefano Crocco wrote: >> If you're used to C programming, you can look at >> http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-c-and-c-/ >> to see a list of the differences between ruby and C/C++ >> >> I hope this helps >> >> Stefano > > thank you crocco. > > i understand your explanation. > > However, c language speaking.. > int input[100]; > for(i=0;i==100;i++) > { > scanf("%d",input[i]); > } > > This code is written in Ruby How do I > > thank you.. > and, sorry.. i can't speak english well Ruby arrays are not like C arrays. They don't have a fixed size, they can change size depending on what you add to or remove from them. For example, creating an array with 100 elements isn't often done in Ruby. You can do it with Array.new(100), but it's just not necessary. Empty arrays are created either with the empty array literal [], or by calling the Array constructor with Array.new. The code you posted can be done like this: input = [] # Create an empty array 100.times do # Read from keyboard and append to array using the << operator # gets reads a line # chomp removes the newline (from the enter key) # to_i converts from a string like "10" into an integer like 10 input << gets.chomp.to_i end -- Michael Morin Guide to Ruby http://ruby.about.com/ Become an About.com Guide: beaguide.about.com About.com is part of the New York Times Company