I am using linkedin module for Rails. I would like to add a method to class LinkedIn::Mash in this module. I do add a mash.rb file to /models/concerns (I am using Rails 4):
module LinkedIn
class Mash
def my_method
end
end
end
However, this doesn't work:
my_mash_ojbect.respond_to? :my_method #=> false
How can I fix this?
Try
LinkedIn::Mash.instance_eval do
def my_method
end
end
Have a look at this if you want to read more about instance_eval or read metaprogramming ruby book
Related
I've created a helper which I'd like to use for text manipulation
module ApplicationHelper
module TextHelper
extend ActionView::Helpers
end
end
However when I run ApplicationHelper::TextHelper.simple_format "foo" in Rails console I get
NoMethodError: undefined method `white_list_sanitizer' for Module:Class
Is there anything else I need included?
I have already looked at https://github.com/rails/rails/issues/13837 but it didn't work.
Using Rails 4, Ruby 1.9.3
If you're in the console, you should be able to just do helper.simple_format('hi'). The helper method is available in console as a way to call some helper methods.
When using a custom helper:
# app/helpers/custom_helper.rb
module CustomHelper
def custom_method(x)
puts "Custom method #{x}"
end
end
# from the console
helper.custom_method('hi')
# from the controller
class SomeController < ApplicationController
def index
view_context.custom_method('hi')
end
end
I want to include a module in a rails helper(is also a module).
The helper is:
module SportHelper
.....
end
And the module is:
module Formula
def say()
....
end
end
Now, I want to use the method say in SportHelper. What should I do?
If I write like this:
module SportHelper
def speak1()
require 'formula'
extend Formula
say()
end
def speak2()
require 'formula'
extend Formula
say()
end
end
This will work, but I don't want to do so, I just want to add the methods on the helper module,not every methods.
You need just to include this module in your helper:
require 'formula'
module SportHelper
include Formula
def speak1
say
end
def speak2
say
end
end
Maybe you don't need this line require 'formula', if it's already in the load path. For check this you can inspect $LOAD_PATH variable. For more information see this answer.
Basic difference between extend and include is that include is for adding methods to an instance of a class and extend is for adding class methods.
module Foo
def foo
puts 'heyyyyoooo!'
end
end
class Bar
include Foo
end
Bar.new.foo # heyyyyoooo!
Bar.foo # NoMethodError: undefined method ‘foo’ for Bar:Class
class Baz
extend Foo
end
Baz.foo # heyyyyoooo!
Baz.new.foo # NoMethodError: undefined method ‘foo’ for #<Baz:0x1e708>
And if you use extend inside the object method, it will adding methods to an instance of a class, but they would be available only inside this one method.
I think directly include should work
module SportHelper
include SportHelper
.........
end
end
I tested like below:
module A
def test
puts "aaaa"
end
end
module B
include A
def test1
test
end
end
class C
include B
end
c = C.new()
c.test1 #=> aaaa
It should work.
I am working on a Ruby gem (aimed at Rails 4) and I want to be able to use the url helpers in it. So I've tried adding include Rails.application.routes.url_helpers, but it results in "undefined method 'routes' for nil:NilClass (NoMethodError)."
I am unsure as to why it does this as I've tested in the console and when I included it there it does just fine.
The class and module is simple just:
module DynamicMenu
class Base
include Rails.application.routes.url_helpers
def get_menu_links
#links
end
private
def define_menu_links(args)
#links = args
end
end
end
Any ideas on how to fix this? I've tried including diffent modules, such as ActionView::Helpers, but that doesn't appear to work either.
One way to do it, though probably not the best way is to use included and method_missing. This is of course assuming that you are in ActionView or ActionController
Something like this should do OK:
module DynamicMenu
def included(base)
#parent = base
end
def method_missing method, *args
super unless #parent.methods.index method
#parent.send(method,args)
end
end
I am using Ruby v1.9.2 and the Ruby on Rails v3.2.2 gem. I had the following module
module MyModule
extend ActiveSupport::Concern
included do
def self.my_method(arg1, arg2)
...
end
end
end
and I wanted to alias the class method my_method. So, I stated the following (not working) code:
module MyModule
extend ActiveSupport::Concern
included do
def self.my_method(arg1, arg2)
...
end
# Note: the following code doesn't work (it raises "NameError: undefined
# local variable or method `new_name' for #<Class:0x00000101412b00>").
def self.alias_class_method(new_name, old_name)
class << self
alias_method new_name, old_name
end
end
alias_class_method :my_new_method, :my_method
end
end
In other words, I thought to extend the Module class someway in order to add an alias_class_method method available throughout MyModule. However, I would like to make it to work and to be available in all my Ruby on Rails application.
Where I should put the file related to the Ruby core extension of the Module class? Maybe in the Ruby on Rails lib directory?
How should I properly "extend" the Module class in the core extension file?
Is it the right way to proceed? That is, for example, should I "extend" another class (Object, BasicObject, Kernel, ...) rather than Module? or, should I avoid implementing the mentioned core extension at all?
But, more important, is there a Ruby feature that makes what I am trying to accomplish so that I don't have to extend its classes?
You could use define_singleton_method to wrap your old method under a new name, like so:
module MyModule
def alias_class_method(new_name, old_name)
define_singleton_method(new_name) { old_name }
end
end
class MyClass
def my_method
puts "my method"
end
end
MyClass.extend(MyModule)
MyClass.alias_class_method(:my_new_method, :my_method)
MyClass.my_new_method # => "my method"
Answering your comment, you wouldn't have to extend every single class by hand. The define_singleton_method is implemented in the Object class. So you could simply extend the Object class, so every class should have the method available...
Object.extend(MyModule)
Put this in an initializer in your Rails app and you should be good to go...
I found an answer on this website: http://engineering.lonelyplanet.com/2012/12/09/monitoring-our-applications-ruby-methods/
The solution is to use class_eval with a block. That enables using variables from the enclosing scope.
module Alias
def trigger
#trigger = true
end
def method_added(name)
if #trigger
#trigger = false
with_x = "#{name}_with_x"
without_x = "#{name}_without_x"
define_method(with_x) do
"#{send(without_x)} with x"
end
alias_method without_x, name
alias_method name, with_x
end
end
def singleton_method_added(name)
if #trigger
#trigger = false
with_x = "#{name}_with_x"
without_x = "#{name}_without_x"
define_singleton_method(with_x) do
"singleton #{send(without_x)} with x"
end
singleton_class.class_eval do
alias_method without_x, name
alias_method name, with_x
end
end
end
end
class TestAlias
extend Alias
trigger
def self.foo
'foo'
end
trigger
def bar
'bar'
end
end
TestAlias.foo # => 'singleton foo with x'
TestAlias.new.bar # => 'bar with x'
If you don't have singleton_class then you should probably upgrade your version of Ruby. If that's not possible you can do this:
class Object
def singleton_class
class << self
self
end
end
end
The accepted answer was confusing and did not work.
class Module
def alias_class_method(new_name, old_name)
define_singleton_method(new_name, singleton_method(old_name))
end
end
module MyModule
def self.my_method
'my method'
end
end
MyModule.alias_class_method(:my_new_method, :my_method)
MyModule.my_new_method # => "my_method"
I wanted to add a method to two models, so I made a module like this and included it in both models.
module UserReputation
def check_something
...
end
end
That worked fine. I then wanted to have that method called as an :after_create on all those models. It works if I add it manually to all the models, but I wanted to be smart and include it in the module like this:
module UserReputation
after_create :check_something
def check_something
...
end
end
But this doesn't work. Is there any way to accomplish this and DRY up the after_create as well?
Try self.included, which is called when the module is mixed into the class base:
module UserReputation
def self.included(base)
base.after_create :check_something
end
end