Florent Guiliani wrote:
> Hi all,
>
> Today I'm learning Ruby. Globally It's a great programming langage but
> I've noticed some irrationnal things:
>
> First thing:
> ============
> irb(main):138:0* class Toto
> irb(main):139:1>    @@test="a"
> irb(main):140:1>
> irb(main):141:1*    def test
> irb(main):142:2>      @test="b"
> irb(main):143:2>      test="c"
> irb(main):144:2>      puts "test #@@test"
> irb(main):145:2>      puts "test #@test"
> irb(main):146:2>      puts "test #test"    # #{test} instead #test why?
> irb(main):147:2>    end
> irb(main):148:1> end
> => nil
> irb(main):149:0> Toto.new.test
> test a
> test b
> test #test
> => nil
>
> Why #test doesn't work for local variables? It would be more logic. It's
> my point of view. We can have both #test and #{test} working.

I don't most of us use those shorcuts, prefering to alwasy use #{...}
notation:

  puts "test #{@@test}"
  puts "test #{@test}"
  puts "test #{test}"

> Second thing:
> =============
> hastables is using a mix of {} and []. Always using {} or [] (but not
> both) for hastable would be "errorless".
>
> Actually {} for initialisation:
> irb(main):150:0> hash = { "A"=>1 , "B"=>2 }
> => {"A"=>1, "B"=>2}
>
> [] for reading:
> irb(main):152:0> hash["A"]
> => 1
>
> If you go wrong:
> irb(main):156:0> hash{"A"}
> => 537984250
> irb(main):154:0> hash = [ "A"=>1 , "B"=>2 ]
> => [{"A"=>1, "B"=>2}]
>
> I would prefer an error or warning. The last line means I can omit {}?
> so let's go on:

I have to agree with you here. But the issue is a much larger one.
Because Ruby handles the trailing block argument as a special case it
is always an optional argument. So any method can take a block and not
raise an error:

  puts { blah!.nothing.matters } => "\n"

So in your case you are actually calling the #hash method which
actually has nothing to do with a Hash class, and your throwing in a
meaningless block to go with it.

I'd rather have blocks be passed in as the trailing argument
regardless, such that the special '&argument' were not needed at all.
This would improve method interfaces.

On another note, I always thought it would be nice to have a merged
Hash/Array, call it "Collection" if you will, such that

  c = Collection.new
  c[index]  # lookup by index
  c{key}    # lookup by key

But that notation might cause other conflicts.

> irb(main):155:0> hash = "A"=>1 , "B"=>2
> SyntaxError: compile error
> (irb):155: parse error, unexpected tASSOC, expecting $
> hash = "A"=>1 , "B"=>2
>              ^
>          from (irb):155
>          from :0
> irb(main):156:0>
>
> Hmm why [ "A"=>1 , "B"=>2 ] silently produce [{"A"=>1, "B"=>2}] ?

Any method can take hash elements which are "silently" converted to a
hash. So:

  def foo(arg)
    p arg
  end

  foo( "A"=>1 , "B"=>2 )  #=> {"A"=>1 , "B"=>2}

The same is true for #[] which is also a method!

Some of us howerver would like to see [ "A"=>1 , "B"=>2 ] be
interpreted as an ordered hash instead if reasonably possible.

HTH,
T.