I really like Smalltalk’s ifNil: and ifNotNil:, and up to now, I could not use these in Ruby. Fortunately, Bob Hutchison came to the rescue with A Little Unnecessary Smalltalk Envy.

I immediately copied that to XLsuite and wrote a couple of tests. Here is sample of what the code feels like:

Oh well, no Sam in my database. The code is here:

7 Responses to “A little Smalltalk in Ruby: #if_nil and #if_not_nil”

  1. Bob Hutchison Says:

    Wow! Unit tests! You’ve raised the bar on these little doodads. I’m going to have to start using them in the future. Some of these things, even though tiny are still seriously useful and deserve the tests.

  2. szeryf Says:

    nice. mind if I steal it? :)

  3. Andy Stewart Says:

    Would #unless_nil be more idiomatic Ruby than #if_not_nil?

    And while I’m being pedantic, how about #if_nil? instead of #if_nil and #unless_nil? instead of #unless_nil …?

    Nice technique though.

  4. macournoyer Says:

    why not just do:

    Party.find_by_email_address(“sam@gamgee.net”) && puts(“Found Sam!”)

    ?

  5. David Says:

    @Andy

    ‘unless’ and ‘if not’ are both widely used in ruby and in this case are equivalent. Just because ‘unless’ is a unique feature of the language doesn’t make it more idiomatic for ruby.

    the convention of adding question-marks to the end of methods is to suggest it returns a boolean value, which these do not.

  6. pete Says:

    @macournoyer:

    I prefer:

    puts(“Found Sam!”) unless Party.find_by_email_address(“sam@gamgee.net”).nil?

    or for a multi-liner:

    unless Party.find_by_email_address(“sam@gamgee.net”).nil? puts(“Found Sam!”) end

    1. other stuff
  7. Lawrence Pit Says:

    @macournoyer: the example could’ve been better. I think the idea is that instead of:

    x = Party.find_by_email(“sam@gamgee.net”)
    name = x && x.name

    you could do:

    name = Party.find_by_email(“sam@gamgee.net”).if_not_nil { |p| p.name }

    or instead of:

    x = Party.find_by_email(“sam@gamgee.net”)
    name = x && parties.find_like( x )

    you could do:

    name = Party.find_by_email(“sam@gamgee.net”).if_not_nil { |p| parties.find_like(p) }

Leave a Reply

 

Search

A picture of me

I am François Beausoleil, a Ruby on Rails coder. During the day, I work on XLsuite. At night, I am interested many things. Read my biography

Tags

(4) (1) (1) (1) (1) (2) (1) (1) (1) (2) (2) (1) (2) (1) (3) (1) (2) (1) (1) (1) (1) (2) (14) (1) (1) (1) (1) (2) (1) (1) (2) (0) (1) (4) (1) (3) (1) (1) (1) (1) (1) (1) (0) (3) (2) (1) (2) (1) (3) (1) (5) (2) (10) (10) (11) (14) (2) (1) (3) (1) (1) (1) (1) (1) (0) (1) (2) (2) (2) (1) (1) (1) (4) (1) (3) (1) (4) (2) (2) (25) (2) (1) (1) (0) (1) (1) (1) (23) (25) (1) (1) (13) (1) (1) (1) (4) (5) (1) (1) (1) (4) (1) (2) (3) (4) (4) (1) (1) (1) (8) (3) (1) (5) (5) (2) (2) (2) (4) (8) (7) (1) (1) (1) (1) (2) (4) (1) (4) (12) (2) (1) (2) (4) (1) (1) (1) (2) (8) (2) (3) (2) (2) (1) (3) (1) (1)

Links

Projects I work on

Categories

Archives