More About Methods(Chapter 8 of Programming Ruby)
2010-06-29 00:05
801 查看
Methods that return a boolean result (socalled predicate methods) are often named with a trailing ?. Methods that are “dangerous,” or that modify their receiver, may be named with a trailing exclamation mark, !. These are sometimes called bang methods.
Methods that can appear on the left side of an assignment end with an equals sign (=).
But what if you want to pass in a variable number of arguments or want to capture multiple arguments into a single parameter? Placing an asterisk before the name of the parameter after the “normal” parameters lets you do just that. This is sometimes called splatting an argument.
However, if the last parameter in a method definition is prefixed with an ampersand, any associated block is converted to a Proc object, and that object is assigned to the parameter. This allows you to store the block for use later.
Methods that can appear on the left side of an assignment end with an equals sign (=).
But what if you want to pass in a variable number of arguments or want to capture multiple arguments into a single parameter? Placing an asterisk before the name of the parameter after the “normal” parameters lets you do just that. This is sometimes called splatting an argument.
However, if the last parameter in a method definition is prefixed with an ampersand, any associated block is converted to a Proc object, and that object is assigned to the parameter. This allows you to store the block for use later.
相关文章推荐
- Containers, Blocks, and Iterators(Chapter 4 of Programming Ruby)
- Ruby.new(Chapter 2 of Programming Ruby)
- Sharing Functionality: Inheritance, Modules, and Mixins(Chapter 5 of Programming Ruby)
- Ruby and Its World(Chapter 15 of Programming Ruby)
- Classes, Objects, and Variables(Chapter 3 of Programming Ruby)
- Standard Types(Chapter 6 of Programming Ruby)
- Expressions(Chapter 9 of Programming Ruby)
- Basic Input and Output(Chapter 11 of Programming Ruby)
- Fibers, Threads, and Processes(Chapter 12 of Programming Ruby)
- Namespaces, Source Files, and Distribution(Chapter 16 of Programming Ruby)
- More on Variables and Data Types(Chapter 10 of Programming in Objective-C 2.0)
- More on Classes(Chapter 7 of Programming in Objective-C 2.0)
- Unit Testing(Chapter 13 of Programming Ruby)
- Exceptions, Catch, and Throw(Chapter 10 of Programming Ruby)
- When Trouble Strikes(Chapter 14 of Programming Ruby)
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.1 OVERVIEW OF THE SYSTEM-LEVEL ARCHITECTURE
- Questions that are independent of programming language. These questions are typically more abstract than other categories.
- Python Design Patterns II(Chapter 9 of Python 3 Object Oriented Programming)
- Memory Management(Chapter 3 of iOS Programming: The Big Nerd Ranch Guide)
- How I solved a problem about usage of pscp by requesting more information