Ruby in man's tears (2)

Source: Internet
Author: User

I think it's up to me to wait for me to take the DBA test. To be a DBA or to be a programmer it's a questions. In fact, I personally prefer to do this.ProgramDesign, but considering my personality, I feel more appropriate to be a DBA. In the afternoon, I read the Oracle certified Administrator (OCA) book. I don't think it's hard. Most of them are database maintenance. Perhaps the only thing that makes people feel rare is to have to go back to the cute Linux (not necessary), but fortunately I have some Linux basics. Do you have any questions about database.

I have been busy working hard over the past few days and looking for novel adjustments, so I didn't write the ruby series for the first time. I hope you will forgive me. Fortunately, my mentality is better now. I think of the poems in Legend of the legend of the fairy sword when I was a child. Now I feel pretty good.

Don't look back, why not forget.
Since there is no chance, why.
Today, there is no trace of water.
Ming Xi He Xi, Jun is no stranger.

Let's get down to the point. Let's take a look at how to define a class in Ruby:

 
ClassMyclassEndOb = myclass. New

The preceding example creates a class and initializes the instance ob of A myclass class. The syntax is quite simple. In fact, I prefer to add () after the new class, mainly because I am afraid of getting dizzy. In fact, even if the myclass we created is an empty class, it is not really poor empty. He also has attributes and methods inherited from the object. The ruby class hierarchy model is introduced here. All classes are inherited from objects, which is the same as most object-oriented languages. Thank you for choosing the "class. Class" method to tell others what class they are. Take a look at the followingCode:

PutsOB.Class#=> Displays "myclass"

The class method can be used to output the class name of OB, which is useful in certain situations. Let's make our class useful. Don't just talk to his father about his name or something else.

 
ClassMyclassDefSaysomethingPuts("Hello")EndEndOb = myclass. newob. saysomething

In this way, a useful class is instantiated and called. I feel that it is clear about Ruby class calls. Let's make the class more lively. Apart from talking about it, we also need to learn and be able to accept what to write by ourselves. Of course, it is still a function.

  class  bird  def  set_name (aname) @ myname = aname  end   def  get_name  return  @ myname  end   def  talk  return  'gigigi'  end   end  Bluebird = bird. newredbird = bird. newbluebird. set_name ('bluebird') Redbird. set_name ('redbird')  puts  (bluebird. get_name)  puts  (Redbird. get_name)  puts  (bluebird. talk)  puts  (Redbird. talk) 

This is a basic preliminary integrated application of the class, simulating a bird action. However, it is difficult to use the class. set_name () function after initializing a class every time. Can I use class () directly. Here we will introduce the following main character constructors. For object initialization functions, C # and the like are all functions with the same name as the class name, but they are different in Ruby. There is a special function name that corresponds to initialize (). Let's take a look at the code for better understanding:

 

ClassManDefInitialize (aname, Aage) @ name = aname @ age = AageEndDefGet_nameReturn@ NameEndDefGet_ageReturn@ AgeEndDefTo_sReturn'Good !!! 'EndEndSuperman = man. New ('jesse ', 21)Puts(Superman. get_name)Puts(Superman. get_age)Puts(Superman. to_s)

The code below shows how to use the initialize () constructor, and the function overload is also mentioned later. I have overloaded the to_s function to implement what I call tostring. It feels better. In fact, sometimes it feels that the real power of Dynamic Language lies in its dynamics, which is reflected in the reflection mechanism. Next, let's look at Ruby reflection through a corner. Enter the following code:

 
Superman. InspectP(Superman)
 
The following content is displayed:

# <Man: 0x28bdb64 @ age = 21, @ name = "Jesse">
Nil

Inspect is the introspection function of Ruby objects. Superman is an instance of the man type, and the following number is the object identifier used by the ruby virtual machine. Both @ age and @ name are values of this object instance. Here you can think of something, consider it yourself. I wrote it tomorrow. Continue to work hard...

 
 
 
 
 
 
Related Article

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.