# Simple, array question!

**URL:** <https://www.ruby-forum.com/t/simple-array-question/147335>\
**Category:** Ruby\
**Created:** [August 31, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335 "2008-08-31T14:42:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 31, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335/1 "2008-08-31T14:42:32Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 31, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335/2 "2008-08-31T15:02:56Z")

</div>

On Sunday 31 August 2008, hwang is wrote:

> oo is difficult…
> 
> plz teach me …

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-/](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

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 31, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335/3 "2008-08-31T16:55:41Z")

</div>

Stefano C. 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-/](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

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 31, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335/4 "2008-08-31T17:28:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [August 31, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335/5 "2008-08-31T17:18:10Z")

</div>

hwang is wrote:

> 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

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [September 2, 2008, 2:14am UTC](https://www.ruby-forum.com/t/simple-array-question/147335/6 "2008-09-02T02:14:01Z")

</div>

thank you. robert

have a nice day 🙂

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [September 1, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335/7 "2008-09-01T16:14:35Z")

</div>

2008/8/31 Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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.

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

---

<div class="post-metadata">

**Author:** ![his2000x](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3d9bf3/32.png) [@his2000x](https://www.ruby-forum.com/u/his2000x)\
**Post date:** [September 4, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/simple-array-question/147335/8 "2008-09-04T15:45:00Z")

</div>

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.
