CSC/ECE 517 Fall 2007/wiki1b 1 c4: Difference between revisions
Jump to navigation
Jump to search
Line 17: | Line 17: | ||
A section of code that does or performs a task and then returns a value. A synonym for a method is a function. Methods can also be included within classes and used as accessors, mutators and setters. Methods are created with the keyword ''def'' and the name of the method and end with the keyword ''end''. A method is called by simply typing the name of the method and enclosing any parameters to the method in "()"/. i.e. some_method(some_parameter). | A section of code that does or performs a task and then returns a value. A synonym for a method is a function. Methods can also be included within classes and used as accessors, mutators and setters. Methods are created with the keyword ''def'' and the name of the method and end with the keyword ''end''. A method is called by simply typing the name of the method and enclosing any parameters to the method in "()"/. i.e. some_method(some_parameter). | ||
Example | Example: | ||
def basic_method | def basic_method | ||
puts "Hello World" | puts "Hello World" | ||
Line 24: | Line 24: | ||
| | | | ||
Call of the method | Call of the method: | ||
basic_method() | basic_method() |
Revision as of 14:33, 1 October 2007
Introduction
Problem
If multiple methods with the same name are defined, there needs to be some way of determining which method a call refers to. The general rule is given on p. 123 of Programming Ruby. But questions still remain.
Questions
- Is it possible to get unexpected behavior if one of the modules you are using is "enhanced" to contain a new method that happens to conflict with a name of an existing method?
- Is it possible to refer to these methods using a qualified name?
- Is it possible to use method aliasing to resolve the ambiguity?
- What approach does good o-o design dictate?
Method
What is it?
A section of code that does or performs a task and then returns a value. A synonym for a method is a function. Methods can also be included within classes and used as accessors, mutators and setters. Methods are created with the keyword def and the name of the method and end with the keyword end. A method is called by simply typing the name of the method and enclosing any parameters to the method in "()"/. i.e. some_method(some_parameter).
Example:
def basic_method
puts "Hello World"
end |
Call of the method:
basic_method()
Output
Hello World
General Rule For Multiple Methods
References
- Programming Ruby: The Pragmatic Programmers' Guide
- http://en.wikibooks.org/wiki/Programming:Ruby_Method_Calls