procer 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/procer.rb +13 -0
  3. metadata +69 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a3d342db7f48f642cbf023a55a7a508012471e015805d96a9e3583871116f80
4
+ data.tar.gz: 4119f171e84df62fc7497554af445ae492aa985b26d74a51372b9a94548a574e
5
+ SHA512:
6
+ metadata.gz: 744015e76d9cb4ff18d0ee8463ad7b135af4b0fd1058a312089fc9f62034b6d350913c177cc2720b5082c01b911cd2fd98d9df132c76a930c414b1d402f24313
7
+ data.tar.gz: 650405168717419f3a6740b14afea0f825740a7a2f7bb98337becd9b636b55bf7e62aa1ef51b8ef5183a6dc1a67d5d477cbcf24ea386259de4e0ec132735abda
data/lib/procer.rb ADDED
@@ -0,0 +1,13 @@
1
+ class Object
2
+ def to_proc
3
+ method = case
4
+ when respond_to?(:call)
5
+ :call
6
+ when respond_to?(:[])
7
+ :[]
8
+ else
9
+ :===
10
+ end
11
+ proc { send(method, _1) }
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: procer
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Jesús Gómez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "# Procer\n\nA reasonable good default `to_proc` method for all objects.\n\nWhe
14
+ you require Procer, all objects will have a default `to_proc`\nmethod which will
15
+ try to call one of the following methods, in the\ngiven order:\n\n - `call`\n -
16
+ `[]`\n - `===`\n \n Many methods which receive a block, can benefit greatlly from
17
+ this\n because you can now pass an object to perform the block role.\n \n Think
18
+ of the Enumerable module and all it's methods.\n \n Many objects define `===`, but
19
+ not `to_proc`. So they will be nicelly\n usable in a `case/when` expression, but
20
+ not in other contexts.\n \n This is the case of classes and ranges, which you can
21
+ use in\n `case/when` expressions, but they don't define `to_proc`.\n \n Now they
22
+ do define `to_proc` so they are useful in those contexts.\n \n Examples:\n \n ```ruby\n
23
+ require 'procer'\n \n[1, 2, '3', '4', 5, 6].filter(&Numeric)\n# => [1, 2, 5, 6]\n\n[-10,
24
+ 100, -2, 3, 20, -33].filter(&(0..50))\n# => [3, 20]\n```\n\nAlso, Hashes already
25
+ implement `to_proc` and that is really useful\nwith enumerator. We can use it as
26
+ a transformation table with `map`:\n\n```ruby\n\ntable = {\n 1 => 'one',\n 2 =>
27
+ 'two',\n 3 => 'three'\n}\n\n[3, 1, 2].map(&table)\n# => ['three, 'one, 'two']\n```\n\nSadly,
28
+ Arrays, even when they have the same interface as hashes as a\nfunction of indices,
29
+ don't implement `to_proc` and so they can't be used\nin the same way. Until now.\n\n```ruby\ntable
30
+ = ['zero', 'one', 'two']\n[2, 0, 1].map(&table)\n# => ['two', 'zero', 'one']\n```\n\nAlternativaly
31
+ you could have use `values_at`:\n\n```ruby\ntable.values_at([3, 1, 2]) # In the
32
+ Hash example\ntable.values_at([2, 0, 1]) # In the Array example\n```\n\nBut the
33
+ map solution is more generic and `table` can be anything that\nimplements `to_proc`
34
+ and not something that necesarilly implement\n`values_at`.\n\nNotice that if the
35
+ object implements `[]` that will triumph over\n`===`. It was expected when I tried
36
+ to use Integers as the object, as\nthey implement `[]` as a way to access it's binary
37
+ form:\n\n```ruby\n5 # b101\n[5[2], 5[1], 5[0]] # [1, 0, 1]\n```\n\nSo the proc will
38
+ work like that:\n\n```ruby\n[2, 4, 5].map(&5)\n# Actual => [1, 0, 0]\n# I was expecting
39
+ => [false, false, true]\n```\n"
40
+ email: jgomo3@gmail.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - lib/procer.rb
46
+ homepage: https://github.com/jgomo3/procer/
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 1.8.7
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.2.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Provides a reasonable good default `to_proc` method to all objects
69
+ test_files: []