Question

How can I use the alias_method method in Ruby to make a copy of the []...

How can I use the alias_method method in Ruby to make a copy of the [] method in the Array class

Homework Answers

Answer #1

create aliases for a method and variable name in ruby. This can be helpful if you want to override the behavior of some method without changing the origin implementation of it. alias_method take a new_name as a copy name of the old_name and it has the following syntax.

Syntax :

alias_method (new_name, old_name)

Example

class Davi

def capital puts "Karaz-a-Karak"

end

alias_method :orig_capital, :capital

def capital puts "Karaz-a-Karak rebuild"

orig_capital

end

end

davi = Davi.new

davi.capital

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I have a method that create the grid, but I want to use the grid and...
I have a method that create the grid, but I want to use the grid and change the grid in other method, how do I have the grid when I call the method 2. class: mian: If method 2 create_grid method method 2 method 3 java please
Code a valid copy constructor for the GrabBag class below: class GrabBag { private: int* values;...
Code a valid copy constructor for the GrabBag class below: class GrabBag { private: int* values; int size; public: GrabBag(); }; #include "GrabBag.h" GrabBag::GrabBag() { size = rand() % 100 + 1;   values = new int[size]; //random array size   for (int i = 0; i < size; i++)   values[i] = rand()% 10 + 1 ;// random array contents   }   
Part 1 I need to make an example class that I make up on my own....
Part 1 I need to make an example class that I make up on my own. It should have some variables, at least one function and at least one procedure in it. And then I should create two instances of the class and demonstrate how to use the class by calling some of the procedures and functions. What I make my class about is up to me. Part 2 Do a simple example of ByVal vs ByRef. I can do...
Block copy, and paste, the argument into the window below. Use the short method or a...
Block copy, and paste, the argument into the window below. Use the short method or a truth table, and write if the argument is valid or invalid. If you use the short method, use a forward slash / for a check mark, or just write ok. If you use the truth table method, put an X after the line that proves that the argument is invalid. Block copy, and paste, the argument into the window below. Use the short method...
How can I write a function in MATLAB with an input of array1 and an output...
How can I write a function in MATLAB with an input of array1 and an output of maxValue (being the highest value in array 1) without calling the built in function in MATLAB. I ned to use a loop to accomplish this.
How can this PHP code be changed to use a parameter to the function that you...
How can this PHP code be changed to use a parameter to the function that you would then pass $students in? (instead of using a global). Also, can you show how to use a loop to handle any length of array? (instead of an array that is 3 elements). <?php $students = array(     array('FName'=>'Jane', 'LName'=>'Doe', 'ClassScore'=>90),     array('FName'=>'Bob', 'LName'=>'Joe', 'ClassScore'=>50),     array('FName'=>'John', 'LName'=>'Doe', 'ClassScore'=>20)     );     //1. CalculateSum() Function     function CalculateSum()     {         // Use $students array throughout the function.         global $students;         // Add...
ONLY use method of alligation to solve the problem. I already know the answer, I want...
ONLY use method of alligation to solve the problem. I already know the answer, I want to know how to solve it with alligation. IF YOU DON'T KNOW WHAT ALLIGATION IS, DON'T ANSWER THE QUESTION. THIS IS FOR A PHARMACY EXAM. A health food store sells a mixture of raisins and roasted nuts. Raisins sell for $3.50/kg and nuts sell for $4.75/kg. How many kilograms of each should be mixed to make 20 kg of this snack worth $4.00/kg? ONLY...
I need new and unique answers, please. (Use your own words, don't copy and paste), Please...
I need new and unique answers, please. (Use your own words, don't copy and paste), Please Use your keyboard (Don't use handwriting) Thank you.. HCI213 Q: Demonstrate how electronic claims such as EDI works during insurance processing in the healthcare system ? I need new and unique answers, please. (Use your own words, don't copy and paste),
If I have 20 grams of H2O, how much H2 gas can I make if I...
If I have 20 grams of H2O, how much H2 gas can I make if I split water into H2(g) and O2(g)?
explain the relationship between a deep copy and its pointer. explain the relationship between a shallow...
explain the relationship between a deep copy and its pointer. explain the relationship between a shallow copy and its pointer. how many bytes are incremented if a pointer such as *p is incremented using pointer arithmetic such as (depending upon which primitive type it point to; int, double, char etc). write a copy constructor to copy a class object containing a dynamic array. use a member by member copy techniques shown in the textbook. using correct c++ syntax, write the...