Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 if_not_ni
Index -> Programming, Ruby -> Ruby Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Fri Nov 23, 2007 2:35 pm   Post subject: if_not_ni

code:
class ElseHandler
    def otherwise(&blk)
        blk.call
    end
end

class Object
    def otherwise(&blk)
        self
    end

    def if_not_nil(&blk)
        if self.is_a? NilClass
            ElseHandler.new
        elsif blk
            yield self
        end
    end
end


And now...

code:
doSomethingThatReturnsNil.if_not_nil { |v| v.doSomething }.otherwise { doSomethingElse }


Can replace:

code:
tmp = doSomethingThatReturnsNil

if tmp
    tmp.doSomething
else
   doSomethingElse
end
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: