rubype 0.3.0 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11e9ed5d164e542b00c10907afbe3267e85782d4
4
- data.tar.gz: 9fd19b2b530147fb64d00e1f35a10d654c830f9a
3
+ metadata.gz: bbafcbd7c094043d0baef1fd0c51255d7cfcf5be
4
+ data.tar.gz: a8b1a0e69ad72db349e56527441b7ec021dddeea
5
5
  SHA512:
6
- metadata.gz: 727672e8759985e9d1a5dabbfe6082c9637bf43897028783d513ce24ca5ce697da54fde51e47c334de55e0b1a7b87390e3c0bdebf3fbc51224bfd9f515d405d8
7
- data.tar.gz: f0c745f01fc40523eab21e642c0d50368920304564bf14443f09c55ec693e604a5baf26ff553c6bafa32d551e608d38dc2f5925ad5e6a7433a423accff537b45
6
+ metadata.gz: f32e1eaeff1cbe50458f9a8fb6f0c8b40000b1f0bbec2c81180c5b07c99e13e3292efcfe8539a0b90f7ddcfdc23e79dcf76dee3ef58c8c60248c4a32b7a43b30
7
+ data.tar.gz: dcf8604ab00e4f535296c896494f6a02996381c2a387ff232a774d3e3a74388475cb990a37d4efe93946f4308cf4fe28a32e1f108937fe9cd246610c40fe271b
@@ -1,5 +1,9 @@
1
1
  ## CHANGELOG
2
2
 
3
+ ### 0.3.1
4
+
5
+ * Fix bugs.
6
+
3
7
  ### 0.3.0
4
8
 
5
9
  * First release with C ext.
data/README.md CHANGED
@@ -180,58 +180,23 @@ MyClass.new.method(:sum).return_type
180
180
 
181
181
  ## Benchmarks
182
182
 
183
- ```ruby
184
- require 'rubype'
185
- require 'benchmark'
186
-
187
- class RubypeCommonClass
188
- def sum(x, y)
189
- x + y
190
- end
191
- typesig :sum, [Numeric, Numeric] => Numeric
192
- end
193
-
194
- class CommonClass
195
- def sum(x, y)
196
- x + y
197
- end
198
- end
199
-
200
- class RubypeDucktypeClass
201
- def sum(x, y)
202
- x.to_i + y
203
- end
204
- typesig :sum, [:to_i, Numeric] => Numeric
205
- end
206
-
207
- class DucktypeClass
208
- def sum(x, y)
209
- x.to_i + y
210
- end
211
- end
212
-
213
- N = 100_000
214
- Benchmark.bm("RubypeDucktypeClass".length + 3) do |x|
215
- x.report("RubypeCommonClass") { N.times { RubypeCommonClass.new.sum(1, 5) } }
216
- x.report("CommonClass") { N.times { CommonClass.new.sum(1, 5) } }
217
- end
218
-
219
- Benchmark.bm("RubypeDucktypeClass".length + 3) do |x|
220
- x.report("RubypeDucktypeClass") { N.times { RubypeDucktypeClass.new.sum(1, 5) } }
221
- x.report("DucktypeClass") { N.times { DucktypeClass.new.sum(1, 5) } }
222
- end
223
- ```
183
+ #### result of `bundle exec rake benchmark`
224
184
 
225
- ### Results
226
- Ruby 2.2.0, Macbook Pro 2.9Ghz Intel Core i7, 8GB RAM
185
+ Ruby 2.2.1, Macbook Pro 2.7Ghz Intel Core i5, 8GB RAM
227
186
 
228
187
  ```
229
- user system total real
230
- RubypeCommonClass 0.530000 0.010000 0.540000 ( 0.566493)
231
- CommonClass 0.030000 0.000000 0.030000 ( 0.035718)
232
- user system total real
233
- RubypeDucktypeClass 0.590000 0.010000 0.600000 ( 0.682504)
234
- DucktypeClass 0.030000 0.000000 0.030000 ( 0.029856)
188
+ ruby version: 2.2.1
189
+ rubype version: 0.3.0
190
+ Calculating -------------------------------------
191
+ Pure Ruby 101.070k i/100ms
192
+ Rubype 63.973k i/100ms
193
+ -------------------------------------------------
194
+ Pure Ruby 7.115M (± 6.1%) i/s - 35.476M
195
+ Rubype 1.537M (± 2.5%) i/s - 7.677M
196
+
197
+ Comparison:
198
+ Pure Ruby: 7114786.0 i/s
199
+ Rubype: 1536611.5 i/s - 4.63x slower
235
200
  ```
