Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laertispappas/ruby_utils
Ruby core extensions and class utilities.
https://github.com/laertispappas/ruby_utils
ruby
Last synced: 16 days ago
JSON representation
Ruby core extensions and class utilities.
- Host: GitHub
- URL: https://github.com/laertispappas/ruby_utils
- Owner: laertispappas
- License: mit
- Created: 2016-11-27T22:22:43.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-17T23:39:02.000Z (almost 8 years ago)
- Last Synced: 2024-10-30T21:27:15.908Z (2 months ago)
- Topics: ruby
- Language: Ruby
- Homepage:
- Size: 15.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ruby_utils
Various ruby core extensions and class utilities.
## Status
TBD## Installation
Add this line to your application's Gemfile:
```ruby
gem 'ruby_utils'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install ruby-utils
## Usage
TBD## Features
### Ruby::Utils::Option
A scala like container for zero or one element. An option can be either Some[Object]
or None object.Creating a Some or None option:
```ruby
def find_person(id)
person = Person.find_by_id
person ? Some(person) : None
endperson = find_person(1)
person.class # None if not found else Some(person)
```#### `#empty?` True if None, false if Some
```ruby
none = None
none.empty? # => true
none.defined? # => falsesome = Some(Class.new)
some.empty? # => false
some.defined? # => true
```
#### `#defined?` True if Some else false on None#### `get` Returns the option's value or an exception is raised on empty option.
```ruby
none = None
none.get # => NoSuchElementErrorsome = Some(12)
some.get # => 12
```#### `get_or_else(default)` Returns the option's value if the option is nonempty, otherwise return the result of evaluating `default`.
```ruby
none = None
none.get_or_else("default") # => 'default'some = Some(12)
some.get_or_else("default") # => 12
```#### `map(f=nil, &block)` 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
```ruby
f = ->(x) { x*x }none = None
none.map(f) #=> None
none.map { |e| e* 100 } # -> nonesome = Some(10)
some.map(f) # => Some(100)
some.map { |e| e*2 } # => Some(20)
```#### `flat_map(f, &block)` If the option is nonempty, return a function applied to its value.
Otherwise return None.```ruby
f = ->(x) { x * 10 }
none = None
none.flat_map(f) # => None
none.flat_map { |a| a } # => Nonesome = Some(20)
some.flat_map(f) # => 200
some.flat_map { |e| e * 2 } # => 40
```#### `filter(p=nil, &block)` If the option is nonempty and the given predicate `p` yields `false` on its value, return `None`. Otherwise return the option value itself.
```ruby
p = ->(x) { x % 2 == 0 }none = None
none.filter(p) # => None
none.filter { |a| a == 1 } # => Nonesome = Some(10)
some.filter(p) # => Some(10)some = Some(3)
some.filter(p) # => None
```#### `or_else(alternative)` If the option is nonempty return it, otherwise return the result of evaluating an alternative expression.
```ruby
none = None
none.or_else(12) # => 12
none.or_else(Some(12)) # => Some(12)some = Some(10)
some.or_else(111) # => Some(10)
```#### `match` [Experimental] Provides pseudo pattern match for option classes (see also `List#match`)
```ruby
def get(id)
if id == 1
None
else
Some(id)
end
endsome_value = get(1) # None
result = some_value.match {
on None => 'missing'
on Some(x) => x * 100
}purs result # => 'missing'
other_value = get(10)
res2 = other_value.match {
on None => 'missing2'
on Some(y) => y * 10
}puts res2 # => 100
```### Ruby::Utils::Param
A wrapper around a ruby hash that takes a `hash` on initialization:```ruby
hash = {
a: 1,
b: 2,
c: {
d: 3,
e: {
f: [{ ff: 1}, { ff: 2 }]
}
}
}param = Ruby::Utils::Param(hash)
param.c.d # => 3
param.c.missing_key # => Raises error
param.get('c.d') # => 3
```To get access to the original has you can call `params.to_hash`
#### `#get`
Fetch the value of a key if one exist or raise an exception of no key can be found.```ruby
param.get('a') # => 1
params.get('c.d') #=> 3
params.get('c.d.missing_key') # => raised an error
```#### `#getOrElse`
Fetch the value of a key if any or else returns nil by default unless one is provided.```ruby
params.getOrElse('a') # => 1
params.getOrElse('a.missing_key') # => nil
params.getOrElse('c.d') # => 3params.getOrElse('a.missing_key', 'default_value') # => 'default_value'
```#### `#defined?`
Returns true if key is defined else false```ruby
param.defined?('a') # => true
param.defined?('c.e.f') # => true
param.defined('a.missing') # => false
```#### `#all?`
Returns true when all keys are present:```ruby
param.all?('c.e.f') # => true
params.all?(c.missing) # => false
```#### `#with`
It call the block specified when a key can be found in the hash:```ruby
param.with('c.e.f') do |cef_value|
puts cef_value # => [{ ff: 1}, { ff: 2 }]
endparam.with('c.missing_key') do |some|
raise "This will never be called"
end
```#### `#map`
It calls the given block for each value when the key can be found in the hash.```ruby
hash = { a: { b: 'some string' } }
param = Ruby::Utils::Param(hash)puts param.map('a.b', &upcase) # => SOME STRING
puts param.map('a.b') do |value|
value.upcase
end # => SOME STRING
```### Ruby::Utils::Hash