I am a web developer working with
SlideShare, Delhi. This is my Website.

YAML Oddity for Ruby arrays


07.02.10 Posted in Blog by admin

I discovered this interesting nugget of oddity today. While normal Ruby arrays are ‘comma separated” and space agnostic ie.

>> array = [2,3 ,4, 5]
=> [2, 3, 4, 5]
>> array.size
=> 4
>> array_spaces = [2 , 3 , 4 , 5]
=> [2, 3, 4, 5]
>> array_spaces.size
=> 4

In case of YAML, the spaces between array elements makes a difference. A much needed code snippet :

A file ‘myfile.yml’

array: [ 1 , 2 ,3, 4]
array_spaces: [1 , 2 , 3 , 4]

Loading this file in ‘irb’:

>> f =YAML.load_file('myfile.yml')
=> {"array"=>[1, "2 ,3", 4], "array_spaces"=>[1, 2, 3, 4]}
>> f["array"]
=> [1, "2 ,3", 4]
>> f["array"].size
=> 3
>> f["array_spaces"]
=> [1, 2, 3, 4]
>> f["array_spaces"].size
=> 4

Now that’s not Ruby-ish. But then, as a seasoned co-developer pointed out, YAML should be language agnostic. I still find it a bit odd, though. Hmmm, interesting. Point taken.



7 Responses to “YAML Oddity for Ruby arrays”

  1. skuteczne pozycjonowanie says:

    Great text and nice blog.

  2. pharmacy technician says:

    Keep posting stuff like this i really like it

  3. Wordpress Themes says:

    Genial fill someone in on and this mail helped me alot in my college assignement. Thanks you as your information.

  4. knockoff gucci handbags says:

    Can I link this post from my blog? Hunter

  5. Pozycjonowanie Warszawa says:

    Interesting information, thanks that you wrote – I look forward to more.

  6. Horacio Cauthron says:

    Thanks for the tip!

    I am going to add your blog to my list!

Leave a Reply