Florent Guiliani 작성:
> [] 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 {}?
Yes, you can omit {} for method argument in some condition.
hash = [ "A"=>1 , "B"=>2 ]
hash = Array.new << {"A"=>1 ,
"B"=>2}
hash = Array.new.push("A"=>1,
"B"=>2)
Third form is regular. Think first and second case is
special thing.
quote in PickAxe book(Programming Ruby):
>Collecting Hash Arguments
>...snip
>You can place key => value pairs in an argument list,
as
>long as they follow any normal arguments and precede any
array and block arguments.
>All these pairs will be collected into a single hash and
passed as one argument to the
>method.
I think this feature make method call interface more freely.
method_call :one => something
method_call :two => another
method_call :one => something, :two => another
method_call :two => another, :one => something, :three
=> this
|