Skip to content

zeninpalm/probably

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Probably

Probably is an extremely lightweight and simple tool. It only exists for one reason: to avoid checking for nil or exception in a calls chain. So, instead of writing like this:

visitors.most_recent.try(:last).first_name || "NoSuchAnUser"

you write like this

visitors.probably.most_recent.last.first_name.or("NoSuchAnUser")

or in a more natural way:

probably(visitors).most_recent.last.first_name.or("NoSuchAnUser")

Install

gem install probably

or in your Gemfile:

gem "probably"

or like this

gem "probably", :git => "git://github.com/zeninpalm/probably.git"

Usage

The simplest scenario

class Statistics
  include Probably
  attr_reader :visitors

  def latest_visitor
    probably(visitors).most_recent.last.first_name.or("NoSuchAnUser")
  end
end

end

or

class Foo
  include Probably
  
  def bar
    probably(nil).some_message.or { 5 / 0 }
  end
end

you many want to check probably_spec.rb for more examples, they are quite simple and intutive!

About

Enables you to chain calls without explicitly checking nils or exceptions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages