others 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +5 -27
- data/lib/others.rb +37 -20
- data/others.gemspec +1 -1
- data/test/test_others.rb +13 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82f1673e95d5864489442a00188a0cc4aa1afcc6bba9ac5c91d9b29126511057
|
4
|
+
data.tar.gz: 99e235fe2e4da529835e871c16dc856a5ef1412858ad6f7b18d48d17a68647e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d969e6b6e188401ed29c04e22e964707ad0b33f7224de643a26e3c6b5b8ed19d9b32af761064f5227bf0a6e4b9b81e3279df228b02a41b9525dcdd379fe2f47
|
7
|
+
data.tar.gz: c7937f5bd9ff9d5e8e76c4816aeb6a3a43865f4b3f4289f58fc3183c3077113dba662280fd1fa13489d00bde8d5439a64dfde05dede3fedd43e48bbe81de8905
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Functions as Objects
|
2
2
|
|
3
3
|
[](http://www.rultor.com/p/yegor256/others)
|
4
4
|
[](https://www.jetbrains.com/ruby/)
|
@@ -11,36 +11,14 @@
|
|
11
11
|
[](https://hitsofcode.com/view/github/yegor256/others)
|
12
12
|
[](https://github.com/yegor256/others/blob/master/LICENSE.txt)
|
13
13
|
|
14
|
-
Let's say
|
15
|
-
adding new attributes and methods to it. Here is how:
|
14
|
+
Let's say you need an object that consists of a single function:
|
16
15
|
|
17
16
|
```ruby
|
18
17
|
require 'others'
|
19
|
-
|
20
|
-
|
21
|
-
def parts
|
22
|
-
@origin.strip.split(@br)
|
23
|
-
end
|
18
|
+
x = others(foo: 42) do |*args|
|
19
|
+
@foo + args[1]
|
24
20
|
end
|
25
|
-
assert(
|
26
|
-
```
|
27
|
-
|
28
|
-
You may also turn an existing class into a decorator:
|
29
|
-
|
30
|
-
```ruby
|
31
|
-
require 'others'
|
32
|
-
class MyString
|
33
|
-
def initialize(s, br)
|
34
|
-
@s = s
|
35
|
-
@br = br
|
36
|
-
end
|
37
|
-
others(:s)
|
38
|
-
def parts
|
39
|
-
@origin.strip.split(@br)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
d = MyString.new('Jeff Lebowski')
|
43
|
-
assert(d.parts == ['Jeff', 'Lebowski'])
|
21
|
+
assert(x.bar(10) == 52)
|
44
22
|
```
|
45
23
|
|
46
24
|
That's it.
|
data/lib/others.rb
CHANGED
@@ -26,31 +26,48 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
|
27
27
|
# License:: MIT
|
28
28
|
def others(attrs = {}, &block)
|
29
|
-
|
30
|
-
|
31
|
-
def initialize(attrs, &block)
|
32
|
-
# rubocop:disable Style/HashEachMethods
|
33
|
-
# rubocop:disable Lint/UnusedBlockArgument
|
34
|
-
attrs.each do |k, v|
|
35
|
-
instance_eval("@#{k} = v", __FILE__, __LINE__) # @foo = v
|
36
|
-
end
|
37
|
-
# rubocop:enable Style/HashEachMethods
|
38
|
-
# rubocop:enable Lint/UnusedBlockArgument
|
29
|
+
if is_a?(Class)
|
30
|
+
class_exec(block) do
|
39
31
|
@block = block
|
40
|
-
|
32
|
+
def self.method_missing(*args)
|
33
|
+
raise 'Block cannot be provided' if block_given?
|
34
|
+
instance_exec(*args, &@block)
|
35
|
+
end
|
41
36
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
37
|
+
def self.respond_to?(_mtd, _inc = false)
|
38
|
+
true
|
39
|
+
end
|
46
40
|
|
47
|
-
|
48
|
-
|
41
|
+
def self.respond_to_missing?(_mtd, _inc = false)
|
42
|
+
true
|
43
|
+
end
|
49
44
|
end
|
45
|
+
else
|
46
|
+
c = Class.new do
|
47
|
+
def initialize(attrs, &block)
|
48
|
+
# rubocop:disable Style/HashEachMethods
|
49
|
+
# rubocop:disable Lint/UnusedBlockArgument
|
50
|
+
attrs.each do |k, v|
|
51
|
+
instance_eval("@#{k} = v", __FILE__, __LINE__) # @foo = v
|
52
|
+
end
|
53
|
+
# rubocop:enable Style/HashEachMethods
|
54
|
+
# rubocop:enable Lint/UnusedBlockArgument
|
55
|
+
@block = block
|
56
|
+
end
|
57
|
+
|
58
|
+
def method_missing(*args)
|
59
|
+
raise 'Block cannot be provided' if block_given?
|
60
|
+
instance_exec(*args, &@block)
|
61
|
+
end
|
50
62
|
|
51
|
-
|
52
|
-
|
63
|
+
def respond_to?(_mtd, _inc = false)
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
67
|
+
def respond_to_missing?(_mtd, _inc = false)
|
68
|
+
true
|
69
|
+
end
|
53
70
|
end
|
71
|
+
c.new(attrs, &block)
|
54
72
|
end
|
55
|
-
c.new(attrs, &block)
|
56
73
|
end
|
data/others.gemspec
CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
28
28
|
s.required_ruby_version = '>=3.0'
|
29
29
|
s.name = 'others'
|
30
|
-
s.version = '0.0.
|
30
|
+
s.version = '0.0.2'
|
31
31
|
s.license = 'MIT'
|
32
32
|
s.summary = 'others'
|
33
33
|
s.description =
|
data/test/test_others.rb
CHANGED
@@ -50,4 +50,17 @@ class TestOthers < Minitest::Test
|
|
50
50
|
x.bar { |i| i + 1 }
|
51
51
|
end
|
52
52
|
end
|
53
|
+
|
54
|
+
def test_as_class
|
55
|
+
x = Class.new do
|
56
|
+
def self.foo(abc)
|
57
|
+
abc + 1
|
58
|
+
end
|
59
|
+
others do |*args|
|
60
|
+
args[1] + 2
|
61
|
+
end
|
62
|
+
end
|
63
|
+
assert_equal(43, x.foo(42))
|
64
|
+
assert_equal(44, x.bar(42))
|
65
|
+
end
|
53
66
|
end
|