angry troll with your bare hands, turn to page 137. • If you try to reason with the troll, turn to page 29. • If you don your invisibility cloak, turn to page 6. • You exit the passageway into a large cavern. Unless you came from page 59, in which case you fall down the sinkhole into a large cavern. • A huge troll, or possibly a badger (if you already visited Queen Pelican), blocks your path. • Unless you threw a button down the wishing well on page 8, in which case there nothing blocking your way. http://en.wikipedia.org/wiki/Choose_Your_Own_Adventure
various formats to buyers. • There is a lot of preexisting purchase data which needs to be imported into the new system. The purchase data has the following form: name,email,product_id,date Crow T. Robot,[email protected],123,2012-06-18
use the record's #email_address to #get_customer. 3. Use the record's #product_id to #get_product. 4. #add_purchased_product to the customer record. 5. #notify_of_files_available for the purchased product. 6. #log_successful_import of the purchase record.
already existing classes or methods defined in the system. • We didn't discuss the app's strategy for persisting data such as customer records; whether it uses an ORM. • We didn't even talk about what class we are defining this method on, and what methods or attributes it might already have.
ensure that inputs are of a specific core type. For instance, you are writing a method with logic that assumes Integer inputs. • Synopsis: Use Ruby's defined conversion protocols, like #to_str, #to_i, #to_path, or #to_ary.
to ensure inputs are of a core type with context-specific extra semantics. For instance, you expect to work with two-element arrays of integers representing X/Y coordinates. • Synopsis: Define new implicit conversion protocols mimicking Ruby's native protocols such as #to_path. • Rationale: By exposing a conventionalized way for third-party objects to convert themselves to the type and "shape" our method needs, we make our method more open to extension.
expects to work with a Meter type, but you would like to leave the possibility open for Feet or other units to be supplied instead. • Synopsis: Define your own conversion protocol for converting arbitrary objects to instances of the target class. • Rationale: A well-documented protocol for making arbitrary objects convertible to our own types makes it possible to accept third-party objects as if they were "native".
want to convert an input object into a core type, no matter what the original type is. For instance, you need to ensure that any input is coerced into an Integer if there is any reasonable way to do so; whether the incoming data is a Float, a nil, or even a hexidecimal string. • Synopsis: Use Ruby's capitalized conversion functions, such as Integer and Array. • Rationale: When a method's internals deal with core types, using a conversion function to pre- process input values allows maximum flexibility for inputs while preventing nonsensical conversions.
#to_* in ruby. • The Kernel module also provides a set of unusually-named conversion functions: ˒ Array(), Float(), String(), Integer(), Rational(), and Complex().! • These share the same name with ruby core classes, but technically they are ordinary methods. • Other examples, uri module provides URI(), pathname library provides Pathname()! • They are idempotent. Calling the method with an argument which is not of the target class will cause it to attempt a conversion. Calling it with an argument of the target type will simply return the unmodified argument. • They can convert a wider array of input types than the equivalent #to_* methods.
convert it to an Integer or Bignum. A Float will be truncated. 2.It will convert strings containing integers in decimal, hexidecimal, octal, or binary formats to an integer value. 3.It will attempt to convert other objects using #to_int if it exists. 4.It will fall back to #to_i if none of the above rules apply.