How can I reverse ruby's include function
module Mod
def foo
puts "fooing"
end
end
class C
include Mod
def self.remove_module(m)
m.instance_methods.each{|m| undef_method(m)}
end
end
>> c = C.new
>> c.foo
fooing
>> C.remove_module(Mod)
=> ["foo"]
>> c.foo
NoMethodError: undefined method `foo' for #< C:0x11b0d90>
Ruby: Is there an opposite of include? for Ruby Arrays?
if @players.exclude?(p.name)
...
end
ActiveSupport adds the exclude?
method to Array
, Hash
, and String
. This is not pure Ruby, but is used by a LOT of rubyists.
Source: Active Support Core Extensions (Rails Guides)
reverse include for array?
There are things you can do that would be nicer. You could use Enumerable#any?
rather than iterating by hand:
array.any? { |item| string.include?(item) }
Or you could use Regexp.union
:
string =~ Regexp.union(array)
# or if you want a true boolean
!!(string =~ Regexp.union(array))
!(string =~ Regexp.union(array)).nil?
Is it possible to reverse the included module in a class?
The details vary by implementation, but I know that at least in JRuby and MRI 1.8 there is a construct called an Include Class that is inserted in to the inheritance chain of a class when a Module is extended or included. The Module therefore won't be garbage collected, since the Include Class still refers to it, and the methods will still be on your class. There are some great articles by Patrick Farley on this and related topics in his blog.
So, to "remove" a module, you could individually undefine each method that came from the module, but that's a pretty unwieldy mechanism for that purpose. If using a gem is acceptable to you, it would probably be better would be to use Mixology, which was designed specifically for the purpose of adding and removing modules dynamically.
Unable to reverse each word in array in Ruby ( Without built-in function reverse)
You are trying to create a range with start value higher than end value and Ruby treats it as empty array, for example:
(5..0).to_a
=> []
In this example Ruby can't find number that is higher than 5
and lower than 0
, so it returns an empty array.
You can try following code to iterate from higher to lower number:
(5).downto(0).each do |number|
...
end
Related Topics
Passing Headers and Query Params in Httparty
How to Unfreeze an Object in Ruby
How to Spawn a Child Process in Ruby
Handling Namespace Models (Classes) in Namespace
How to Share Variables Across My .Rb Files
Getting the Highest Value of a Column in Mongodb
Rails 3 Additional Session Configuration Options (Key, Expires_After, Secure)
How to Spawn an Eventmachine "Inside" a Rails App
Eager Loading: the Right Way to Do Things
How to Convert PDF Files to Images Using Rmagick and Ruby
How to Check If a Variable Is a Number or a String
Get Class Location from Class Object
Sum 2 Hashes Attributes with the Same Key
Rails Link_To External Site, Url Is Attribute of User Table, Like: @Users.Website
Cannot Load Such File -- Readline (Loaderror) When Running Heroku Create --Stack Cedar