LoginSignup
8
0

More than 5 years have passed since last update.

Rubyでif xにネストしたunless xの中に実行到達する方法

Last updated at Posted at 2018-02-22

このRubyの問題を解いてみました。ネタバレが含まれるので自分で解きたい人は今すぐ戻るように!

やりかた

if xやunless xが実行されるまでの間にローカル変数xの値を書き換える

ruby_is_awesome.rb
def f(x)
  if x
    unless x
      p :foo
    end
  end
end

TracePoint.trace(:line) do |tp|
  if tp.lineno == 2
    tp.binding.local_variable_set(:x, true)
  end

  if tp.lineno == 3
    tp.binding.local_variable_set(:x, false)
  end
end

f("whatever")
# foo

f(nil)
# foo

f(false)
# foo
8
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
0