Fun with function definitions

Discussion in 'Ruby' started by Anders Höckersten, Oct 20, 2005.

  1. --=-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--
    Anders Höckersten, Oct 20, 2005
    #1
    1. Advertising

  2. Anders Höckersten

    Mike Wilson Guest

    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
    Mike Wilson, Oct 21, 2005
    #2
    1. Advertising

  3. Anders Höckersten

    Mike Wilson Guest

    Err nevermind... that's just not unusual at all...
    Mike Wilson, Oct 21, 2005
    #3
  4. Anders Höckersten

    Stefan Holst Guest

    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
    Stefan Holst, Oct 21, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    65
    Views:
    1,728
    Mabden
    May 18, 2004
  2. Hongzheng Wang
    Replies:
    8
    Views:
    297
    Irrwahn Grausewitz
    Jun 9, 2004
  3. Hallvard B Furuseth

    imports after function definitions?

    Hallvard B Furuseth, Aug 28, 2003, in forum: Python
    Replies:
    4
    Views:
    301
    Hallvard B Furuseth
    Sep 1, 2003
  4. dolphin
    Replies:
    4
    Views:
    315
    Jorgen Grahn
    Aug 25, 2007
  5. er
    Replies:
    2
    Views:
    499
Loading...

Share This Page