M
Mathias Weyel
Hello!
I am quite new to Ruby and come directly from the world of Java. While
the lack of a static type system has proven to be somehow irritating to
me, I have stepped over a rather small problem concerning overloading of
methods and parameter names. In Java, this problem cannot occur due to
the static type system.
Here's what I want to do:
I have a class that serves as a container for objects. I want to have a
method "remove" that removes Objects from the list. This method should
exist twice. One which accepts an object and deletes the object that is
== to the given object and another one that accepts ints and deletes the
object at the given index. Here the solution in Java:
class Whatever {
List objects;
public void remove(Object o) {...}
public void remove(int i) {...}
}
The problem in converting this code to Ruby is that the remove method
has no type and, because of this, cannot be overloaded this way. While
having only one method and checking the type of the given argument
manually seems somehow wrong to me, I wonder, how to call the parameter.
The int parameter I would normally call "index" while the object
parameter would correspond to the objects stored in the list. What name
should the parameter get? I want to have a name that actually makes
sense, but "objectOrIndex" seems stupid.
Greetings
Mathias Weyel
I am quite new to Ruby and come directly from the world of Java. While
the lack of a static type system has proven to be somehow irritating to
me, I have stepped over a rather small problem concerning overloading of
methods and parameter names. In Java, this problem cannot occur due to
the static type system.
Here's what I want to do:
I have a class that serves as a container for objects. I want to have a
method "remove" that removes Objects from the list. This method should
exist twice. One which accepts an object and deletes the object that is
== to the given object and another one that accepts ints and deletes the
object at the given index. Here the solution in Java:
class Whatever {
List objects;
public void remove(Object o) {...}
public void remove(int i) {...}
}
The problem in converting this code to Ruby is that the remove method
has no type and, because of this, cannot be overloaded this way. While
having only one method and checking the type of the given argument
manually seems somehow wrong to me, I wonder, how to call the parameter.
The int parameter I would normally call "index" while the object
parameter would correspond to the objects stored in the list. What name
should the parameter get? I want to have a name that actually makes
sense, but "objectOrIndex" seems stupid.
Greetings
Mathias Weyel