Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Fun with function definitions

Reply
Thread Tools

Fun with function definitions

 
 
Anders Höckersten
Guest
Posts: n/a
 
      10-20-2005
--=-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--


 
Reply With Quote
 
 
 
 
Mike Wilson
Guest
Posts: n/a
 
      10-21-2005
On 10/20/05, Anders H=F6ckersten <> wrote:
> 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


 
Reply With Quote
 
 
 
 
Mike Wilson
Guest
Posts: n/a
 
      10-21-2005
Err nevermind... that's just not unusual at all...


 
Reply With Quote
 
Stefan Holst
Guest
Posts: n/a
 
      10-21-2005
Anders H=F6ckersten wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT Thursday, uh, fun, yeah, fun! Consultant MCSE 17 02-10-2007 03:39 AM
3 PIX VPN questions - FUN FUN FUN frishack@gmail.com Cisco 3 03-16-2006 02:25 PM
OT: Wednesday follow-up-to-Tuesday-Fun Fun Ken Briscoe MCSE 0 07-14-2004 01:41 PM
Programming is not as much fun/more fun than it used to be. Andy Fish Java 65 05-18-2004 08:24 PM
Fun fun fun Luke Computer Support 3 10-07-2003 03:45 PM



Advertisments