Monday, 3 February 2014

How To Get The Length Of An Array In Ruby



When creating an array in Ruby one thing you might want to do is to get the length of that array. In most other programming languages this can be a rather drawn out affair. However, this is not the case with ruby. Getting the length of an array is a snap. First enter into irb at the command line and set up your array as shown below:

 my_array = ['element1','element2','element3']

Now you get the length of the array by simply by adding the 'length' operator onto your array.

puts my_array.length

Now hit enter to get the result:

3

And that's all there is to getting the length of an array in Ruby.


No comments:

Post a Comment

Post a Comment