Skip to content

Instantly share code, notes, and snippets.

View k-tsj's full-sized avatar

Kazuki Tsujimoto k-tsj

View GitHub Profile
$ bin/ruby -v
ruby 2.4.0dev (2016-09-12 master 56141) [x86_64-linux]
$ bin/ri
ri 5.0.0.beta2
$ bin/ri String
= String < Object
------------------------------------------------------------------------------
module DidYouMean
class MethodNameChecker
prepend Module.new {
PY2RB = {
String => {
lower: %i(downcase)
},
Object => {
len: %i(obj.length)
}
user system total real
Yhpg.new(actual).output == expect 0.210000 0.010000 0.220000 ( 0.228040)
assert { Yhpg.itself.new(actual).output == expect } 1.650000 0.000000 1.650000 ( 1.650489)
assert { Yhpg.new(actual).output == expect } 115.110000 0.000000 115.110000 (115.058014)
ruby -v -rstringio -rbenchmark -e 'N=1_000_000;Benchmark.bm(5){|x| x.report("String#<<"){s="";N.times{s<<"a"}}; x.report("Array#<<+join"){a=[];N.times{a<<"a"};a.join}; x.report("StringIO#<<"){i=StringIO.new;N.times{i<<"a"};i.to_s}};'
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
user system total real
String#<< 0.150000 0.000000 0.150000 ( 0.149649)
Array#<<+join 0.200000 0.030000 0.230000 ( 0.231170)
StringIO#<< 0.250000 0.010000 0.260000 ( 0.255604)
diff --git a/lib/power_assert.rb b/lib/power_assert.rb
index 1a19d07..51064f1 100644
--- a/lib/power_assert.rb
+++ b/lib/power_assert.rb
@@ -5,7 +5,23 @@
require 'power_assert/version'
require 'ripper'
-require 'pattern-match'
+require 'pattern-match/core'
# power_assert.rb
#
# Copyright (C) 2014 Kazuki Tsujimoto, All rights reserved.
# License: BSDL
#
# $ ruby power_assert.rb
# "0".class == "3".to_i.times.map {|i| i + 1 }.class
# | | | | |
# | | | | Array
# | | | [1, 2, 3]
@k-tsj
k-tsj / gist:9894424
Created March 31, 2014 15:04
Patch for SymbolGC SEGV
diff --git a/parse.y b/parse.y
index 3f756ce..680242d 100644
--- a/parse.y
+++ b/parse.y
@@ -10705,7 +10705,13 @@ rb_str_dynamic_intern(VALUE str)
ID id, type;
if (st_lookup(global_symbols.sym_id, str, &id)) {
- return ID2SYM(id);
+ VALUE sym = ID2SYM(id);
require 'pattern-match'
require 'set'
module PatternMatch
class PatternSetDeconstructor < PatternDeconstructor
def initialize(klass, *subpatterns)
super(*subpatterns)
@klass = klass
end
@k-tsj
k-tsj / main.rb
Created December 28, 2013 10:02
Object#assert_pattern
require 'pattern-match'
class Object
def assert_pattern(pattern)
match(self) do
with(Kernel.eval(pattern, Kernel.binding)) { self }
end
end
end
@k-tsj
k-tsj / main.rb
Created December 28, 2013 09:50
Type(pattern) annotations for Ruby
require 'pattern-match'
class Module
def check(pattern, mid)
prepend Module.new {
define_method(mid) do |*args|
ret = super(*args)
match(ret) do
with(Kernel.eval(pattern, Kernel.binding)) { ret }
end