arguments 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +22 -0
- data/Manifest.txt +12 -0
- data/README.rdoc +114 -0
- data/Rakefile +15 -0
- data/arguments.gemspec +49 -0
- data/lib/arguments.rb +27 -0
- data/lib/arguments/class.rb +51 -0
- data/lib/arguments/mri.rb +9 -0
- data/lib/arguments/vm.rb +21 -0
- data/spec/arguments_spec.rb +151 -0
- data/spec/klass.rb +58 -0
- data/spec/module.rb +9 -0
- metadata +130 -0
data/History.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
== 0.4 2009-06-30
|
2
|
+
|
3
|
+
* 1 major enhancement:
|
4
|
+
* Initial release
|
5
|
+
* Works with Ruby 1.8.6 and 1.9.1
|
6
|
+
|
7
|
+
== 0.4.2 2009-07-03
|
8
|
+
|
9
|
+
* Fixed a Bug where not passing arguments would rise try to call #key? on nil
|
10
|
+
|
11
|
+
== 0.4.3 2009-07-07
|
12
|
+
|
13
|
+
* Fixed a serious bug where default values could not be overriden by passing arguments without keyword
|
14
|
+
|
15
|
+
== 0.6 2009-08-06
|
16
|
+
|
17
|
+
* Fixed a bug where converting a block to proc argument using ampersand would blow method call
|
18
|
+
* Passing a keyword not corresponding to an argument name raises an error
|
19
|
+
* Using RubyParser in Ruby 1.9.1 instead of Regexp to extract argument names and default values
|
20
|
+
* Method definition that uses splat operator is now ignored
|
21
|
+
* Last Hash argument is not used for assigning argument values if total number of arguments has been passed
|
22
|
+
* named_arguments_for can be called passing a Class method name eg: named_args_for :instance_method, 'self.class_method'
|
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
= arguments
|
2
|
+
|
3
|
+
http://github.com/maca/arguments
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support.
|
8
|
+
Arguments has been tested with Ruby 1.8.6 and ruby 1.9.1 and eventually will work with JRuby (if someone is interested in contributing, I guess is possible since merb-action-args works with JRuby)
|
9
|
+
|
10
|
+
== SYNOPSIS:
|
11
|
+
|
12
|
+
require 'arguments'
|
13
|
+
|
14
|
+
class Example
|
15
|
+
def meth(a = :a, b = :b, c = :c)
|
16
|
+
[a,b,c]
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def class_method(a = :a, b = :b, c = :c)
|
21
|
+
[a,b,c]
|
22
|
+
end
|
23
|
+
|
24
|
+
def other_class_method(a = :a, b = :b, c = :c)
|
25
|
+
[a,b,c]
|
26
|
+
end
|
27
|
+
named_args_for :class_method
|
28
|
+
end
|
29
|
+
|
30
|
+
named_args_for :meth, :'self.other_class_method'
|
31
|
+
end
|
32
|
+
|
33
|
+
nu = Example.new
|
34
|
+
nu.meth #=> [:a,:b,:c]
|
35
|
+
nu.meth(1, :c => Class) #=> [1,:b,Class]
|
36
|
+
nu.meth(:b => nil, :a => 'something') #=> ['something', nil, :c]
|
37
|
+
|
38
|
+
Example.class_method(:b => nil, :a => 'something') #=> ['something', nil, :c]
|
39
|
+
Example.other_class_method(:b => nil, :a => 'something') #=> ['something', nil, :c]
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
* #named_arguments_for is aliased as #named_args_for and #named_args
|
45
|
+
* Calling #named_args_for without arguments patches all previously declared methods in scope
|
46
|
+
* Last Hash argument is used to assign argument values unless passing all accepted arguments:
|
47
|
+
|
48
|
+
nu.meth(1, :c => 'string') #=> [1, :b, 'string']
|
49
|
+
nu.meth(1, 2, :a => 'string') #=> [1, 2, {:a => 'string'}]
|
50
|
+
|
51
|
+
* Any number of method names (Symbol or String) corresponding to existing methods can be passed
|
52
|
+
* Methods with no optionals won't be patched (will behave as usual with no named params)
|
53
|
+
* Same for methods with splatted arguments and for native methods (not declared in Ruby)
|
54
|
+
* Keyword argument take precedence over argument order:
|
55
|
+
|
56
|
+
nu.meth(10, :a => -10) #=> [-10, :b, :c]
|
57
|
+
|
58
|
+
* pasing a key not corresponding to an argument name will raise ArgumentError:
|
59
|
+
|
60
|
+
nu.meth(10, :four => -10) #=> ArgumentError: `four` is not an argument name.
|
61
|
+
|
62
|
+
== LIMITATIONS
|
63
|
+
|
64
|
+
* Performance penalty occurs only in 1.8.6 due to ParseTree use and only while calling Class#named_args\_for, penalty while calling the actuall method is neglectable.
|
65
|
+
* With Ruby 1.8.6 it can patch methods declared with eval, with 1.9.1 only methods declared in a source file.
|
66
|
+
|
67
|
+
== REQUIREMENTS:
|
68
|
+
|
69
|
+
- Ruby2Ruby 1.1.9
|
70
|
+
|
71
|
+
Ruby 1.8.6:
|
72
|
+
- ParseTree >= 3.0.3
|
73
|
+
|
74
|
+
Ruby 1.9.1
|
75
|
+
- RubyParser >= 2.0.2
|
76
|
+
|
77
|
+
== INSTALL:
|
78
|
+
|
79
|
+
sudo gem install arguments --source http://gemcutter.org
|
80
|
+
|
81
|
+
== LATEST CHANGES (0.6)
|
82
|
+
|
83
|
+
* Fixed a bug where converting a block to proc argument using ampersand would blow method call
|
84
|
+
* Passing a keyword not corresponding to an argument name raises an error
|
85
|
+
* Using RubyParser in Ruby 1.9.1 instead of Regexp to extract argument names and default values
|
86
|
+
* Method definition that uses splat operator is now ignored
|
87
|
+
* Last Hash argument is not used for assigning argument values if total number of arguments has been passed
|
88
|
+
* named_arguments_for can be called passing a Class method name eg: named_args_for :instance_method, 'self.class_method'
|
89
|
+
|
90
|
+
|
91
|
+
== LICENSE:
|
92
|
+
|
93
|
+
(The MIT License)
|
94
|
+
|
95
|
+
Copyright (c) 2009 Macario Ortega
|
96
|
+
|
97
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
98
|
+
a copy of this software and associated documentation files (the
|
99
|
+
'Software'), to deal in the Software without restriction, including
|
100
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
101
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
102
|
+
permit persons to whom the Software is furnished to do so, subject to
|
103
|
+
the following conditions:
|
104
|
+
|
105
|
+
The above copyright notice and this permission notice shall be
|
106
|
+
included in all copies or substantial portions of the Software.
|
107
|
+
|
108
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
109
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
110
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
111
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
112
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
113
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
114
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
require './lib/arguments'
|
4
|
+
|
5
|
+
Hoe.plugin :newgem
|
6
|
+
|
7
|
+
$hoe = Hoe.spec 'arguments' do
|
8
|
+
self.developer 'Macario Ortega', 'macarui@gmail.com'
|
9
|
+
# self.url 'http://github.com/maca/arguments'
|
10
|
+
self.extra_deps = [
|
11
|
+
['ruby_parser', '>= 2.0.2'],
|
12
|
+
['ParseTree', '>= 3.0.3'],
|
13
|
+
['ruby2ruby', '= 1.1.9']
|
14
|
+
]
|
15
|
+
end
|
data/arguments.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{arguments}
|
5
|
+
s.version = "0.6"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Macario Ortega"]
|
9
|
+
s.date = %q{2010-01-17}
|
10
|
+
s.description = %q{You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support.
|
11
|
+
Arguments has been tested with Ruby 1.8.6 and ruby 1.9.1 and eventually will work with JRuby (if someone is interested in contributing, I guess is possible since merb-action-args works with JRuby)}
|
12
|
+
s.email = ["macarui@gmail.com"]
|
13
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt"]
|
14
|
+
s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "arguments.gemspec", "lib/arguments.rb", "lib/arguments/class.rb", "lib/arguments/mri.rb", "lib/arguments/vm.rb", "spec/arguments_spec.rb", "spec/klass.rb", "spec/module.rb"]
|
15
|
+
s.homepage = %q{http://github.com/maca/arguments}
|
16
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{arguments}
|
19
|
+
s.rubygems_version = %q{1.3.5}
|
20
|
+
s.summary = %q{You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_runtime_dependency(%q<ruby_parser>, [">= 2.0.2"])
|
28
|
+
s.add_runtime_dependency(%q<ParseTree>, [">= 3.0.3"])
|
29
|
+
s.add_runtime_dependency(%q<ruby2ruby>, ["= 1.1.9"])
|
30
|
+
s.add_development_dependency(%q<rubyforge>, [">= 2.0.3"])
|
31
|
+
s.add_development_dependency(%q<gemcutter>, [">= 0.3.0"])
|
32
|
+
s.add_development_dependency(%q<hoe>, [">= 2.5.0"])
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<ruby_parser>, [">= 2.0.2"])
|
35
|
+
s.add_dependency(%q<ParseTree>, [">= 3.0.3"])
|
36
|
+
s.add_dependency(%q<ruby2ruby>, ["= 1.1.9"])
|
37
|
+
s.add_dependency(%q<rubyforge>, [">= 2.0.3"])
|
38
|
+
s.add_dependency(%q<gemcutter>, [">= 0.3.0"])
|
39
|
+
s.add_dependency(%q<hoe>, [">= 2.5.0"])
|
40
|
+
end
|
41
|
+
else
|
42
|
+
s.add_dependency(%q<ruby_parser>, [">= 2.0.2"])
|
43
|
+
s.add_dependency(%q<ParseTree>, [">= 3.0.3"])
|
44
|
+
s.add_dependency(%q<ruby2ruby>, ["= 1.1.9"])
|
45
|
+
s.add_dependency(%q<rubyforge>, [">= 2.0.3"])
|
46
|
+
s.add_dependency(%q<gemcutter>, [">= 0.3.0"])
|
47
|
+
s.add_dependency(%q<hoe>, [">= 2.5.0"])
|
48
|
+
end
|
49
|
+
end
|
data/lib/arguments.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
gem 'ruby2ruby', '= 1.1.9'
|
2
|
+
require 'ruby2ruby'
|
3
|
+
|
4
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
5
|
+
require 'arguments/class'
|
6
|
+
|
7
|
+
RUBY_VERSION.to_f >= 1.9 ? require( 'arguments/vm' ) : require( 'arguments/mri' )
|
8
|
+
|
9
|
+
module Arguments
|
10
|
+
VERSION = '0.6'
|
11
|
+
|
12
|
+
def self.names klass, method
|
13
|
+
args = ast_for_method(klass, method).assoc(:args)
|
14
|
+
args = args[1..-1]
|
15
|
+
|
16
|
+
return [] if args.empty? or args.last.is_a?(Symbol)
|
17
|
+
vals = args.pop[1..-1]
|
18
|
+
|
19
|
+
args.collect do |arg|
|
20
|
+
if val = vals.find{ |v| v[1] == arg }
|
21
|
+
[arg, Ruby2Ruby.new.process(val.last)]
|
22
|
+
else
|
23
|
+
[arg]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
class Class
|
2
|
+
def named_arguments_for *methods
|
3
|
+
methods = instance_methods - Object.methods if methods.empty?
|
4
|
+
|
5
|
+
methods.each do |meth|
|
6
|
+
meth = meth.to_s
|
7
|
+
klass = meth.sub!(/^self./, '') ? (class << self; self; end) : self
|
8
|
+
names = Arguments.names klass, meth
|
9
|
+
next if names.empty? or names.inject(false) { |bol, pair| bol || /^\*/ === pair.first.to_s }
|
10
|
+
assigns = []
|
11
|
+
names.pop if /^&/ === names[-1][0].to_s
|
12
|
+
|
13
|
+
names.each_with_index do |name, index|
|
14
|
+
unless name.size == 1
|
15
|
+
assigns << <<-RUBY_EVAL
|
16
|
+
#{ name.first } =
|
17
|
+
if opts.key? :#{ name.first }
|
18
|
+
opts.delete :#{ name.first }
|
19
|
+
else
|
20
|
+
args.size >= #{ index + 1 } ? args[#{ index }] : #{ name.last }
|
21
|
+
end
|
22
|
+
RUBY_EVAL
|
23
|
+
else
|
24
|
+
assigns << <<-RUBY_EVAL
|
25
|
+
begin
|
26
|
+
#{ name.first } = opts.key?(:#{ name.first }) ? opts.delete(:#{ name.first }) : args.fetch(#{ index })
|
27
|
+
rescue
|
28
|
+
raise ArgumentError.new('passing `#{ name.first }` is required')
|
29
|
+
end
|
30
|
+
RUBY_EVAL
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
klass.module_eval <<-RUBY_EVAL, __FILE__, __LINE__
|
35
|
+
def __#{ meth }_with_keyword_arguments *args, &block
|
36
|
+
opts = args.last.kind_of?( Hash ) && args.size < #{ names.size } ? args.pop : {}
|
37
|
+
#{ assigns.join("\n") }
|
38
|
+
unless opts.empty?
|
39
|
+
raise ArgumentError.new("`\#{ opts.keys.join(', ') }` \#{ opts.size == 1 ? 'is not a recognized argument keyword' : 'are not recognized argument keywords' }")
|
40
|
+
end
|
41
|
+
__original_#{ meth } #{ names.collect{ |n| n.first }.join(', ') }, &block
|
42
|
+
end
|
43
|
+
|
44
|
+
alias __original_#{ meth } #{ meth }
|
45
|
+
alias #{ meth } __#{ meth }_with_keyword_arguments
|
46
|
+
RUBY_EVAL
|
47
|
+
end
|
48
|
+
end
|
49
|
+
alias :named_args_for :named_arguments_for
|
50
|
+
alias :named_args :named_arguments_for
|
51
|
+
end
|
data/lib/arguments/vm.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
gem 'ruby_parser', '>= 2.0.2'
|
2
|
+
require 'ruby_parser'
|
3
|
+
|
4
|
+
module Arguments
|
5
|
+
class PermissiveRubyParser < RubyParser
|
6
|
+
def on_error t, val, vstack
|
7
|
+
@rescue = vstack.first
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse str, file = "(string)"
|
11
|
+
super || @rescue
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.ast_for_method klass, method
|
16
|
+
source, line = klass.instance_method(method).source_location
|
17
|
+
str = IO.readlines( source )[ (line-1)..-1 ].join
|
18
|
+
ast = PermissiveRubyParser.new.parse( str )
|
19
|
+
ast.assoc( :defn ) or ast
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require "#{ dir = File.dirname __FILE__ }/../lib/arguments"
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
|
6
|
+
# TODO: Refactor specs for clarity and better coverage
|
7
|
+
describe Arguments do
|
8
|
+
|
9
|
+
before do
|
10
|
+
Object.send(:remove_const, 'Klass') rescue nil
|
11
|
+
load "#{ dir }/klass.rb"
|
12
|
+
@instance = Klass.new
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not respond to named_arguments" do
|
16
|
+
lambda { Klass.new.send( :named_arguments_for ) }.should raise_error( NoMethodError )
|
17
|
+
end
|
18
|
+
|
19
|
+
it "shouldn't break defaults" do
|
20
|
+
@instance.two(1).should == [1, 2, Klass.new]
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should allow passing named argument" do
|
24
|
+
Klass.send( :named_arguments_for, :two )
|
25
|
+
@instance.two(1, :three => 3).should == [1, 2, 3]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should raise ArgumentError if not passing required params" do
|
29
|
+
Klass.send( :named_arguments_for, :two )
|
30
|
+
error =
|
31
|
+
begin
|
32
|
+
@instance.two( :three => 3 )
|
33
|
+
rescue ArgumentError => e
|
34
|
+
e
|
35
|
+
end
|
36
|
+
error.to_s.should == "passing `one` is required"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should override passed value with hash" do
|
40
|
+
Klass.send( :named_arguments_for, :two )
|
41
|
+
@instance.two( :one => nil ).should == [nil, 2, Klass.new]
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should allow overriding with nil" do
|
45
|
+
Klass.send( :named_arguments_for, :two )
|
46
|
+
@instance.two( 1, :three => nil ).should == [1, 2, nil]
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should pass block" do
|
50
|
+
Klass.send( :named_arguments_for, :with_block )
|
51
|
+
@instance.with_block( 1, :three => nil, :two => 'something' ){ :block }.should == [1, 'something', nil, :block]
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should patch methods that accept proc as argument" do
|
55
|
+
Klass.send( :named_arguments_for, :with_block2 )
|
56
|
+
@instance.with_block2(1, :three => nil, :two => 'something'){ :block }.should == [1, 'something', nil, :block]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should override defaults on standard passing" do
|
60
|
+
Klass.send( :named_arguments_for, :asr )
|
61
|
+
@instance.asr(0, 1, :curve => 3).should == [0, 1, 1, 3]
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should work with class methods" do
|
65
|
+
(class << Klass; self; end).send( :named_arguments_for, :k_method )
|
66
|
+
Klass.k_method(:d => :d).should == [1, 2, 3, :d]
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should override defaults on standard passing" do
|
70
|
+
Klass.send( :named_arguments_for, 'self.k_method' )
|
71
|
+
Klass.k_method(:d => :d).should == [1, 2, 3, :d]
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should not use options if all arguments are passed" do
|
75
|
+
Klass.send( :named_arguments_for, :two )
|
76
|
+
@instance.two( 1, 2, :three => nil ).should == [1, 2, {:three => nil}]
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should raise ArgumentError if passing a not recoginized keyword" do
|
80
|
+
Klass.send( :named_arguments_for, :two )
|
81
|
+
error =
|
82
|
+
begin
|
83
|
+
@instance.two( 1, :four => nil )
|
84
|
+
rescue ArgumentError => e
|
85
|
+
e
|
86
|
+
end
|
87
|
+
error.to_s.should == "`four` is not a recognized argument keyword"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should raise ArgumentError if passing recoginized keywords" do
|
91
|
+
Klass.send( :named_arguments_for, :two )
|
92
|
+
error =
|
93
|
+
begin
|
94
|
+
@instance.two( 1, :four => nil, :five => nil )
|
95
|
+
rescue ArgumentError => e
|
96
|
+
e
|
97
|
+
end
|
98
|
+
error.to_s.should == "`four, five` are not recognized argument keywords"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should not patch methods that accept no args" do
|
102
|
+
Klass.send( :named_arguments_for, :no_args )
|
103
|
+
lambda { @instance.no_args(1) }.should raise_error(ArgumentError)
|
104
|
+
@instance.no_args.should be_nil
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should not patch methods that use splatter op" do
|
108
|
+
Klass.send( :named_arguments_for, :splatted )
|
109
|
+
@instance.splatted(1, :args => 1).should == [1, {:args => 1}]
|
110
|
+
|
111
|
+
Klass.send( :named_arguments_for, :splatted2 )
|
112
|
+
@instance.splatted2(:a => 1, :"*rest" => 3).should == [{:a => 1, :'*rest' => 3}, []]
|
113
|
+
|
114
|
+
Klass.send( :named_arguments_for, :splatted3 )
|
115
|
+
@instance.splatted3(:a => 1, :"*args" => 3).should == [{:a => 1, :"*args" => 3}, []]
|
116
|
+
@instance.splatted3(1, :b => 2, :args => 1).should == [1, [{:b => 2, :args => 1}]]
|
117
|
+
|
118
|
+
Klass.send( :named_arguments_for, :splatted4 )
|
119
|
+
@instance.splatted4(1, :b => 2, :args => 1).should == [1, {:b => 2, :args => 1}, []]
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should not patch methods with no optionals" do
|
123
|
+
Klass.send( :named_arguments_for, :no_opts )
|
124
|
+
@instance.method(:no_opts).arity.should == 3
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should patch all methods" do
|
128
|
+
Klass.send( :named_args )
|
129
|
+
@instance.two(1, :three => 3).should == [1, 2, 3]
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should benchmark without hack" do
|
133
|
+
puts Benchmark.measure {
|
134
|
+
1_000.times do
|
135
|
+
@instance.with_block( 1, :three => nil ){ :block }
|
136
|
+
end
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
it "should benchmark with hack" do
|
142
|
+
puts Benchmark.measure {
|
143
|
+
Klass.send( :named_arguments_for, :with_block )
|
144
|
+
}
|
145
|
+
puts Benchmark.measure {
|
146
|
+
1_000.times do
|
147
|
+
@instance.with_block( 1, :three => nil ){ :block }
|
148
|
+
end
|
149
|
+
}
|
150
|
+
end
|
151
|
+
end
|
data/spec/klass.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
class Klass
|
2
|
+
def three one = 1, two = 2, three = 3
|
3
|
+
[one, two, three]
|
4
|
+
end
|
5
|
+
|
6
|
+
def two one, two = 2, three = Klass.new
|
7
|
+
[one, two, three]
|
8
|
+
end
|
9
|
+
|
10
|
+
def with_block one, two = 2, three = 3
|
11
|
+
[one, two, three, yield]
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_block2 one, two = 2, three = 3, &action
|
15
|
+
[one, two, three, action.call]
|
16
|
+
end
|
17
|
+
|
18
|
+
def asr attackTime = 3, sustainLevel = 2, releaseTime = 1, curve = 0
|
19
|
+
[attackTime, sustainLevel, releaseTime, curve]
|
20
|
+
end
|
21
|
+
|
22
|
+
def no_args
|
23
|
+
end
|
24
|
+
|
25
|
+
def splatted *args
|
26
|
+
args
|
27
|
+
end
|
28
|
+
|
29
|
+
def splatted2 a=1, *rest
|
30
|
+
[a, rest]
|
31
|
+
end
|
32
|
+
|
33
|
+
def splatted3 a, *rest
|
34
|
+
[a, rest]
|
35
|
+
end
|
36
|
+
|
37
|
+
def splatted4 a, b=1, *rest
|
38
|
+
[a, b, rest]
|
39
|
+
end
|
40
|
+
|
41
|
+
def no_opts a, b, c
|
42
|
+
[a, b, c]
|
43
|
+
end
|
44
|
+
|
45
|
+
class << self
|
46
|
+
def asr attackTime = 3, sustainLevel = 2, releaseTime = 1, curve = 0
|
47
|
+
[attackTime, sustainLevel, releaseTime, curve]
|
48
|
+
end
|
49
|
+
|
50
|
+
def k_method a = 1, b = 2, c = 3, d = 4
|
51
|
+
[a, b, c, d]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def == other
|
56
|
+
self.class == other.class
|
57
|
+
end
|
58
|
+
end
|
data/spec/module.rb
ADDED
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arguments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.6"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Macario Ortega
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-17 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ruby_parser
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: ParseTree
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.3
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: ruby2ruby
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.1.9
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: rubyforge
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.0.3
|
54
|
+
version:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: gemcutter
|
57
|
+
type: :development
|
58
|
+
version_requirement:
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 0.3.0
|
64
|
+
version:
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: hoe
|
67
|
+
type: :development
|
68
|
+
version_requirement:
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 2.5.0
|
74
|
+
version:
|
75
|
+
description: |-
|
76
|
+
You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support.
|
77
|
+
Arguments has been tested with Ruby 1.8.6 and ruby 1.9.1 and eventually will work with JRuby (if someone is interested in contributing, I guess is possible since merb-action-args works with JRuby)
|
78
|
+
email:
|
79
|
+
- macarui@gmail.com
|
80
|
+
executables: []
|
81
|
+
|
82
|
+
extensions: []
|
83
|
+
|
84
|
+
extra_rdoc_files:
|
85
|
+
- History.txt
|
86
|
+
- Manifest.txt
|
87
|
+
files:
|
88
|
+
- History.txt
|
89
|
+
- Manifest.txt
|
90
|
+
- README.rdoc
|
91
|
+
- Rakefile
|
92
|
+
- arguments.gemspec
|
93
|
+
- lib/arguments.rb
|
94
|
+
- lib/arguments/class.rb
|
95
|
+
- lib/arguments/mri.rb
|
96
|
+
- lib/arguments/vm.rb
|
97
|
+
- spec/arguments_spec.rb
|
98
|
+
- spec/klass.rb
|
99
|
+
- spec/module.rb
|
100
|
+
has_rdoc: true
|
101
|
+
homepage: http://github.com/maca/arguments
|
102
|
+
licenses: []
|
103
|
+
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options:
|
106
|
+
- --main
|
107
|
+
- README.rdoc
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: "0"
|
115
|
+
version:
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: "0"
|
121
|
+
version:
|
122
|
+
requirements: []
|
123
|
+
|
124
|
+
rubyforge_project: arguments
|
125
|
+
rubygems_version: 1.3.5
|
126
|
+
signing_key:
|
127
|
+
specification_version: 3
|
128
|
+
summary: You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support
|
129
|
+
test_files: []
|
130
|
+
|