Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / chudnovsky.rb
Last active April 5, 2024 04:26 — forked from fetburner/chudnovsky.rb
Chudnovskyの公式で円周率を計算するプログラム
def series(l, r)
if r - l <= 1
p = (2 * r - 1) * (6 * r - 5) * (6 * r - 1)
q = r * r * r * 10939058860032000
a = (r % 2 == 0 ? 1 : -1) * (13591409 + 545140134 * r)
[p, q, a * p]
else
m = (l + r) / 2
p0, q0, t0 = series(l, m)
@tompng
tompng / gongo.rb
Last active January 28, 2022 01:15 — forked from gongo/gongo.rb
それぞれのメソッドで「戻り値になるのはこれとこれ」っていうリストを作りたい
# https://gist.github.com/gongo/c404167ae49165acd4df63c27ae1a54c
# iseqの中から [:putobject, object], :RUBY_EVENT_RETURN や [:putstring, "string"], ?, :RUBY_EVENT_RETURN を雑に探す作戦
def nanika_no_method(code)
flat_iseq = RubyVM::InstructionSequence.new(code).to_a.flatten
flat_iseq.select.with_index do |s, idx|
flat_iseq[idx + 1, 2].include?(:RUBY_EVENT_RETURN) && %i[putobject putstring].include?(flat_iseq[idx - 1])
end.uniq
end
@tompng
tompng / goto.rb
Last active December 16, 2015 15:36 — forked from hanachin/double.rb
ブロック中のでgotoが使えるメソッド書いてみた
#! ./ruby -I.ext/x86_64-darwin14 --disable-gems
require '-test-/iseq_load/iseq_load'
def spaghetti(&block)
iseq_data_magic_index = 0
iseq_data_major_version_index = 1
iseq_data_minor_version_index = 2
iseq_data_format_type_index = 3
iseq_data_misc_index = 4
iseq_data_label_index = 5
@tompng
tompng / gist:5949809
Last active December 19, 2015 11:48 — forked from webcrafts/gist:5948862
#coding: utf-8
class Te
attr_accessor :te_num
def initialize num=nil
self.te_num = num ? num % 3 : rand(3)
end
def > te
(te_num + 1) % 3 == te.te_num