Questions tagged [alias-method]
32 questions
27
votes
1 answer
Why alias_method fails in Rails model
class Country < ActiveRecord::Base
#alias_method :name, :langEN # here fails
#alias_method :name=, :langEN=
#attr_accessible :name
def name; langEN end # here works
end
In first call alias_method fails with:
NameError: undefined method…

sites
- 21,417
- 17
- 87
- 146
16
votes
5 answers
Is there an alias_method for a class method?
Consider the following class:
class Foo
def an_inst_method
'instance method'
end
def self.a_class_method
'class method'
end
alias_method :a_new_inst_method, :an_inst_method
end
This is no issue and you can call…

aarona
- 35,986
- 41
- 138
- 186
14
votes
2 answers
Unexpected value of __callee__ when including a module – is this a Ruby bug?
When invoked via a method created by alias_method, __callee__ ignores the name of the old method (here xxx) and returns the name of the new method, as below:
class Foo
def xxx() __callee__ end
alias_method :foo, :xxx
end
Foo.new.foo # =>…

user513951
- 12,445
- 7
- 65
- 82
13
votes
1 answer
alias_method and class_methods don't mix?
I've been trying to tinker with a global Cache module, but I can't figure out why this isn't working.
Does anyone have any suggestions?
This is the error:
NameError: undefined method `get' for module `Cache'
from (irb):21:in `alias_method'
...…

Daniel
- 7,006
- 7
- 43
- 49
12
votes
3 answers
Alias a method multiple times
I wish to put two aliases for one original method, but i don't see the ability of alias_method to do multiple aliases at once, rather one by one.
So is there a possibility to change from this:
alias_method :aliased_first_method,…

Zippie
- 6,018
- 6
- 31
- 46
11
votes
3 answers
Is there an elegant way to test if one instance method is an alias for another?
In a unit test I need to test whether alias methods defined by alias_method have been properly defined. I could simply use the same tests on the aliases used for their originals, but I'm wondering whether there's a more definitive or efficient…

Mori
- 27,279
- 10
- 68
- 73
7
votes
1 answer
what is the difference between alias_method and alias_method_chain?
I was working on my web-application and I wanted to override a method for example if the original class is
class A
def foo
'original'
end
end
I want to override foo method it can be done like this
class A
alias_method :old_foo, :foo
…

H.Elsayed
- 431
- 2
- 11
6
votes
1 answer
undefined method `render' for class `ActionView::Base'
When I start the application crashes following error
/home/stereodenis/.rvm/gems/ruby-1.9.3-p194@nyanya/gems/haml-3.1.6/lib/haml/helpers/action_view_mods.rb:15:in `alias_method': undefined method `render' for class `ActionView::Base'…

stereodenis
- 3,618
- 2
- 22
- 27
6
votes
2 answers
Can I remove a method alias in Ruby?
Suppose I've got a section of Ruby code where I'd like to alias a method (I don't know why; let's just suppose I have a good reason).
class String
alias_method :contains?, :include?
end
Would it be possible for me, after this section, to remove…

Dan Tao
- 125,917
- 54
- 300
- 447
4
votes
3 answers
`alias_method` a private method
I have a class that exposes two interface methods client_options and user_options, and at this ancestry level, they are equivalent to default_options. I don't want other developers to implement default_options directly, hence it's private.
class…

equivalent8
- 13,754
- 8
- 81
- 109
4
votes
1 answer
Ruby/Rails: alias_method practices
I'm trying to override Rails' "fields_for" method, which I'm currently doing as follows:
module ActionView::Helpers::FormHelper
include ActionView::Helpers::FormTagHelper
alias_method :original_fields_for, :fields_for
def fields_for(

PlankTon
- 12,443
- 16
- 84
- 153
3
votes
1 answer
Ruby: alias_method for module static method
Given this module
module Test
def self.foo(v)
puts "Test.foo with #{v}"
end
end
The following doesn't work
module Test
alias_method :bar, :foo
# ...
end
although it works for instance methods. I get following error
NameError:…

chepukha
- 2,371
- 3
- 28
- 40
3
votes
1 answer
An efficient version/alternative to the alias method that samples without replacement
I am writing a quiz/teaching game in HTML5/JS in which the player is offered a set of 10 questions from a larger mastery set. The game keeps track of the players' scores over time and is more likely to choose questions from the question list that…

TheHans255
- 2,059
- 1
- 19
- 36
3
votes
1 answer
Chain of Responsibility and alias_method problems in Ruby
I'm trying to implement the chain of responsibility pattern in Ruby and ActiveRecord for a polymorphic object. I'm having a few problems.
Sometimes I get an error that a method is not defined when I try to alias_method it, I think this is because…

David
- 537
- 3
- 14
3
votes
3 answers
Alias method chain in JavaScript?
In JavaScript, how could you create a new function with the same name as an existing function, while also preserving the original function so it could be called from within the new one?
user65663