https://github.com/lpogic/koper
Controversial standard class operator set
https://github.com/lpogic/koper
operators ruby
Last synced: about 1 year ago
JSON representation
Controversial standard class operator set
- Host: GitHub
- URL: https://github.com/lpogic/koper
- Owner: lpogic
- Created: 2024-02-28T10:11:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-28T21:57:25.000Z (about 2 years ago)
- Last Synced: 2025-03-06T10:55:56.594Z (over 1 year ago)
- Topics: operators, ruby
- Language: Ruby
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
koper - Controversial standard class operator set
===
It extends some standard classes with operators whose mathematical and logical meaning is difficult to justify.
Installation
---
```
gem install koper
```
Usage
---
### 1. Complete example
```RUBY
require 'koper'
hash = {
a: 1,
b: 2
}
p hash / :a # => 1 (#fetch or #fetch_values if right side is an Enumerable)
p hash / [:b, :a] # => [2, 1]
p hash & :a # => {:a=>1} (#slice or #slice with splat if right side is an Enumerable)
p hash & [:b, :a] # => {:b=>2, :a=>1}
p !hash # => {1=>:a, 2=>:b} (#invert)
p hash + {c: 3} # => {:a=>1, :b=>2, :c=>3} (#merge)
array = [3, 4, 5, 6]
p array / 2 # => 5 (#[] alias)
p array >> 2 # => [3, 4] (like #pop but returns self)
p array >> 1 << 8 # => [3, 8] (last element swap)
p array[] = 9 # => 9 (like #push but returns right side argument)
```
### 2. Digging
```RUBY
require 'koper'
hash = {
a: 1,
b: {
c: 2
},
d: [
5,
{
e: 5
}
]
}
p hash/:b/:c # => 2
p hash/:x/:y # => nil
p hash/:d/1/:e # => 5
```
Authors
---
- Łukasz Pomietło (oficjalnyadreslukasza@gmail.com)