--jRdC2OsRnuV8iIl8
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Robert (bob.news / gmx.net) wrote:

> irb(main):144:0> procs = [
> irb(main):145:1*   Proc.new{|a| p a},
> irb(main):146:1*   Proc.new{|*a| p a},
> irb(main):147:1*   Proc.new{|a,| p a},
> irb(main):148:1*   Proc.new{|a,b| p a}
> irb(main):149:1> ]
> 
> 1) Why is it that "proc[0].arity = -1"?  Or put differently: To me it seems
> there is an asymmetry between proc[0].arity = proc[1].arity and the behavior
> of proc[0] that resembles more p[2].  If "proc[0].arity = 1" I would be
> fine, but -1 puzzles me...

-1 means 0 or more args, -2 means 1 or more args, etc

> 2) Why does this parse "Proc.new{|a,| p a}"? (the comma with following bar)

the |...| construct is pretty much the same as multiple assignment with
,

a,b,c = [1,2,3]

a, = [:a, :b, :c]

a is now :a, and :b and :c got thrown away.

You can even do stuff like:

class Foo
  attr_accessor :bar
end

f = Foo.new

[1,2,3].each do |f.bar|
end

f.bar == 3 #true

-- 
Eric Hodel - drbrain / segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04


--jRdC2OsRnuV8iIl8
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (FreeBSD)

iD8DBQE+VWEmMypVHHlsnwQRAhLRAKC8e9oAtLYDvDTghucRJGdswoQq5wCfT6fn
Tnk5Niy4PW832EH1t3GPYo0Tj
-----END PGP SIGNATURE-----

--jRdC2OsRnuV8iIl8--