Access an array in more readable way

Although this is an old topic, I'd like to introduce the library which I've written for rubyists all over the world anyway.

Someone may remember DHH's legendary commit to the rails repository.

https://github.com/rails/rails/commit/22af62cf486721ee2e45bb720c42ac2f4121faf4

+ # Equal to self[1]
+ def second
+   self[1]
+ end
+
+ # Equal to self[2]
+ def third
+   self[2]
+ end
+
..snip..
+
+ # Equal to self[8]
+ def ninth
+   self[8]
+ end
+
+ # Equal to self[9]
+ def tenth
+   self[9]
+ end 


To tell the truth, I was disappointed by DHH. I couldn't believe this was done by him. He is always extreme. Why are there methods only for up to tenth? I'm eager to access an array in such readable way for over 11th items.

So, I did.

$ irb 
>> require 'array_ordinal_accessor'
=> true
>> array = (1..200000).to_a
..snip..
>> array.seventy_third
=> 73
>> array.three_hundred_and_nineth
=> 309 
>> array.one_hundred_thirty_two_thousand_six_hundred_and_forty_eighth
=> 132648


Of course, you can assign new value in the same way.

>> array.fifty_thousand_six_hundred_and_fifty_eighth
=> 50658
>> array.fifty_thousand_six_hundred_and_fifty_eighth = 'hello'
=> "hello"
>> array.fifty_thousand_six_hundred_and_fifty_eighth
=> "hello"


Do you want to know how large indexed item you can access in this way? That's a good question.

This is the maximum number the library can handle:

>> array.nine_hundred_ninety_nine_noventrigintillion_nine_hundred_ninety_nine_octotrigintillion_nine_hundred_ninety_nine_septentrigintillion_nine_hundred_ninety_nine_sestrigintillion_nine_hundred_ninety_nine_quinquatrigintillion_nine_hundred_ninety_nine_quattuortrigintillion_nine_hundred_ninety_nine_trestrigintillion_nine_hundred_ninety_nine_duotrigintillion_nine_hundred_ninety_nine_untrigintillion_nine_hundred_ninety_nine_trigintillion_nine_hundred_ninety_nine_novemvigintillion_nine_hundred_ninety_nine_octovigintillion_nine_hundred_ninety_nine_septemvigintillion_nine_hundred_ninety_nine_sesvigintillion_nine_hundred_ninety_nine_quinquavigintillion_nine_hundred_ninety_nine_quattuorvigintillion_nine_hundred_ninety_nine_tresvigintillion_nine_hundred_ninety_nine_duovigintillion_nine_hundred_ninety_nine_unvigintillion_nine_hundred_ninety_nine_vigintillion_nine_hundred_ninety_nine_novemdecillion_nine_hundred_ninety_nine_octodecillion_nine_hundred_ninety_nine_septendecillion_nine_hundred_ninety_nine_sexdecillion_nine_hundred_ninety_nine_quindecillion_nine_hundred_ninety_nine_quattuordecillion_nine_hundred_ninety_nine_tredecillion_nine_hundred_ninety_nine_duodecillion_nine_hundred_ninety_nine_undecillion_nine_hundred_ninety_nine_decillion_nine_hundred_ninety_nine_nonillion_nine_hundred_ninety_nine_octillion_nine_hundred_ninety_nine_septillion_nine_hundred_ninety_nine_sextillion_nine_hundred_ninety_nine_quintillion_nine_hundred_ninety_nine_quadrillion_nine_hundred_ninety_nine_trillion_nine_hundred_ninety_nine_billion_nine_hundred_ninety_nine_million_nine_hundred_ninety_nine_thousand_nine_hundred_and_ninety_ninth


I believe that this library can make all ruby codes much more readable.

Let's download and try this!
http://github.com/technohippy/ordinal-accessors-for-an-array-object



This is the 10th entry for Ruby Advent Calendar jp-en: 2010. The previous entry was written by takano32 and the next will be written by tenderlove known as タコ焼き仮面.