Tuesday, February 5, 2008

Code Samples From "Programming Ruby" That Made Me Stop, Look Up At the Ceiling, and Sigh Longingly (Part 1)

CHAPTER 4. CONTAINERS, BLOCKS, AND ITERATORS

PAGE 44

a = [ 1, 3, 5, 7, 9]

a[-1]    ->  9

a[-2]    ->  7

a[-99]   ->  nil

3 comments:

  1. Perhaps I am missing something - why does that make you sigh longingly. Is it the banality of the example?

    ReplyDelete
  2. No, that's not the case at all.

    Coming from a C#/Java/C background, I'm trying to highlight features I've discovered in Ruby that make me long for it.

    ReplyDelete
  3. Ahh... that makes sense. I'm a bit like that with C and Python.

    BTW, these examples give identical results in Python except for 'a[-99]' which raises an IndexError exception. I can see advantages to both approaches and both are better than a program crash!

    ReplyDelete

Note: Only a member of this blog may post a comment.