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
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...
Hi. I want to make this cocktail sort method, but with the call sort(Comparable[] array). What...
Hi. I want to make this cocktail sort method, but with the call sort(Comparable[] array). What changes do I have to make, and how will the end result be? public class CocktailSort { public static void sort(Integer[] array) { int start = -1; int end = array.length - 2; boolean swapped; do { swapped = false; for (int i = ++start; i <= end; i++) { if (array[i] > array[i + 1]) { swap(array, i, i + 1); swapped =...
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
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
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...
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   }   
how can i make the graph in exel
how can i make the graph in exel
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.
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT