RUBY : Basic Concepts Of Ruby

本文介绍了Ruby语言的基础概念,强调其纯面向对象的特点,并通过创建食谱应用的例子展示了如何使用类和对象来模拟实际问题。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值