Recipe 8.1. Managing Instance Data
Problem
You want to associate a variable with an object. You may also want the variable to be readable or writable from outside the object.
Solution
Within the code for the object's class, define a ...
Recipe 8.2. Managing Class Data
Problem
Instead of storing a bit of data along with every instance of a class, you want to store a bit of data along with the class itself.
Solution
Instance
variables are prefixed by a single ...
Recipe 8.3. Checking Class or Module Membership
Problem
You want to see if an object is of the right type for your purposes.
Solution
If you plan to call a specific method on the object, just check to see whether the object reponds t...
Recipe 8.4. Writing an Inherited Class
Problem
You want to create a new class that extends or modifies the behavior of an existing class.
Solution
If you're writing a new method that conceptually belongs in the original class, you ca...
Recipe 8.5. Overloading Methods
Problem
You want to create two different versions of a method with the same name: two methods that differ in the arguments they take.
Solution
A Ruby class can have only one method with a given name. W...
Recipe 8.6. Validating and Modifying Attribute Values
Problem
You want to let outside code set your objects'
instance variables, but you also want to impose some control over the values your variables are set to. You might wan...
Recipe 8.7. Defining a Virtual Attribute
Problem
You want to create
accessor methods for an attribute that isn't directly backed by any instance variable: it's a calculated value derived from one or more different instance variable...
Recipe 8.8. Delegating Method Calls to Another Object
Problem
You'd like to delegate some of an object's method calls to a different object, or make one object capable of "
impersonating" another.
Solution
If you want to completely...
Recipe 8.9. Converting and Coercing Objects to Different Types
Problem
You have an object of one type and you want to use it as though it were of another type.
Solution
You might not have to do anything at all. Ruby doesn't enfo...
Recipe 8.10. Getting a Human-Readable Printout of Any Object
Problem
You want to look at a natural-looking rendition of a given object.
Solution
Use
Object#inspect. Nearly all the time, this method will give you something more readable...
Recipe 8.11. Accepting or Passing a Variable Number of Arguments
Problem
You want to write a method that can accept any number of arguments. Or maybe you want to pass the contents of an array as arguments into such a method, rather than
...
Recipe 8.12. Simulating Keyword Arguments
Problem
A function or method can accept many optional arguments. You want to let callers pass in only the arguments they have values for, but Ruby doesn't support keyword arguments as Python and Li...
Recipe 8.13. Calling a Superclass's Method
Problem
When overriding a class's method in a subclass, you want to extend or decorate the behavior of the
superclass, rather than totally replacing it.
Solution
Use the super keywor...
Recipe 8.14. Creating an Abstract Method
Problem
You want to define a method of a class, but leave it for subclasses to fill in the actual implementations.
Solution
Define the method normally, but have it do nothing except raise a
...
Recipe 8.15. Freezing an Object to Prevent Changes
Problem
You want to prevent any further changes to the state of an object.
Solution
Freeze the object with Object#freeze:
frozen_string = 'Brrrr!'
frozen_string.freeze
frozen...
Recipe 8.16. Making a Copy of an Object
Problem
You want to make a copy of an existing object: a new object that can be modified separately from the original.
Solution
Ruby provides two ways of doing this. If you only want to have to reme...
Recipe 8.17. Declaring Constants
Problem
You want to prevent a variable from being assigned a different value after its initial definition.
Solution
Declare the variable as a constant. You can't absolutely prohibit the variable from ...
Recipe 8.18. Implementing Class and Singleton Methods
Problem
You want to associate a new method with a class (as opposed to the instances of that class), or with a particular object (as opposed to other instances of the same class).
...
AJAX popular information C# language guides Windows books and cookbooks
.......
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.