Reputation: 167
I am trying to understand a code not written by me. How can I know the type of argument a method expects? For example, let's say I have a method:
def foo(bar)
@bar = bar
@baz = get_user(@bar.user)
end
How can I know which object I should send to bar?
Upvotes: 1
Views: 126
Reputation: 3267
You can't know what kind of argument foo
is waiting to receive, you only know that that object must respond to user
in your example.
This principle is known as Duck Typing: With normal typing, suitability is assumed to be determined by an object's type only. In duck typing, an object's suitability is determined by the presence of certain methods and properties (with appropriate meaning), rather than the actual type of the object.
Upvotes: 4