Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby 2.0 How do I uninclude a module out from a module after including it?

module X
end

module Y
end

module Z
  #TODO include X replacement of including Y
  #TODO include Y replacement of including X
end

Is there a way to work around the fact that ruby contains no uninclude keyword??

like image 301
zotherstupidguy Avatar asked Oct 19 '13 03:10

zotherstupidguy


1 Answers

If you really need this kind of functionality, you could probably do it by using refinements.

class Foo
end

module X
  def x
    puts 'x'
  end
end

module Y
end

module R
  refine Foo do
    include X
    include Y
  end
end

# In a separate file or class
using R
# Foo now includes X and Y
Foo.new.x

# In a different file or class
# Foo no longer includes X and Y
Foo.new.x # NoMethodError
like image 126
davogones Avatar answered Oct 02 '22 13:10

davogones