Last active
July 20, 2020 05:03
-
-
Save r7kamura/1dc91ed5ab03fa0a1645e9f155d4887f 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
shared_examples "foo" do | |
# ここで "foo" を参照したいのですが、何か方法はありませんか? | |
end |
shared_examples "foo" do
File.foreach(__FILE__).take(__LINE__ - 1).last[/"(.+)"/, 1] #=> "foo"
end
なかなか案が出ないので、マクドナルド理論として、この方法を提案しておきます。
ちなみに、現実のプロジェクトでは以下のようなコードで対処することにしました。
require "active_support/concern"
module MyApplication
module SpecHelpers
module SharedExample
extend ::ActiveSupport::Concern
module ClassMethods
# @param [String] name
def shared_example(name, &block)
shared_examples name do
it name, &block
end
end
end
end
end
end
RSpec.configuration.include(MyApplication::SpecHelpers::SharedExample)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
仮にこういうコードを書くケースがあるとして、このように二重に定義するのを防ぎたいというのが背景にある目的のひとつです。