object_as 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f3e347ce7c24a0e05b63e4511275e005f4a3bbc
4
- data.tar.gz: 9636725de1751fab9b7176cdb9abde1a584d6c3f
3
+ metadata.gz: 6fdc23d5eb1fc0c97181778cdc9a829e0f06f5f9
4
+ data.tar.gz: fc696bc977cf6747851af7101ecc18c7e7a33ccc
5
5
  SHA512:
6
- metadata.gz: a0c37f75b48450ae6ca8d15a2fbe97530840f9779dda9946c6b9e69a59f847b79da7ad38873172e3ca2aa3f7c5f9e83be1414d98c1d7b51caf59d25c2af5cc5b
7
- data.tar.gz: ee78c6a3280f37c8dccb3d420ae30b656e21aac7a45bca8f21aaef5f72c3d3db58203df0af3acaca55faf59bf9367939ffa902c70b123765830ca25f3d3112c2
6
+ metadata.gz: 172e51388a1c0379d2a1d472d2159e1e86b4aa699e13edf403aa16476f6e1e0caa5f391e4ceaa15154b099ea7af7efaa9373611514f21ccbf8a1add72a11803d
7
+ data.tar.gz: 91b0eabfd462cb3403d695460e574f17170ba950bfe0c09d19838b9f48473794edb024d467b6f8926ce4a4eb1866a6735fa3a894c9a8e62a0543aa219d7c8d8c
data/README.md CHANGED
@@ -24,23 +24,64 @@ Or install it yourself as:
24
24
  Object#as it much like Object#tap except that it returns the value of the block
25
25
  instead of the value being tapped.
26
26
 
27
- > "Ruby".as {|n| n.reverse }
28
- => "ybuR"
27
+ > keys = [1, 2, 3]
28
+ > values = %w(a b c)
29
+ > keys.zip(values).as {|kv| Hash[kv] }
30
+ => {1=>"a", 2=>"b", 3=>"c"}
29
31
 
30
32
  Object#as is useful when chaining methods. This is a complete Ruby source file:
31
33
 
32
34
  require 'object_as'
33
35
  using ObjectAs
34
- [1, 2, 3].map {|x| x * x }.as {|array| array.reverse }.each {|x| puts x }
35
- # prints 9, 4 and 1
36
+
37
+ def square(array)
38
+ array.map {|x| x * x }
39
+ end
40
+
41
+ [1, 2, 3].as {|array| square array }.each {|x| puts x }
42
+ # prints 1, 4, 9
36
43
 
37
44
  For Ruby versions older than 2.1, there is a way to use Object#as without
38
45
  refinements. This method uses monkeypatching.
39
46
 
40
47
  require 'object_as_no_refinements'
41
- [1, 2, 3].map {|x| x * x }.as {|array| array.reverse }.each {|x| puts x }
42
- # prints 9, 4 and 1
43
48
 
49
+ def square(array)
50
+ array.map {|x| x * x }
51
+ end
52
+
53
+ [1, 2, 3].as {|array| square array }.each {|x| puts x }
54
+ # prints 1, 4, 9
55
+
56
+ ## Example
57
+
58
+ In this example I am using OpenCv to process an image in a method chain, but at
59
+ one point I want to subtract the average value of the image from itself.
60
+
61
+
62
+ require 'object_as'
63
+ require 'opencv'
64
+
65
+ using ObjectAs
66
+
67
+ region_of_interest =
68
+ CvMat.load("image.png")
69
+ sub_rect(x, y, 20, 20).
70
+ normalize(0.0, 1.0, CV_NORM_MINMAX, CV_32F).
71
+ as {|i| i.sub(i.avg) }
72
+
73
+ The alternative, without Object#as would look something like this:
74
+
75
+ Require 'opencv'
76
+
77
+ tmp =
78
+ CvMat.load(filename)
79
+ sub_rect(x, y, 20, 20).
80
+ normalize(0.0, 1.0, CV_NORM_MINMAX, CV_32F)
81
+
82
+ region_of_interest = tmp.sub(tmp.avg)
83
+
84
+ The method chain is broken, and we have to introduce a temporary variable.
44
85
 
45
86
  ## Contributing
46
87
 
@@ -1,3 +1,3 @@
1
1
  module ObjectAs
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["tallak@tveide.net"]
11
11
  spec.summary = %q{Implementation of the Object#as method}
12
12
  spec.description = %q{Object#as is useful for method chaining}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/tallakt/object_as"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_as
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tallak Tveide
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-20 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,7 +56,7 @@ files:
56
56
  - object_as.gemspec
57
57
  - test/test_object_as.rb
58
58
  - test/test_object_as_no_refinement.rb
59
- homepage: ''
59
+ homepage: https://github.com/tallakt/object_as
60
60
  licenses:
61
61
  - MIT
62
62
  metadata: {}