calling module method into another module in Ruby
You stumbled over the difference of include
and extend
.
include
makes the method in the included module available to instances of your classextend
makes the methods in the included module available in the class
self.method_name
and you access self
within that method, self
is bound to the current class. check_expiry
, however, is included and thus only available on the instance side.
To fix the problem either extend CardExpiry
, or make check_expiry
a class method.
Calling selected methods of a module from another module
That module A
should be raising an ArgumentError
unless it also has an extends ActiveSupport::Concern
at the top. Without ActiveSupport::Concern
, you'd be calling the Module#included
instance method here:
included do
...
end
but that requires an argument.If you say this:
module A
extend ActiveSupport::Concern
included do
def self.some_func
end
end
end
then you'll get the included
you're trying to use and the module A
that you're expecting.Also, module B
doesn't include A
so it has nowhere to get some_func
from so this:
module B
some_func
end
will give you a NoMethodError
. If you include A
:module B
include A
some_func
end
then it will work. How to access method defined in a module inside another module in Ruby?
Assuming you don't want class level methods, you can also include the module into a class, instantiate an object of that class and call whoa
:
class C
include A::B
end
c = C.new
c.whoa
# Whoa!
How to call a method from a module of an other ruby file
Require needs the absolute path to the file unless the file is located in one of Ruby's load paths. You can view the default load paths with puts $:
. It is common to do one of the following to load a file:
Add the main file's directory to the load path and then use relative paths with require:
$: << File.dirname(__FILE__)
require "my_module"
Ruby 1.8 code that only loads a single file will often contain a one-liner like:require File.expand_path("../my_module", __FILE__)
Ruby 1.9 added require_relative:require_relative "my_module"
In the module you will need to define the methods as class methods, or use Module#module_function:module MyModule
def self.method1 ary
...
end
def method2
...
end
module_function :method2
end
a = [1,2,3,4]
MyModule.method1(a)
call a method of a module from another
Try to define check_mail in helpers/mail_check and then include MailCheck in user_controller.
Is it possible to call a Module function from inside a class that is also in that module
In order to invoke M::helper
you need to define it as def self.helper; end
For the sake of comparison, take a look at helper and helper2 in the following modified snippet
module M
class C < Struct.new(:param)
include M # include module to get helper2 mixed in
def work
M::helper(param)
helper2(param)
end
end
def self.helper(param)
puts "hello #{param}"
end
def helper2(param)
puts "Mixed in hello #{param}"
end
end
c = M::C.new("world")
c.work
Including a module in other module
You can't.
When you include
a mixin M
into a class C
, Ruby creates a new class ⟦M′⟧
whose method table points to the method table of the mixin M
and whose superclass is the superclass of C
, then makes this class the new superclass of C
. This is repeated for every mixin that was mixed into M
.
Note that this algorithm is run only once, when you mix M
into C
. Modules that get include
d later, will not get considered.
Related Topics
How to Avoid Circular Creation of Associated Models in Factory_Girl
Clean Install Osx 10.9.1 Returns "Undefined Method 'Path2Class'" When Trying to Install Gems
Recommended Way to Generate a Presigned Url to S3 Bucket in Ruby
Overriding Instance Variable Array's Operators in Ruby
Can Optionparser Skip Unknown Options, to Be Processed Later in a Ruby Program
How to Remove Backslashes from a JSON String
Nomethoderror: Undefined Method 'On' for Main:Object
How to Validate This Hash Parameter
How to Resize Image Only If Width Exceeds with Graphics/Image Magick
How to Handle Omniauth Callbacks in Multiple Environments
Escaping Strings for Ruby Sqlite Insert
How to Get All Message History from Hipchat for a Room via The API
Indent Multiline String in Erb
Interpolation Within Single Quotes
Segmentation Fault with Ruby 1.8.7
When Is a Block or Object That Is Passed to Hash.New Created or Run
Devise with Ruby on Rails - Force User to Change Password on First Login