Sha256: 3be898542b65ce3bfb9b09099053f33a282fc8a93352f704ee0353f41a2df217
Contents?: true
Size: 1.76 KB
Versions: 1
Compression:
Stored size: 1.76 KB
Contents
# HoboSupport HoboSupport is a mixed bag of core ruby extensions that have been extracted from the [Hobo][] project [Hobo]: http://hobocentral.net doctest_require: '../lib/hobosupport' {.hidden} >> HoboSupport::VERSION => "0.9.101" ## Contents * [Enumerable](/manual/hobosupport/enumerable) * [Hash](/manual/hobosupport/hash) * [Implies](/manual/hobosupport/implies) * [Metaid](/manual/hobosupport/metaid) * [Methodphitamine](/manual/hobosupport/methodphitamine) * [Module](/manual/hobosupport/module) ## Object extensions ### `Object#is_one_of?` Like `is_a?` but multiple types to be checked in one go >> "foo".is_one_of?(String, Symbol) => true >> :foo.is_one_of?(String, Symbol) => true >> 1.is_one_of?(String, Symbol) => false ## Method call extensions We have the "." operator to call methods on objects. These extensions introduce two "special dots". ### `Object#_?` "`._?.`" only calls the method if the receiver is not `nil`. Otherwise it returns nil. >> "foo"._?.length => 3 >> nil._?.length => nil `to_s`, `to_yaml` and `to_json` and a few system functions that start with an underscore are the only exceptions -- they call their corresponding functions on nil. >> nil._?.to_s => "" ### `Object#try` "`.try`" only calls the method if the receiver responds to that method. >> "foo".try.reverse => "oof" >> :foo.try.reverse => nil ### What's the difference? `_?` is generally used when nil is an expected response. `try` is generally used when interfaces may be different. For instance, you may use `try` to call a Rails 2.3 function that doesn't exist on Rails 2.2. nil responds to some functions that may surprise you. >> nil.try.to_i => 0 >> nil._?.to_i => nil
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hobosupport-0.9.101 | test/hobosupport.rdoctest |