From: Jason Roelofs Date: 2006-10-19T00:32:12+09:00 Subject: Re: My first feeling of Ruby ------=_Part_32673_24270391.1161185527622 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Frankly I never knew you could do "test #@@varname". I've always used the #{} syntax. As for part 2, that is purely an issue with the parser. hash = "A" => 1, "B" => 2 - Where would the parser end? Ruby allows this: a, b = 1, 2 so your attempt at assigning a hash as such is a syntax error, as you found out. As function calls however: def func1(options = {}); ... ; end you're allowed to do what seems to be the same thing because the parser is now grabbing parameters until the end of the line: func1 :key => '1', :key2 => '2' is the same as func1(:key => '1', :key2 => '2') is the same as func1( {:key => '1', :key2 => '2'} ) As for "Hmm why [ "A"=>1 , "B"=>2 ] silently produce [{"A"=>1, "B"=>2}] ?", the parser sees that you want an array. Arrays don't have keys and so for the same reason as the function parameter syntax, it sees key/value pairs and creates a hash. Jason On 10/18/06, 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. > > > 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, > > ------=_Part_32673_24270391.1161185527622--