# http://stackoverflow.com/q/38628521/5290909

regex = /^
		(\s*)              # matches the indentation (we'll backreference later)
		def\ +has_many\b   # literal "def has_many" with a word boundary
		(?:.*+\n)*?        # match whole lines - as few as possible
		\1                 # matches the same indentation as the def line
		end\b              # literal "end"
		/x

subject = %q|
  def has_many(name, scope = nil, options = {}, &extension)
      if association.nil?
        instance_variable_set("@#{association_name}", association)
      end
  end|
		

#Print matched text
puts subject.to_enum(:scan,regex).map {$&}