[#3006] mismatched quotation — "stevan apter" <apter@...>

ruby documentation uses a punctuation convention i've never seen

13 messages 2000/05/27

[ruby-talk:02938] Re: Iterators...

From: Dave Thomas <Dave@...>
Date: 2000-05-23 03:25:40 UTC
List: ruby-talk #2938
"David Douthitt" <DDouthitt@cuna.com> writes:

> Brings up an interesting point: the code snippet:
> 
> { 1, 2, 3, }
> 
> is valid Perl code; is it valid Ruby code?  If not, why not?

Because {} is a Hash constructor, and hash elements must be
paired. (For that matter, it's illegal in Perl as well):

  dave[code/wm 22:22:56] perl -w -e '%a = {1, 2, 3, }'
  Odd number of elements in hash assignment at -e line 1.
  Reference found where even-sized list expected at -e line 1.

  dave[code/wm 22:23:05] ruby -e 'a = {1, 2, 3, }'
  -e:1: odd number list for Hash

However, the trailing comma is acceptable to Ruby otherwise:

  dave[code/wm 22:24:49] ruby -e 'p({1, 2, 3, 4, })'
  {1=>2, 3=>4}
  dave[code/wm 22:24:53] ruby -e 'p([1, 2, 3, 4, ])'
  [1, 2, 3, 4]



Regards


Dave

In This Thread

Prev Next