{"id":16659338,"url":"https://github.com/bhb/maybe","last_synced_at":"2025-07-29T03:35:01.132Z","repository":{"id":788133,"uuid":"484344","full_name":"bhb/maybe","owner":"bhb","description":"The Maybe monad for Ruby","archived":false,"fork":false,"pushed_at":"2018-01-01T19:32:35.000Z","size":25,"stargazers_count":50,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-26T22:57:10.729Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bhb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-01-22T20:52:40.000Z","updated_at":"2024-09-15T20:26:21.000Z","dependencies_parsed_at":"2022-08-16T10:55:06.172Z","dependency_job_id":null,"html_url":"https://github.com/bhb/maybe","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bhb/maybe","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bhb%2Fmaybe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bhb%2Fmaybe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bhb%2Fmaybe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bhb%2Fmaybe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bhb","download_url":"https://codeload.github.com/bhb/maybe/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bhb%2Fmaybe/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267455576,"owners_count":24090008,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T10:24:43.532Z","updated_at":"2025-07-29T03:35:01.044Z","avatar_url":"https://github.com/bhb.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"maybe\n=====\n\nA library for treating nil and non-nil objects in a similar manner. Technically speaking, Maybe is an implemenation of the maybe monad.\n\nSynopsis\n--------\n\nThe Maybe class wraps any value (nil or non-nil) and lets you treat it as non-nil.\n\n    require \"maybe\"\n    \"hello\".upcase                         #=\u003e \"HELLO\"\n    nil.upcase                             #=\u003e NoMethodError: undefined method `upcase' for nil:NilClass\n    Maybe.new(\"hello\").upcase.__value__    #=\u003e \"HELLO\"\n    Maybe.new(nil).upcase.__value__        #=\u003e nil\n\nYou can also use the method `Maybe` for convenience. The following are equivalent:\n\n    Maybe.new(\"hello\").__value__           #=\u003e \"hello\"\n    Maybe(\"hello\").__value__               #=\u003e \"hello\"\n     \nYou can also optionally patch `Object` to include a `#maybe` method:\n\n    require \"maybe/core_ext\"\n    \"hello\".maybe.upcase                   #=\u003e \"HELLO\"\n   \nWhen you call `Maybe.new` with a value, that value is wrapped in a Maybe object. Whenever you call methods on that object, it does a simple check: if the wrapped value is nil, then it returns another Maybe object that wraps nil. If the wrapped object is not nil, it calls the method on that object, then wraps it back up in a Maybe object. \n\nThis is especially handy for long chains of method calls, any of which could return nil.\n\n    # foo, bar, and/or baz could return nil, but this will still work\n    Maybe.new(foo).bar(1).baz(:x)\n\nHere's a real world example. Instead of writing this:\n\n    if(customer \u0026\u0026 customer.order \u0026\u0026 customer.order.id==newest_customer_id)\n      # ... do something with customer\n    end\n\njust write this:\n\n    if(Maybe.new(customer).order.id.__value__==newest_customer_id)\n      # ... do something with customer\n    end\n\nIf your wrapped object does not have a `#value` method, you can call\n\n    Maybe.new(obj).value\n\ninstead of\n\n    Maybe.new(obj).__value__\n\nExamples\n--------\n\n    require \"maybe\"\n\n    Maybe.new(\"10\")                    #=\u003e A Maybe object, wrapping \"10\"\n  \n    Maybe.new(\"10\").to_i               #=\u003e A Maybe object, wrapping 10\n  \n    Maybe.new(\"10\").to_i.__value__     #=\u003e 10\n  \n    Maybe.new(nil)                     #=\u003e A Maybe object, wrapping nil \n  \n    Maybe.new(nil).to_i                #=\u003e A Maybe object, still wrapping nil\n  \n    Maybe.new(nil).to_i.__value__      #=\u003e nil\n\nRelated Reading\n---------------\n\n* [MenTaLguY has a great tutorial on Monads in Ruby over at Moonbase](http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/00introduction.html)\n* [Oliver Steele explores the problem in depth and looks at a number of different solutions](http://osteele.com/archives/2007/12/cheap-monads)\n* [Reg Braithwaite explores this same problem and comes up with a different, but very cool solution in Ruby](http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html)\n* [Weave Jester has another solution, inspired by the Maybe monad](http://weavejester.com/node/10)\n\nNote on Patches/Pull Requests\n------\n \n* Fork the project.\n* Make your feature addition or bug fix.\n* Add tests for it. This is important so I don't break it in a\n  future version unintentionally.\n* Commit, do not mess with rakefile, version, or history.\n  (if you want to have your own version, that is fine but\n  bump version in a commit by itself I can ignore when I pull)\n* Send me a pull request. Bonus points for topic branches.\n\nCopyright\n----\n\nCopyright (c) 2009-2018 Ben Brinckerhoff. See LICENSE for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbhb%2Fmaybe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbhb%2Fmaybe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbhb%2Fmaybe/lists"}