236
201
 
237
202
 
@@ -40,17 +40,20 @@ VALUE expected_mes(VALUE expected)
40
40
  }
41
41
  }
42
42
 
43
+ #define assing_ivars VALUE meth_caller, meth, arg_types, rtn_type;\
44
+ meth_caller = rb_ivar_get(self, id_owner);\
45
+ meth = rb_ivar_get(self, id_meth);\
46
+ arg_types = rb_ivar_get(self, id_arg_types);\
47
+ rtn_type = rb_ivar_get(self, id_rtn_type);
48
+
43
49
  static VALUE
44
- rb_rubype_assert_type(VALUE self, VALUE args, VALUE rtn)
50
+ rb_rubype_assert_args_type(VALUE self, VALUE args)
45
51
  {
46
52
  int i;
47
53
  VALUE target;
48
- VALUE meth_caller, meth, arg_types, arg_type, rtn_type, arg;
54
+ VALUE arg, arg_type;
49
55
 
50
- meth_caller = rb_ivar_get(self, id_owner);
51
- meth = rb_ivar_get(self, id_meth);
52
- arg_types = rb_ivar_get(self, id_arg_types);
53
- rtn_type = rb_ivar_get(self, id_rtn_type);
56
+ assing_ivars
54
57
 
55
58
  for (i=0; i<RARRAY_LEN(args); i++) {
56
59
  arg = rb_ary_entry(args, i);
@@ -61,6 +64,14 @@ rb_rubype_assert_type(VALUE self, VALUE args, VALUE rtn)
61
64
  rb_raise(rb_eRubypeArgumentTypeError, error_fmt, target, expected_mes(arg_type), arg);
62
65
  }
63
66
  }
67
+ return Qnil;
68
+ }
69
+
70
+ static VALUE
71
+ rb_rubype_assert_rtn_type(VALUE self, VALUE rtn)
72
+ {
73
+ VALUE target;
74
+ assing_ivars
64
75
 
65
76
  if (unmatch_type_p(rtn, rtn_type)){
66
77
  target = rb_sprintf("%"PRIsVALUE"#%"PRIsVALUE"'s return", meth_caller, meth);
@@ -89,7 +100,8 @@ Init_rubype(void)
89
100
 
90
101
  rb_cContract = rb_define_class_under(rb_mRubype, "Contract", rb_cObject);
91
102
  rb_define_method(rb_cContract, "initialize", rb_rubype_initialize, 4);
92
- rb_define_method(rb_cContract, "assert_type", rb_rubype_assert_type, 2);
103
+ rb_define_method(rb_cContract, "assert_args_type", rb_rubype_assert_args_type, 1);
104
+ rb_define_method(rb_cContract, "assert_rtn_type", rb_rubype_assert_rtn_type, 1);
93
105
 
94
106
  id_meth = rb_intern("@meth");
95
107
  id_owner = rb_intern("@owner");
@@ -16,7 +16,8 @@ module Rubype
16
16
  @@typed_methods[owner][meth] = contract
17
17
  method_visibility = get_method_visibility(owner, meth)
18
18
  __rubype__.send(:define_method, meth) do |*args, &block|
19
- contract.assert_type(args, super(*args, &block))
19
+ contract.assert_args_type(args)
20
+ super(*args, &block).tap { |rtn| contract.assert_rtn_type(rtn) }
20
21
  end
21
22
 
22
23
  __rubype__.send(method_visibility, meth)
@@ -1,3 +1,3 @@
1
1
  module Rubype
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -132,6 +132,21 @@ class TestRubype < Minitest::Test
132
132
  end
133
133
  end
134
134
 
135
+ def test_for_readme
136
+ assert_raises(Rubype::ArgumentTypeError) do
137
+ eval <<-RUBY_CODE
138
+ class MyClass
139
+ def sum(x, y)
140
+ x.to_i + y
141
+ end
142
+ typesig :sum, [Numeric, Numeric] => Numeric
143
+ end
144
+
145
+ MyClass.new.sum(:has_no_to_i, 2)
146
+ RUBY_CODE
147
+ end
148
+ end
149
+
135
150
  private
136
151
  def assert_equal_to_s(str, val)
137
152
  assert_equal str, val.to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubype
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-19 00:00:00.000000000 Z
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler