Fun with function definitions

A

Anders Höckersten

--=-qYJr1ki3wJSOCpLEDMiR
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Me and a friend realised earlier today that this is actually a legal
Ruby program:
foo(def foo(a=3Dnil)
puts "hello"
end)
foo

The fact that this works seems to be a side effect of Ruby's semantics
rather than an intended feature, but maybe it's actually useful to be
able to do this? I can't think of any (practical) use of it -
suggestions are welcome.

Regards,
Anders

--=-qYJr1ki3wJSOCpLEDMiR
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: Detta =?ISO-8859-1?Q?=E4r?= en digitalt signerad
meddelandedel

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDWAOo9vwv2G5xhTwRAgs/AKCi748fp4WJ4YNOrAQYdxqRJ57fTgCfbbo2
0NiBcZVUHmfY3W4S8XfGUPw=
=MMUk
-----END PGP SIGNATURE-----

--=-qYJr1ki3wJSOCpLEDMiR--
 
M

Mike Wilson

Me and a friend realised earlier today that this is actually a legal
Ruby program:
foo(def foo(a=3Dnil)
puts "hello"
end)
foo

The fact that this works seems to be a side effect of Ruby's semantics
rather than an intended feature, but maybe it's actually useful to be
able to do this? I can't think of any (practical) use of it -
suggestions are welcome.

Regards,
Anders

That's pretty interesting. It makes for some interesting recursion too :)

irb(main):021:0> foo(def foo(a=3Dnil)
irb(main):022:2> puts a
irb(main):023:2> a +=3D 1 if not a.nil?
irb(main):024:2> return a
irb(main):025:2> end)
nil
=3D> nil
irb(main):026:0> foo(foo(foo(foo(foo(foo(1))))))
1
2
3
4
5
6
=3D> 7
 
S

Stefan Holst

Anders said:
Me and a friend realised earlier today that this is actually a legal
Ruby program:
foo(def foo(a=3Dnil)
puts "hello"
end)
foo
=20
The fact that this works seems to be a side effect of Ruby's semantics
rather than an intended feature, but maybe it's actually useful to be
able to do this? I can't think of any (practical) use of it -
suggestions are welcome.
=20
Regards,
Anders

neat. i experimented a bit an found this one:

irb(main):027:0> foo def foo(o); o+'o'; end || foo('f')
=3D> "foo"

interestingly, 'or' and || are not the same:

irb(main):030:0> nil or 'fo'
=3D> "fo"
irb(main):031:0> nil || 'fo'
=3D> "fo"
irb(main):032:0> foo(nil or 'fo')
SyntaxError: compile error
(irb):32: syntax error
foo(nil or 'fo')
^
(irb):32: syntax error
from (irb):32
from (null):0
irb(main):033:0> foo(nil || 'fo')
=3D> "foo"

'or' works however with an extra parenthesis:

irb(main):034:0> foo((nil or 'fo'))
=3D> "foo"

and even

irb(main):035:0> foo((nil; 'fo'))
=3D> "foo"

ry
stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Once again: Interest in "Cologne/Bonn Ruby Users"? 1
Array#unzip 0
More fun with C 4
[ANN] fastthread-0.6.3 1
Ruby Planet, anyone? 11
Block that takes a block 1
[ANN] XMPP4R-0.3.1 0
[ANN] lazy.rb 7

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top