Search code examples
rubynotation

How do I read square brackets in Ruby documentation


How do I read this argument list? Is this two values in the argument list or just one? What does the comma inside the bracket mean?

  fetch( key [,default] ) -> obj 

Solution

  • I don't see anywhere on that documentation site where the notation used for argument lists is explained. However, the square brackets are frequently used to mean "optional" in programming documentation. So this means that the fetch method can be called with just one argument, or two. If you pass two, obviously you would need a comma. So these two calls would be valid:

    a.fetch(k)
    a.fetch(k, 5)