Analysis of Rudy Class _ruby topics

Source: Internet
Author: User
The real world is full of objects, and we can classify them. For example, when a very young child sees a dog, no matter what kind it is, it calls them "bow-wow"; we are born with the ability to understand the world through species.

In OO programming terminology, a class of objects such as "Dogs" are called classes, and some special objects belonging to this class are called entities of the class (instance of that class).

In general, creating an object in Ruby or other OO language first defines the attributes of the class, and then creates an entity. To illustrate this, let's first define a simple dog class.

Ruby> class Dog
| def speak
| Print "Bow wow\n"
| End
| End
Nil


In Ruby, a class is defined as a section of code between the keyword class and end. Def in the domain begins a method of defining a class (which we have discussed in the previous section) and corresponds to some particular object method in this class.

Now that we have the dog class, we can use it to create a dog:

ruby> Pochi = dog.new
#<dog:0xbcb90>


We have completed a new entity of the dog class and named it Pochi. The new method of any class will create an entity. Because Pochi is a dog that corresponds to our defined class, it has all the attributes of the dog we define. Because our idea of dog is simple, We can only let Pochi play a trick.

Ruby> Pochi.speak
Bow Wow
Nil


Creating a new class entity is sometimes referred to as the entity of a class (instantiating). We need to tease a real dog, rather than want to let the dog class bark at us.

Ruby> Dog.speak
ERR: (eval): 1:undefined method ' speak ' for Dog:class


Eating "The concept of sandwiches" is clearly meaningless.

At the same time, if we just want to hear a dog cry without any emotion, we can create a short-lived, temporary dog and bark a little before it disappears.

Ruby> (dog.new). Speak # or more commonly, Dog.new.speak
Bow Wow
Nil


"Wait," you said, "Where did this poor guy disappear?" Is this: if we don't mind giving it a name (as we did to Pochi earlier), Ruby's automatic garbage collector would think it was a stray dog and then dispose of it without mercy. It's really doable, you know, because we can create any dog at will.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.