Is it possible to identify aliased methods in Ruby?
In Ruby 1.9, aliased instance methods will be eql?
, so you can define:
class Module
def aliased_methods
instance_methods.group_by{|m| instance_method(m)}.
map(&:last).keep_if{|symbols| symbols.length > 1}
end
end
Now if you try it, you will get:
class Foo
def bar; 42 end
alias baz bar
def hello; 42 end
end
Foo.aliased_methods # => [[:bar, :baz]]
Array.aliased_methods # => [[:inspect, :to_s], [:length, :size]]
Note that some pairs are missing, e.g. [:map, :collect]
. This is due to a bug that is now fixed and will be in the next version (2.0.0) If it is important to you, you can roll your own group_by
without using hashes or eql?
and only using ==
.
How to test method alias ruby
You can use Object#method.
Test.method(:b) == Test.method(:a)
Find which alias method is called in rails model
def net_stock_quantity(alias_used = :net_stock_quantity)
method_called = caller[0]
#some code
end
The method_called
will contain the name of called alias.
Is there an elegant way to test if one instance method is an alias for another?
According to the documentation for Method,
Two method objects are equal if that
are bound to the same object and
contain the same body.
Calling Object#method
and comparing the Method
objects that it returns will verify that the methods are equivalent:
m.method(:bar) == m.method(:foo)
How can I obtain the aliases of of a method?
Not sure if iterating over all instance methods is considered "brute force", but this would work:
class Foo
def bar
end
alias bar2 bar
alias_method :bar3, :bar
end
Foo.instance_methods.select do |m|
Foo.instance_method(m) == Foo.instance_method(:bar)
end
#=> [:bar, :bar2, :bar3]
Alias a method to a single object
Singleton methods are contained in that object's singleton class:
class Object
def define_singleton_alias(new_name, old_name)
singleton_class.class_eval do
alias_method new_name, old_name
end
end
end
rob = 'Rob'
bob = 'Bob'
bob.define_singleton_alias :add_cuteness, :+
bob.add_cuteness 'by' # => "Bobby"
rob.add_cuteness 'by' # => NoMethodError
Object#define_singleton_method
basically does something like this:
def define_singleton_method(name, &block)
singleton_class.class_eval do
define_method name, &block
end
end
Assignment methods are not aliased properly
Setter methods need explicit receivers. Your code:
def attr= value ; a_attr= value ; end
is not calling the setter a_attr=
; it is assigning a value to a local variable a_attr
.
To do what you want, you need to do:
def attr= value; self.a_attr= value; end
Related Topics
How to Click First Link in List of Items After Upgrading to Capybara 2.0
How to Stub Applicationcontroller Method in Request Spec
Ruby Gems Returns "Command Not Found"
How Does Ruby's Sort_By {Rand} Work
How to Get Indexes of All Occurrences of a Pattern in a String
How to Downgrade from Ruby 1.9.2 to Ruby 1.8.7 to Run Rails 2.0.2
How to Find Out Who Is Connected to Actioncable
Use of Caret Symbol (^) in Ruby
Zlib in Ruby to Uncompress .Gz
How to Validate Xhtml with Nokogiri
Is Everything an Object in Ruby
How to Send Message Using Gmail API with Ruby Google API Client
How to Track the Execution Process of Ruby Program
Ruby on Rails Display Half a Star for a Decimal Rating, E.G. 4.5
What's the Point of the Prefix? Operator in Ruby 1.9
How to Dump an Object's Fields to the Console