-
-
Save tompng/cdacf00f21c0d756e4bc76a8c58ac1e5 to your computer and use it in GitHub Desktop.
それぞれのメソッドで「戻り値になるのはこれとこれ」っていうリストを作りたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# こういうことをしてくれる nanika_no_method が欲しい | |
# Ripper とかそこらへん使えばいけるのだろうか | |
nanika_no_method(<<EOS) | |
def gongo1 | |
'a' | |
end | |
EOS | |
# => ['a'] | |
nanika_no_method(<<EOS) | |
def gongo2(num) | |
num.odd? ? 'b' : 'c' | |
end | |
EOS | |
# => ['b', 'c'] | |
nanika_no_method(<<EOS) | |
def gongo3(num1, num2) | |
return 'd' if num1.odd? | |
num2.odd? ? 'e' : 'f' | |
end | |
EOS | |
# => ['d', 'e', 'f'] | |
nanika_no_method(<<EOS) | |
def gongo4(num1, num2) | |
num1 + num2 | |
end | |
EOS | |
# => こういうのは対応しなくてOK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
typeprofだと型情報が正確に出てすごい