Forum: Ruby on Rails Include Case-Insensitive

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sam K. (Guest)
on 2007-03-25 22:29
Is there a way to make include? case insensitive?
Eden L. (Guest)
on 2007-03-25 23:27
(Received via mailing list)
Sort of.  You can't expect to change ruby's constant system to be
insensitive, but if you're OK with passing in strings, this might
work:

class Module
  def include_insensitive(*args)
    args.each do |m|
      include(const_get(Module.constants.grep(/^#{m}$/i).first))
    end
    self
  end
end

module M
  def im_in_m
    :hi
  end
end

class C
  include_insensitive "m"
end

C.new.im_in_m #=> :hi

On Mar 26, 2:29 am, Sam K. <removed_email_address@domain.invalid>
François T. (Guest)
on 2008-09-30 17:57
Sam K. wrote:
> Is there a way to make include? case insensitive?

You can also use the downcase() function :

"Hello World".downcase.include?("hello") => true
This topic is locked and can not be replied to.