Pavel Smerk scribbled on Tuesday 14 Mar 2006 21:50:

> 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 can, however, expand the array:
 Hash[*%w{a b c d}] yields {"a"=>"b", "c"=>"d"}

> = 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.

irb(main):006:0> 0 == true
=> false

0 is not true :)

> = 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. ;-)

Not that I know of. Well, you could extend the == operator on Fixnum (and
String) to throw a warning:

class Fixnum
  def ==(a)
    warn "Warn" unless a.is_a? Fixnum
    super(a)
  end
end

> = why I can use {|...| ...} as argument for map, each etc., but I cannot
> write foo = {|...| ...}, though I can write bar = [...] or bar = {...}?

You can't use {} literals in an assignment to denote a block because Ruby
thinks it is supposed to be a Hash. Use Kernel#proc (or the alias #lambda)
for that:

irb(main):026:0> p = proc {|n| puts n}
=> #<Proc:0xb7c98138@(irb):26>
irb(main):027:0> p.call(5)
5

You can pass that block to any method that wants a block (note the &
operator that denotes the passed argument is a block):

irb(main):029:0> [33, 44, 55].each &p
33
44
55

See also Method#block_given?

Hope that helps.