Skip to content

Instantly share code, notes, and snippets.

View k-tsj's full-sized avatar

Kazuki Tsujimoto k-tsj

View GitHub Profile
@k-tsj
k-tsj / gist:5180187
Last active December 15, 2015 01:29
ReplaceRepeated by Ruby
require 'pattern-match'
def replace_repeated(val, &block)
ret = match(val, &block)
if ret == val
ret
else
replace_repeated(ret, &block)
end
rescue PatternMatch::NoMatchingPatternError
@k-tsj
k-tsj / 0-report.txt
Created April 27, 2013 23:10
RGenGC(056c914e53c6) SEGV
$ make TESTS='--gc-stress objspace/test_objspace.rb pathname/test_pathname.rb' test-all
TestPathname#test_each_entry/home/kazuki/work/ruby/lib/pp.rb:165: [BUG] Segmentation fault
ruby 2.1.0dev (2013-04-25 trunk 40462) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0061 p:0008 s:0261 e:000259 METHOD /home/kazuki/work/ruby/lib/pp.rb:165
c:0060 p:0011 s:0255 e:000254 BLOCK /home/kazuki/work/ruby/lib/pp.rb:356
c:0059 p:0029 s:0252 e:000251 BLOCK /home/kazuki/work/ruby/lib/pp.rb:247 [FINISH]
c:0058 p:---- s:0249 e:000248 CFUNC :each
@k-tsj
k-tsj / gist:7854083
Created December 8, 2013 06:55
Extend rb_scan_args to get keyword arguments
diff --git a/array.c b/array.c
index 9a93d7f..b673c63 100644
--- a/array.c
+++ b/array.c
@@ -4429,7 +4429,7 @@ static VALUE sym_random;
static VALUE
rb_ary_shuffle_bang(int argc, VALUE *argv, VALUE ary)
{
- VALUE opts, randgen = rb_cRandom;
+ VALUE rnd, randgen = rb_cRandom;
@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
@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
require 'pattern-match'
require 'set'
module PatternMatch
class PatternSetDeconstructor < PatternDeconstructor
def initialize(klass, *subpatterns)
super(*subpatterns)
@klass = klass
end
@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);
# 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]
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'
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)