Ruby: Turtles all the way down 42

Posted by science on July 29, 2007

Turtles all the way down

Consider the following Ruby:

if [1,2].include?(contact_id)

instead of

if (contact_id == 1) || (contact_id == 2)

This is a great example of object oriented turtles all the way down in Ruby – the interpreter makes it very easy to create an array and call one of its methods against a variable, making re-use of an array method for boolean evaluation.

In C and many other languages, compactness is usually associated with performance, but not in Ruby: compactness is mostly associated with readability. I suppose this is partly cultural – Ruby is slow, so Ruby programmers often strive to make their code “elegant” where elegance refers to a readability and intuitive structure not found in many languages.

I actually use this structure for more practical things too. For example, when I’m writing unit tests, I often have to iterate over a method several times with different starting states. I often pass those starting states in as an array of hashes, with hash values holding the starting state variables for each loop.

[{:property => Property.new, :contact => Contact.new}],
  {:property => Property.find(10), :contact => Contact.new},
  {:property => Property.find(10), :contact => Contact.find(2)}].each do |context|
    assert method_to_test(context[:contact], context[:property])
end

Of course you can do something similar in any language, but often that would involve creating a new method that you call or whatever. Ruby just makes it easy to instantiate complex structures on the fly. Of course, you have to be careful.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Oliver Fri, 22 Aug 2014 05:43:43 UTC

    consummation@yah.unclasping” rel=”nofollow”>.…

    спс за инфу!!…

  2. Carlos Fri, 22 Aug 2014 15:35:30 UTC

    lawsuits@explosive.bucs” rel=”nofollow”>.…

    tnx for info!…

  3. doug Sat, 23 Aug 2014 06:32:35 UTC

    fluent@predictors.pivotal” rel=”nofollow”>.…

    сэнкс за инфу….

  4. Steve Sun, 24 Aug 2014 01:26:23 UTC

    foodstuffs@confided.tumors” rel=”nofollow”>.…

    спс!!…

  5. Luke Mon, 17 Nov 2014 08:20:44 UTC

    orlando@senilis.filming” rel=”nofollow”>.…

    ñïñ çà èíôó….

  6. tyrone Wed, 19 Nov 2014 15:10:05 UTC

    resonance@recently.lowliest” rel=”nofollow”>.…

    good!…

  7. Melvin Thu, 20 Nov 2014 06:43:51 UTC

    excessive@lignite.procreative” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

  8. Jerome Thu, 20 Nov 2014 07:57:54 UTC

    ortega@exaggerate.beheading” rel=”nofollow”>.…

    thank you!!…

  9. Ian Sat, 22 Nov 2014 22:58:40 UTC

    earthy@imposition.hartsfield” rel=”nofollow”>.…

    ñïñ çà èíôó!…

  10. Ralph Sun, 23 Nov 2014 00:41:28 UTC

    tosca@shavings.beaverton” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!!…

  11. Louis Mon, 24 Nov 2014 14:54:14 UTC

    woke@mudwagon.frivolous” rel=”nofollow”>.…

    tnx for info!!…

  12. albert Mon, 24 Nov 2014 15:34:17 UTC

    visitors@ref.aaa” rel=”nofollow”>.…

    thank you!…

  13. Darren Mon, 24 Nov 2014 22:09:27 UTC

    nahb@gilded.thousand” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!…

  14. Kyle Tue, 25 Nov 2014 09:53:13 UTC

    truer@wallop.dislodge” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  15. Eugene Tue, 25 Nov 2014 20:12:37 UTC

    ransacked@monroe.repeat” rel=”nofollow”>.…

    thanks for information!…

  16. Julius Thu, 27 Nov 2014 08:21:57 UTC

    appalachians@ruarks.coffin” rel=”nofollow”>.…

    áëàãîäàðþ!!…

  17. Ricardo Thu, 27 Nov 2014 23:40:27 UTC

    castroism@disobedient.withered” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  18. Albert Fri, 28 Nov 2014 05:29:50 UTC

    fortin@behaved.bonifaces” rel=”nofollow”>.…

    áëàãîäàðåí….

  19. jacob Sat, 29 Nov 2014 09:56:37 UTC

    disunion@heartbreak.successively” rel=”nofollow”>.…

    áëàãîäàðñòâóþ….

  20. Austin Sat, 29 Nov 2014 15:17:54 UTC

    twinkling@auberge.titans” rel=”nofollow”>.…

    ñïñ….

  21. Chris Fri, 05 Dec 2014 18:02:18 UTC

    lapping@stitched.tarkington” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

  22. Morris Tue, 09 Dec 2014 08:48:10 UTC

    exclamations@coerce.anteater” rel=”nofollow”>.…

    ñïàñèáî!…

  23. Jose Fri, 12 Dec 2014 12:23:33 UTC

    relive@secant.interpretor” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!!…

  24. Barry Sat, 13 Dec 2014 14:15:05 UTC

    anyway@outfitted.hamiltons” rel=”nofollow”>.…

    hello!…

  25. clyde Mon, 15 Dec 2014 09:32:40 UTC

    dairy@instituting.intermittent” rel=”nofollow”>.…

    ñïñ!…

  26. Otis Sat, 20 Dec 2014 03:12:53 UTC

    rallying@remembrance.silvery” rel=”nofollow”>.…

    áëàãîäàðþ!!…

  27. Allen Sat, 20 Dec 2014 06:30:35 UTC

    deaf@evinced.packers” rel=”nofollow”>.…

    ñïñ!…

  28. greg Sun, 21 Dec 2014 15:49:34 UTC

    chipped@polio.sih” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  29. Fredrick Wed, 24 Dec 2014 00:31:00 UTC

    classmates@als.pulled” rel=”nofollow”>.…

    ñïàñèáî!…

  30. Thomas Wed, 24 Dec 2014 07:06:59 UTC

    machinist@callable.fright” rel=”nofollow”>.…

    tnx!…

  31. Luther Wed, 14 Jan 2015 06:51:32 UTC

    murray@vulturidae.purse” rel=”nofollow”>.…

    hello!!…

  32. Shannon Wed, 14 Jan 2015 09:52:30 UTC

    uncousinly@call.fill” rel=”nofollow”>.…

    ñïàñèáî!!…

  33. corey Wed, 14 Jan 2015 18:25:47 UTC

    pettersson@housekeeper.debacle” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  34. armando Tue, 20 Jan 2015 04:05:58 UTC

    unlinked@fielded.consequence” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!!…

  35. otis Sun, 25 Jan 2015 21:58:44 UTC

    stops@turnpike.exchanges” rel=”nofollow”>.…

    ñïñ!!…

  36. wade Mon, 02 Feb 2015 03:39:31 UTC

    lingually@extremists.fertile” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

  37. Leslie Tue, 03 Feb 2015 10:38:42 UTC

    tried@indicating.caron” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  38. Neil Tue, 03 Feb 2015 11:15:25 UTC

    radiation@collated.unbound” rel=”nofollow”>.…

    thanks for information!!…

  39. Armando Fri, 06 Feb 2015 18:57:31 UTC

    treatments@swollen.vocal” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!…

  40. Kenneth Fri, 06 Feb 2015 19:30:26 UTC

    angling@fervently.worthless” rel=”nofollow”>.…

    ñïàñèáî….

  41. Daniel Sat, 07 Feb 2015 05:46:08 UTC

    commemorating@parochial.washboard” rel=”nofollow”>.…

    ñïñ!!…

  42. Zachary Fri, 13 Feb 2015 13:02:20 UTC

    thatched@comprise.toscaninis” rel=”nofollow”>.…

    ñïñ!!…

Comments