{"id":15593413,"url":"https://github.com/laertispappas/ruby_utils","last_synced_at":"2025-08-25T01:33:56.537Z","repository":{"id":56893741,"uuid":"74917939","full_name":"laertispappas/ruby_utils","owner":"laertispappas","description":"Ruby core extensions and class utilities.","archived":false,"fork":false,"pushed_at":"2017-02-17T23:39:02.000Z","size":16,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-28T14:21:53.040Z","etag":null,"topics":["ruby"],"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/laertispappas.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":"2016-11-27T22:22:43.000Z","updated_at":"2019-01-30T13:01:14.000Z","dependencies_parsed_at":"2022-08-20T16:10:48.304Z","dependency_job_id":null,"html_url":"https://github.com/laertispappas/ruby_utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/laertispappas/ruby_utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laertispappas%2Fruby_utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laertispappas%2Fruby_utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laertispappas%2Fruby_utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laertispappas%2Fruby_utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laertispappas","download_url":"https://codeload.github.com/laertispappas/ruby_utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laertispappas%2Fruby_utils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271991235,"owners_count":24854735,"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-08-24T02:00:11.135Z","response_time":111,"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":["ruby"],"created_at":"2024-10-03T00:17:37.289Z","updated_at":"2025-08-25T01:33:56.507Z","avatar_url":"https://github.com/laertispappas.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ruby_utils\n\nVarious ruby core extensions and class utilities.\n\n## Status\nTBD\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'ruby_utils'\n```\n\nAnd then execute:\n\n  $ bundle\n\nOr install it yourself as:\n\n  $ gem install ruby-utils\n\n## Usage\nTBD\n\n\n## Features\n\n### Ruby::Utils::Option\nA scala like container for zero or one element. An option can be either Some[Object]\nor None object.\n\nCreating a Some or None option:\n```ruby\n  def find_person(id)\n    person = Person.find_by_id\n    person ? Some(person) : None\n  end\n\n  person = find_person(1)\n  person.class # None if not found else Some(person)\n```\n\n#### `#empty?` True if None, false if Some\n\n```ruby\n  none = None\n  none.empty? # =\u003e true\n  none.defined? # =\u003e false\n\n  some = Some(Class.new)\n  some.empty? # =\u003e false\n  some.defined? # =\u003e true\n```\n#### `#defined?` True if Some else false on None\n\n#### `get` Returns the option's value or an exception is raised on empty option.\n\n```ruby\n  none = None\n  none.get # =\u003e NoSuchElementError\n\n  some = Some(12)\n  some.get # =\u003e 12\n```\n\n#### `get_or_else(default)` Returns the option's value if the option is nonempty, otherwise return the result of evaluating `default`.\n\n```ruby\n  none = None\n  none.get_or_else(\"default\") # =\u003e 'default'\n\n\n  some = Some(12)\n  some.get_or_else(\"default\") # =\u003e 12\n```\n\n#### `map(f=nil, \u0026block)` Returns a Some containing the result of applying f / or evaluating the block to self option's value if this option is nonempty. Otherwise return None\n\n```ruby\n  f = -\u003e(x) { x*x }\n\n  none = None\n  none.map(f) #=\u003e None\n  none.map { |e| e* 100 } # -\u003e none\n\n  some = Some(10)\n  some.map(f) # =\u003e Some(100)\n  some.map { |e| e*2 } # =\u003e Some(20)\n```\n\n#### `flat_map(f, \u0026block)` If the option is nonempty, return a function applied to its value.\nOtherwise return None.\n\n```ruby\n  f = -\u003e(x) { x * 10 }\n  none = None\n  none.flat_map(f) # =\u003e None\n  none.flat_map { |a| a } # =\u003e None\n\n  some = Some(20)\n  some.flat_map(f) # =\u003e 200\n  some.flat_map { |e| e * 2 } # =\u003e 40\n```\n\n#### `filter(p=nil, \u0026block)` If the option is nonempty and the given predicate `p` yields `false` on its value, return `None`. Otherwise return the option value itself.\n\n```ruby\n  p = -\u003e(x) { x % 2 == 0 }\n\n  none = None\n  none.filter(p) # =\u003e None\n  none.filter { |a| a == 1 } # =\u003e None\n\n  some = Some(10)\n  some.filter(p) # =\u003e Some(10)\n\n  some = Some(3)\n  some.filter(p) # =\u003e None\n```\n\n#### `or_else(alternative)` If the option is nonempty return it, otherwise return the result of evaluating an alternative expression.\n\n```ruby\n  none = None\n  none.or_else(12) # =\u003e 12\n  none.or_else(Some(12)) # =\u003e Some(12)\n\n  some = Some(10)\n  some.or_else(111) # =\u003e Some(10)\n```\n\n#### `match` [Experimental] Provides pseudo pattern match for option classes (see also `List#match`)\n\n```ruby\n  def get(id)\n    if id == 1\n      None\n    else\n      Some(id)\n    end\n  end\n\n  some_value = get(1) # None\n  result = some_value.match {\n    on None =\u003e 'missing'\n    on Some(x) =\u003e x * 100\n  }\n\n  purs result # =\u003e 'missing'\n\n\n  other_value = get(10)\n  res2 = other_value.match {\n    on None =\u003e 'missing2'\n    on Some(y) =\u003e y * 10\n  }\n\n  puts res2 # =\u003e 100\n```\n\n\n### Ruby::Utils::Param\nA wrapper around a ruby hash that takes a `hash` on initialization:\n\n```ruby\nhash = {\n    a: 1,\n    b: 2,\n    c: {\n      d: 3,\n      e: {\n        f: [{ ff: 1}, { ff: 2 }]\n      }\n    }\n  }\n\nparam = Ruby::Utils::Param(hash)\n\nparam.c.d # =\u003e 3\nparam.c.missing_key # =\u003e Raises error\nparam.get('c.d') # =\u003e 3\n```\n\nTo get access to the original has you can call `params.to_hash`\n\n#### `#get`\nFetch the value of a key if one exist or raise an exception of no key can be found.\n\n```ruby\nparam.get('a') # =\u003e 1\nparams.get('c.d') #=\u003e 3\nparams.get('c.d.missing_key') # =\u003e raised an error\n```\n\n#### `#getOrElse`\nFetch the value of a key if any or else returns nil by default unless one is provided.\n\n```ruby\nparams.getOrElse('a') # =\u003e 1\nparams.getOrElse('a.missing_key') # =\u003e nil\nparams.getOrElse('c.d') # =\u003e 3\n\nparams.getOrElse('a.missing_key', 'default_value') # =\u003e 'default_value'\n```\n\n#### `#defined?`\nReturns true if key is defined else false\n\n```ruby\nparam.defined?('a') # =\u003e true\nparam.defined?('c.e.f') # =\u003e true\nparam.defined('a.missing') # =\u003e false\n```\n\n#### `#all?`\nReturns true when all keys are present:\n\n```ruby\nparam.all?('c.e.f') # =\u003e true\nparams.all?(c.missing) # =\u003e false\n```\n\n#### `#with`\nIt call the block specified when a key can be found in the hash:\n\n```ruby\nparam.with('c.e.f') do |cef_value|\n  puts cef_value # =\u003e [{ ff: 1}, { ff: 2 }]\nend\n\nparam.with('c.missing_key') do |some|\n  raise \"This will never be called\"\nend\n```\n\n#### `#map`\nIt calls the given block for each value when the key can be found in the hash.\n\n```ruby\nhash = { a: { b: 'some string' } }\nparam = Ruby::Utils::Param(hash)\n\nputs param.map('a.b', \u0026upcase) # =\u003e SOME STRING\n\nputs param.map('a.b') do |value|\n  value.upcase\nend # =\u003e SOME STRING\n```\n\n### Ruby::Utils::Hash\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaertispappas%2Fruby_utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaertispappas%2Fruby_utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaertispappas%2Fruby_utils/lists"}