Gieno RUBY : Basic Concepts Of Ruby | ||
Ruby is an object-oriented language, but before you skip this post because you think you know what this is all about because you have used C++ or some other unfortunate excuse for an object-oriented language, then please pause and at least read the following sentence. In Ruby, everything you manipulate will be an object. Everything. Even the results of operations on said objects are objects; this approach differs from C++ or C# where primitive types exist or some statements do not return a value. If you have never delved into object-oriented programming, then that is a different story altogether. When writing Ruby code, or object-oriented code in general, the idea is to create models in your code that render the process you are trying to go through in code. For example, if you were creating a cookbook application (Gieno always love food O(∩_∩)O), you would probably want to create a list of recipes (my skills of deduction are amazing,^o^). To model that in a not-so-object-oriented way, you would most likely use a series of list structures of some sort to hold the various sorts of data with a synchronized way to track the position of each list or some such nonsense. Object-oriented programming simplifies this and allows you to create classes and objects to model the needed components. Using example before, you could create a Recipe class with string attributes name and author and a hash or arrary attribute of ingredients. A class's purpose is to model some thing in your application; calsses create the "prototype" for the nouns in your programs: objects. Class instances (or objects), then take that prototype and put it into action. In this example, objects could be created for each recipe in the list that would be instances of the class Recipe, which would in turn could hold data and do things related to being a recipe (i.e., hold a list of ingredients, add ingredients to that list, and so on) and enforce constraints that would be enforced on a normal recipe. | ||
转载于:https://www.cnblogs.com/gieno/archive/2009/08/03/1537561.html