Simple, array question!

hi! Gentlemen

i don't understand array declaration..

c languages, array declaration is...

char str[100];

but, how do i declaration an array for ruby..

oo is difficult...

plz teach me ..

···

--
Posted via http://www.ruby-forum.com/.

In ruby you don't have to declare a variable (actually, you can't), you just
assign a value to it. For example

a = [1, 2, 3, 4]

stores an array containing the elements 1, 2, 3 and 4 to the local variable a.

By the way, ruby has a String class, so you don't need to use an array of char
to represent a string:

str = "text"

or

str = String.new "text"

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

···

On Sunday 31 August 2008, hwang is wrote:

hi! Gentlemen

i don't understand array declaration..

c languages, array declaration is...

char str[100];

but, how do i declaration an array for ruby..

oo is difficult...

plz teach me ..

hwang is wrote:

hi! Gentlemen

i don't understand array declaration..

c languages, array declaration is...

char str[100];

but, how do i declaration an array for ruby..

oo is difficult...

plz teach me ..

To get a specific size of array, you specify the number of items.

ar = Array.new(100)
p ar.length
=> 100

Also, you can specify how to initialize the array.

n = 5
ar = Array.new(n, "A")
n.times {|i| p i, ar[i]}

results in:

0
"A"
1
"A"
2
"A"
3
"A"
4
"A"

I hope that helps.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.

Some additonal note: the is an Array constructor. It's a special
syntax for creating a new Array instance and appending elements given.
This is similar to "", '' and // which are also syntactic sugar for
instance creation:

irb(main):001:0> String.new
=> ""
irb(main):002:0> ""
=> ""

Kind regards

robert

···

2008/8/31 Stefano Crocco <stefano.crocco@alice.it>:

In ruby you don't have to declare a variable (actually, you can't), you just
assign a value to it. For example

a = [1, 2, 3, 4]

stores an array containing the elements 1, 2, 3 and 4 to the local variable a.

--
use.inject do |as, often| as.you_can - without end

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

Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company

thank you. robert

have a nice day :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

wow. thank to understand too well...
thank you.^^

···

--
Posted via http://www.ruby-forum.com/.