Getting started with Ruby – Part 3 – Collections, Ranges & Arrays

In my previous article Getting started with Ruby – Part 2 I introduced you all to Ruby & its philosophy and I started out with data type classes in Ruby. I was able to give you an overview about Strings & Numbers in ruby. Today I will start with Collections.

  • The Range – As the name suggests, ranges hold a sequential collection of numbers. Defining a range in Ruby is extremely simple, instead of explaining, i’ll show it to you. Don’t be afraid to start an IRB shell to try the code out.

    From the examples above, it should be clear what the syntax to define a range is, although you might be confused regarding the difference in line 1 & line 2, I can assure you it’s not a typo. Ruby provides 2 ways to create ranges, one using two periods (..) and one using triple periods (…), the difference being full_range = 1..10 is a range which starts from value 1 and ends at value 10, whereas loop_range = 0…10 is a range that starts from value 0 and ends at value 9.

    Here I have also introduced you to 3 methods of the range ==, eql? & include?, I am sure that the methods names are quite self explanatory. Although I would like to bring your attention towards a convention that is followed in ruby for method names. Method names in Ruby may end in =, ? or !. As a convention we end a method name in ? to indicate that the method will be returning a boolean value, i.e. true or false, we end a method name in = to indicate that it is a setter method used for assignment operations, and we end a method name in ! to indicate that the execution of the method will alter the state / value of the caller object. We will get into details of methods later again when we would be digging into classes.

  • Array – These are simple indexed or ordered collection of elements or objects. If you have worked in C/C++/Java, Arrays are similar in concept except for the fact that the various elements within the Array need not be of the same type, they could be of any type. Let me give you some examples to make it more clear :

    The above are various ways in which one can create & access Arrays in Ruby, I think most of them are pretty self explanatory, there may be some minor constructs here that you may not understand, but I will be going through them more in detail later on, for now lets concentrate on just Arrays.

    Let us now take a look at various ways to manipulate arrays :

    The above should give you a very good idea on how to create & manipulate arrays, the examples should be explanatory, I have also added helpful comments along.

    I had forgotten to demonstrate some really powerful features of arrays before hand. Let me do that now :

    Since the article is already very long, I will cut it short here and take up the last collection data type Hash in my next article. After that I will take you through various mechanisms for looping over collections in Ruby, which will apply to all collection data types.

    In Ruby there are always several ways to do one thing, the reason for that is because there are always more than one intuitive way to do the same thing. Different developers use different ways to do what they wish to as per what suits them best. I am sure you will get there too soon enough.

    Please do feel free to launch the IRB and try out various things that I have shown here, experiment & play around with it and I am sure you will have a nice time :). After I finish with covering the looping mechanisms for collections I will also write about how you can use IRB & Ruby’s reflective nature to inspect objects.

    I hope you find the series of articles easy to understand and that it helps you getting started with Ruby. In case you have any questions or suggestions, please do let me know by adding it in the comments below.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Leave a Reply