From: Florent G. [mailto:[email protected]]
Today I’m learning Ruby.
Welcome to Ruby!
Why #test doesn’t work for local variables? It would be more
logic.
The general form for string interpolation is #{…}, where … can be
any ruby code. Because it’s so unlikely that you’d ever see #@ inside a
normal string, that is a special case that has a shortcut for it.
For example, the following string: “Use the String#split method.” might
be typed by a rubyist as an instruction…it would be unfortunate if
EVERY use of the # character needed to be escaped to prevent it from
being intpretted as a variable.
hastables is using a mix of {} and []. Always using {} or [] (but not
both) for hastable would be “errorless”.
I understand your perspective. I personally like that Arrays, and
Hashes, and (any other class) have a [] method available. (When you
write my_hash[“A”] you’re actually calling a method named “[]” and
passing in a parameter “A” to get a return value.) It’s consistent that
[] means “get the value with this index/key”.
When you are creating a hash or array from a literal, then you need some
way to distinguish them. I agree that it might be nice if [1,2,3] made
an array and [“a”=>1, “b”=>2] made a hash. Right now, that’s not how it
works. (Although in the future, we might see some form of the latter
for an ordered hash implementation. Just speculation, though, not a
promise.)
Hmm why [ “A”=>1 , “B”=>2 ] silently produce [{“A”=>1, “B”=>2}] ?
I suppose for consistency with the fact that:
my_method “a”=>1, “b”=>2
and
my_method( “a”=>1, “b”=>2 )
and
my_method( { “a”=>1, “b”=>2 } )
all create a hash that is passed as the first parameter to the method.
Or, maybe it’s a side effect of existing parsing rules. (I’m not sure on
the “why”.)