use_arguments 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +63 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/example/simple.rb +61 -0
- data/lib/use_arguments.rb +77 -0
- data/lib/use_arguments/version.rb +3 -0
- data/use_arguments.gemspec +24 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 34aad7b80e79a2e0b3ce2c53e6ca7233f4482af3
|
4
|
+
data.tar.gz: e05c5df2eeab53f272c5893327d860f095a8b0bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb26e0dd72d4bed75342bfad6f2cf36e393d02c0724854f800a4bc43aaf4aab7b08602e8c586a36cf6b0de1154e4b5ca171a30fede75b57f07a4bc8e85b12392
|
7
|
+
data.tar.gz: ebc3598168a6b70a48ac44c71fc2dd879bb82bc63daa7aa35342869a4d7d8320ddb6869ad9f690e350c5439e80d48b6be136a962f12c8836f703b2b40be183f4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/osyo-manga/gem-use_arguments.svg?branch=master)](https://travis-ci.org/osyo-manga/gem-use_arguments)
|
2
|
+
|
3
|
+
# UseArguments
|
4
|
+
|
5
|
+
Use arguments keyword in block.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'use_arguments'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install use_arguments
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "use_arguments"
|
27
|
+
|
28
|
+
=begin
|
29
|
+
Implicitly defined parameters in block.
|
30
|
+
_1, _2..._N : argument value.
|
31
|
+
_args : argument array.
|
32
|
+
_self : self Proc object.
|
33
|
+
_yield : block argument.
|
34
|
+
=end
|
35
|
+
|
36
|
+
# UseArguments::{Use args class name}
|
37
|
+
using UseArguments::Array
|
38
|
+
# or
|
39
|
+
# using UseArguments.usable Array
|
40
|
+
|
41
|
+
p [1, 2, 3].map { _1 + _1 }
|
42
|
+
# => [2, 4, 6]
|
43
|
+
|
44
|
+
using UseArguments::Hash
|
45
|
+
p ({homu: 13, mami: 14, mado: 13}).select { _2 < 14 }
|
46
|
+
# => {:homu=>13, :mado=>13}
|
47
|
+
```
|
48
|
+
|
49
|
+
## Development
|
50
|
+
|
51
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
52
|
+
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/osyo-manga/use_arguments.
|
58
|
+
|
59
|
+
## Special Thanks
|
60
|
+
|
61
|
+
* [supermomonga](http://qiita.com/supermomonga/items/b0576847b1b88e3cd400)
|
62
|
+
|
63
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "use_arguments"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/example/simple.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require "use_arguments"
|
2
|
+
|
3
|
+
using UseArguments
|
4
|
+
|
5
|
+
|
6
|
+
=begin
|
7
|
+
Implicitly defined parameters in block.
|
8
|
+
_1, _2..._N : argument value.
|
9
|
+
_args : argument array.
|
10
|
+
_self : self Proc object.
|
11
|
+
_yield : block argument.
|
12
|
+
=end
|
13
|
+
|
14
|
+
#--------------------------------------
|
15
|
+
# Proc#use_args
|
16
|
+
#--------------------------------------
|
17
|
+
|
18
|
+
p proc { _args }.use_args.call 1, 2, 3
|
19
|
+
# => [1, 2, 3]
|
20
|
+
|
21
|
+
plus = proc { _1 + _2 }.use_args
|
22
|
+
p plus.call 1, 2
|
23
|
+
# => 3
|
24
|
+
|
25
|
+
fact = proc { _1 == 1 ? 1 : _1 * _self.(_1 - 1); }.use_args
|
26
|
+
p fact.call 5
|
27
|
+
# => 120
|
28
|
+
|
29
|
+
f = proc { _yield 1, 2 }.use_args
|
30
|
+
p f.call { |a, b| a - b }
|
31
|
+
# => -1
|
32
|
+
|
33
|
+
p f.call &plus
|
34
|
+
# => 3
|
35
|
+
|
36
|
+
|
37
|
+
#--------------------------------------
|
38
|
+
# Object#use_args
|
39
|
+
#--------------------------------------
|
40
|
+
|
41
|
+
# receiver#use_args#{any receiver method.}
|
42
|
+
p [1, 2, 3].use_args.map { _1 * _1 }
|
43
|
+
# => [1, 4, 9]
|
44
|
+
|
45
|
+
|
46
|
+
#--------------------------------------
|
47
|
+
# Class method use args.
|
48
|
+
#--------------------------------------
|
49
|
+
|
50
|
+
# UseArguments::{Use args class name}
|
51
|
+
using UseArguments::Array
|
52
|
+
# or
|
53
|
+
# using UseArguments.usable Array
|
54
|
+
|
55
|
+
p [1, 2, 3].map { _1 + _1 }
|
56
|
+
# => [2, 4, 6]
|
57
|
+
|
58
|
+
using UseArguments::Hash
|
59
|
+
p ({homu: 13, mami: 14, mado: 13}).select { _2 < 14 }
|
60
|
+
# => {:homu=>13, :mado=>13}
|
61
|
+
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# Original source code.
|
2
|
+
# http://qiita.com/supermomonga/items/b0576847b1b88e3cd400
|
3
|
+
require "use_arguments/version"
|
4
|
+
|
5
|
+
|
6
|
+
module UseArguments
|
7
|
+
module ToUseArgs
|
8
|
+
def use_args
|
9
|
+
return self unless self.parameters.size == 0
|
10
|
+
self_ = self
|
11
|
+
::Object.new.instance_eval do
|
12
|
+
x = proc { |*args, &block|
|
13
|
+
define_singleton_method(:_args) { args }
|
14
|
+
define_singleton_method(:_) { args[0] }
|
15
|
+
define_singleton_method(:_self) { x }
|
16
|
+
define_singleton_method(:_yield) { |*args, &_block| block.call *args, &_block }
|
17
|
+
args.size.times do |i|
|
18
|
+
define_singleton_method("_#{i + 1}") { args[i] }
|
19
|
+
end
|
20
|
+
instance_exec *args, &self_
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
refine ::Proc do
|
27
|
+
include ToUseArgs
|
28
|
+
end
|
29
|
+
|
30
|
+
module AsUseArgs
|
31
|
+
def use_args
|
32
|
+
self_ = self
|
33
|
+
::Class.new(::BasicObject) do
|
34
|
+
define_singleton_method(:method_missing) do |name, *args, &block|
|
35
|
+
return self_.__send__(name, *args, &block) unless block && block.parameters.size == 0
|
36
|
+
self_.__send__(name, *args, &ToUseArgs.instance_method(:use_args).bind(block).())
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
refine ::Object do
|
43
|
+
include AsUseArgs
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.const_missing name
|
47
|
+
self.usable ::ObjectSpace.each_object(::Class).find { |klass| klass.name == name.to_s }
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.usable klass
|
51
|
+
::Module.new do
|
52
|
+
refine klass do
|
53
|
+
prepend ::UseArguments::Usable
|
54
|
+
class <<klass
|
55
|
+
prepend ::UseArguments::Usable
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
using ::UseArguments
|
64
|
+
|
65
|
+
module UseArguments
|
66
|
+
module Usable
|
67
|
+
def self.prepend_features mod
|
68
|
+
mod.__send__ :prepend, (Module.new do
|
69
|
+
for name in mod.instance_methods
|
70
|
+
define_method name do |*args, &block|
|
71
|
+
super *args, &(block ? block.use_args : nil)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'use_arguments/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "use_arguments"
|
8
|
+
spec.version = UseArguments::VERSION
|
9
|
+
spec.authors = ["osyo-manga"]
|
10
|
+
spec.email = ["osyo.manga@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Use arguments keyword in block.}
|
13
|
+
spec.description = %q{Use arguments keyword in block.}
|
14
|
+
spec.homepage = ""
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: use_arguments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- osyo-manga
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Use arguments keyword in block.
|
56
|
+
email:
|
57
|
+
- osyo.manga@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- example/simple.rb
|
71
|
+
- lib/use_arguments.rb
|
72
|
+
- lib/use_arguments/version.rb
|
73
|
+
- use_arguments.gemspec
|
74
|
+
homepage: ''
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.5.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Use arguments keyword in block.
|
97
|
+
test_files: []
|