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 …
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 …
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-/
to see a list of the differences between ruby and C/C++
I hope this helps
Stefano
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-/
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
wow. thank to understand too well…
thank you.^^
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
input << gets.chomp.to_i
end
thank you. robert
have a nice day
2008/8/31 Stefano C. [email protected]:
In ruby you don’t have to declare a variable (actually, you can’t), you just
assign a value to it. For examplea = [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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs