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.
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:
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}] ?
my 2c,
Florent,