James Gray
3/14/2006 9:16:00 PM
On Mar 14, 2006, at 3:03 PM, Pavel Smerk wrote:
> Hi, I'm new to this language and as I'm Perl user, some things
> seems strange to me:
>
> = %w{a b} produces ['a', 'b']. Is there some similarily easy way
> for {'a' => 'b'}? Or, can I transform an array to some "list"? I
> can use Hash['a', 'b'], but not Hash[%w{...}], because I cannot
> generate a list, only an array.
You are looking for the "splat" operator:
>> Hash[*%w{a b}]
=> {"a"=>"b"}
> = how can I do 'perlish' a[1] <=> b[1] || a[2] <=> b[2] if I want
> compare a and b accordind to some my own rules, i.e. if a[1] == b
> [2], "return" a[2] <=> b[2]? In Ruby this is not possible, because
> 0 is true.
We use sort_by() for that:
>> %w{one two three}.sort_by { |str| [-str.length, str] }
=> ["three", "one", "two"]
> = can I somehow make ruby produce warnings on 1 == '1' (number ==
> string) like comparisons? In Perl true, in Ruby false. Many my
> mistakes are of this kind and as these values seems same on
> output. ;-)
Hmm, you could redefine ==(), but you don't want to do that, trust
me. ;) The transition phase will pass in time...
> = why I can use {|...| ...} as argument for map, each etc., but I
> cannot write foo = {|...| ...}, though I can write bar = [...] or
> bar = {...}?
You can use lambda() for this:
proc_object = lambda { |...| ... }
James Edward Gray II