in_enumerable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/LICENSE +20 -0
- data/README.rdoc +74 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/in_enumerable.gemspec +58 -0
- data/lib/in_enumerable.rb +18 -0
- data/test/helper.rb +10 -0
- data/test/test_in_enumerable.rb +57 -0
- metadata +80 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Brian Morearty
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
= in_enumerable
|
2
|
+
|
3
|
+
A simple but nice language extension.
|
4
|
+
|
5
|
+
in_enumerable extends the Object type with the tasty 'in?' method, which returns true if an object is included
|
6
|
+
in a list or other enumerable value. So you can do this:
|
7
|
+
|
8
|
+
1.in? [1,2] # => true
|
9
|
+
3.in? [1,2] # => false
|
10
|
+
|
11
|
+
instead of the slightly more awkward:
|
12
|
+
|
13
|
+
[1,2].include?(1) # => true
|
14
|
+
[1,2].include?(3) # => false
|
15
|
+
|
16
|
+
Despite its name, in_enumerable doesn't require an enumerable type.
|
17
|
+
It uses duck typing to work with any type that has an 'include?' method, such as:
|
18
|
+
|
19
|
+
# Array (see examples above)
|
20
|
+
|
21
|
+
# Hash
|
22
|
+
h = { "a" => 100, "b" => 200 }
|
23
|
+
"a".in?(h) # => true
|
24
|
+
"z".in?(h) # => false
|
25
|
+
|
26
|
+
# String
|
27
|
+
"lo".in?("hello") # => true
|
28
|
+
"ol".in?("hello") # => false
|
29
|
+
?h.in?("hello") # => true
|
30
|
+
|
31
|
+
# Range
|
32
|
+
25.in?(1..50) # => true
|
33
|
+
75.in?(1..50) # => false
|
34
|
+
|
35
|
+
# Set
|
36
|
+
require 'set'
|
37
|
+
s = Set.new([1,2])
|
38
|
+
1.in?(s) # => true
|
39
|
+
3.in?(s) # => false
|
40
|
+
|
41
|
+
# Even Module
|
42
|
+
module A
|
43
|
+
end
|
44
|
+
class B
|
45
|
+
include A
|
46
|
+
end
|
47
|
+
class C < B
|
48
|
+
end
|
49
|
+
A.in?(B) # => true
|
50
|
+
A.in?(C) # => true
|
51
|
+
A.in?(A) # => false
|
52
|
+
|
53
|
+
== Installation
|
54
|
+
|
55
|
+
gem install in_enumerable
|
56
|
+
|
57
|
+
== Usage
|
58
|
+
|
59
|
+
require 'rubygems'
|
60
|
+
require 'in_enumerable'
|
61
|
+
|
62
|
+
== Note on Patches/Pull Requests
|
63
|
+
|
64
|
+
* Fork the project.
|
65
|
+
* Make your feature addition or bug fix.
|
66
|
+
* Add tests for it. This is important so I don't break it in a
|
67
|
+
future version unintentionally.
|
68
|
+
* Commit, do not mess with rakefile, version, or history.
|
69
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
70
|
+
* Send me a pull request. Bonus points for topic branches.
|
71
|
+
|
72
|
+
== Copyright
|
73
|
+
|
74
|
+
Copyright (c) 2009 Brian Morearty. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "in_enumerable"
|
8
|
+
gem.summary = %Q{extends the Object type with the tasty 'in?' method}
|
9
|
+
gem.description = %Q{extends the Object type with the tasty 'in?' method, which returns true if an object is included
|
10
|
+
in a list or other enumerable value. So you can do this:
|
11
|
+
1.in? [1,2] # => true
|
12
|
+
3.in? [1,2] # => false
|
13
|
+
}
|
14
|
+
gem.email = "brian at morearty.org"
|
15
|
+
gem.homepage = "http://github.com/BMorearty/in_enumerable"
|
16
|
+
gem.authors = ["Brian Morearty"]
|
17
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
18
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
Rake::TestTask.new(:test) do |test|
|
27
|
+
test.libs << 'lib' << 'test'
|
28
|
+
test.pattern = 'test/**/test_*.rb'
|
29
|
+
test.verbose = true
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'rcov/rcovtask'
|
34
|
+
Rcov::RcovTask.new do |test|
|
35
|
+
test.libs << 'test'
|
36
|
+
test.pattern = 'test/**/test_*.rb'
|
37
|
+
test.verbose = true
|
38
|
+
end
|
39
|
+
rescue LoadError
|
40
|
+
task :rcov do
|
41
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
task :test => :check_dependencies
|
46
|
+
|
47
|
+
task :default => :test
|
48
|
+
|
49
|
+
require 'rake/rdoctask'
|
50
|
+
Rake::RDocTask.new do |rdoc|
|
51
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "in_enumerable #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{in_enumerable}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Brian Morearty"]
|
12
|
+
s.date = %q{2009-12-24}
|
13
|
+
s.description = %q{extends the Object type with the tasty 'in?' method, which returns true if an object is included
|
14
|
+
in a list or other enumerable value. So you can do this:
|
15
|
+
1.in? [1,2] # => true
|
16
|
+
3.in? [1,2] # => false
|
17
|
+
}
|
18
|
+
s.email = %q{brian at morearty.org}
|
19
|
+
s.extra_rdoc_files = [
|
20
|
+
"LICENSE",
|
21
|
+
"README.rdoc"
|
22
|
+
]
|
23
|
+
s.files = [
|
24
|
+
".document",
|
25
|
+
".gitignore",
|
26
|
+
"LICENSE",
|
27
|
+
"README.rdoc",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"in_enumerable.gemspec",
|
31
|
+
"lib/in_enumerable.rb",
|
32
|
+
"test/helper.rb",
|
33
|
+
"test/test_in_enumerable.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/BMorearty/in_enumerable}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{extends the Object type with the tasty 'in?' method}
|
40
|
+
s.test_files = [
|
41
|
+
"test/helper.rb",
|
42
|
+
"test/test_in_enumerable.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Extends the Object type with the tasty 'in?' method, which tells you if an object is included
|
2
|
+
# in an Array or other enumerable value
|
3
|
+
module InEnumerable
|
4
|
+
# Returns true if enumerable includes self. Like this:
|
5
|
+
#
|
6
|
+
# 1.in? [1,2] # => true
|
7
|
+
# 3.in? [1,2] # => false
|
8
|
+
#
|
9
|
+
# == Parameters
|
10
|
+
# [enumerable] Any value that implements an include? method. E.g., Array, Hash, String, Range.
|
11
|
+
def in?(enumerable)
|
12
|
+
enumerable.include? self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Object
|
17
|
+
include InEnumerable
|
18
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
class TestInEnumerable < Test::Unit::TestCase
|
5
|
+
context "An Array [1,2]" do
|
6
|
+
should "include 1 but not 3" do
|
7
|
+
assert 1.in?([1,2]) # => true
|
8
|
+
assert !3.in?([1,2]) # => false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "A Hash with keys 'a' and 'b'" do
|
13
|
+
should "include 'a' but not 'z'" do
|
14
|
+
h = { "a" => 100, "b" => 200 }
|
15
|
+
assert "a".in?(h) # => true
|
16
|
+
assert !"z".in?(h) # => false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "A String 'hello'" do
|
21
|
+
should "include 'lo' and ?h but not 'ol'" do
|
22
|
+
assert "lo".in?("hello") # => true
|
23
|
+
assert !"ol".in?("hello") # => false
|
24
|
+
assert ?h.in?("hello") # => true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "A Range (1..50)" do
|
29
|
+
should "include 25 but not 75" do
|
30
|
+
assert 25.in?(1..50) # => true
|
31
|
+
assert !75.in?(1..50) # => false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "A Set {1,2}" do
|
36
|
+
should "include 1 but not 3" do
|
37
|
+
s = Set.new([1,2])
|
38
|
+
assert 1.in?(s) # => true
|
39
|
+
assert !3.in?(s) # => false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "Module A included in B which has subclass C" do
|
44
|
+
should "be in B and C but not in itself" do
|
45
|
+
module A
|
46
|
+
end
|
47
|
+
class B
|
48
|
+
include A
|
49
|
+
end
|
50
|
+
class C < B
|
51
|
+
end
|
52
|
+
assert A.in?(B) # => true
|
53
|
+
assert A.in?(C) # => true
|
54
|
+
assert !A.in?(A) # => false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: in_enumerable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Morearty
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-24 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: |
|
26
|
+
extends the Object type with the tasty 'in?' method, which returns true if an object is included
|
27
|
+
in a list or other enumerable value. So you can do this:
|
28
|
+
1.in? [1,2] # => true
|
29
|
+
3.in? [1,2] # => false
|
30
|
+
|
31
|
+
email: brian at morearty.org
|
32
|
+
executables: []
|
33
|
+
|
34
|
+
extensions: []
|
35
|
+
|
36
|
+
extra_rdoc_files:
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
files:
|
40
|
+
- .document
|
41
|
+
- .gitignore
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
- Rakefile
|
45
|
+
- VERSION
|
46
|
+
- in_enumerable.gemspec
|
47
|
+
- lib/in_enumerable.rb
|
48
|
+
- test/helper.rb
|
49
|
+
- test/test_in_enumerable.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/BMorearty/in_enumerable
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.5
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: extends the Object type with the tasty 'in?' method
|
78
|
+
test_files:
|
79
|
+
- test/helper.rb
|
80
|
+
- test/test_in_enumerable.rb
|