Step-by-step learning Ruby (12): Ruby Class

First, the definition of categories, examples of methods of class

Step-by-step learning Ruby (12): Ruby Class

Second, specific object

In Ruby, we can separate the definition of an object method

Step-by-step learning Ruby (12): Ruby Class

Third, re-defined methods

As can be seen from the following code, the definition of the back cover in front of the definition of

Step-by-step learning Ruby (12): Ruby Class

Fourth, re-open category

Ruby can we open a class to add such a method, a bit like C # in the division of categories, but do not add the special tag.

We generally do not open the class definition, as well as understanding, open-type one of the reasons is divided up among them a number of documents.

Step-by-step learning Ruby (12): Ruby Class

V. instance variables

The front said, add here

Instance variable is used to record the status of a single object. Ruby's instance variables have the following characteristics

  • To @ at the beginning of
  • Only a single object can be seen
  • Regardless of any place in the class definition, in other places are the same

Step-by-step learning Ruby (12): Ruby Class

Sixth, to initialize the object state

Similar to other languages of the constructor

Step-by-step learning Ruby (12): Ruby Class

Seven or better to read and write the attributes, power = No.

Above we can see a variety of methods can be used to write all of the attributes, but the most familiar and convenient, or to other languages, the direct assignment of attributes, and then read

Step-by-step learning Ruby (12): Ruby Class

Syntax sugar

Syntax sugar refers to special rules, not in line with the conventional formulation, Ruby provides a write method call syntax sugar, to see if age = interpreter will ignore the equal sign in front of the box, and the method of single parameter can be omitted brackets, so it can be

Step-by-step learning Ruby (12): Ruby Class

= Because of the power, we can assign their own when dealing with, a bit like. Net in the (get; set;)

8, automatically generated operations to read and write attributes attr_ *

If each of the attributes are as simple as the definition above is a very complicated thing, Ruby provides a better read and write operations automatically generated methods, facie Table

Method of Effect Examples Equivalent code
attr_reader Reading Method attr_reader: age def age
@ age
end
attr_writer Write method attr_writer: price def age = (age)
@ age = age
end
attr_accessor Have to read and write methods att_accessor: age def age
@ age
end
def age = (age)
@ age = age
end
attr Methods and have time to write the optional method (If the second parameter is true) 1. Attr: age
2. Attr: age, true
1. Attr_reader see
2. Attr_accessor see

Step-by-step learning Ruby (12): Ruby Class

9, class methods

Category is a special object, is the only way to create a new object of a class of objects

Defined in the methodology together with the class name

Step-by-step learning Ruby (12): Ruby Class

There are two ways to call the type of approach:

  • Person.oldest ()
  • Person:: oldest ()

10, inheritance

Ruby Lane with <the realization of the succession

Step-by-step learning Ruby (12): Ruby Class

The author of this article: Wang water

分类:Ruby 时间:2009-09-23 人气:666
分享到:
blog comments powered by Disqus

相关文章

  • Ruby instance variables and class instance variables 2011-04-28

    Code first class Kitty @class_ins_1 = 1 attr_accessor :ins_1, :ins_2 def initialize @ins_1 = 11 end class << Kitty attr_accessor :class_ins_1, :class_ins_2 end end puts Kitty.class_ins_1 Kitty.class_ins_2 = 2 puts Kitty.class_ins_2 k = Kitty.new put

  • ActionView's Magic: ERB & Binding why the page can use instance variables 2010-06-30

    Rails as a MVC framework, its core consists of three blocks: ActiveRecord, ActionController, and ActionView. Today, Bowen protagonist of this is ActionView, unlock template system magic. Typically, through scaffold has been able to create simple, con

  • java foundation's "instance methods, instance variables, class methods, class variables" 2008-10-25

    Analysis of these first 11 Dongdong: instance method, instance variables, class methods, class variables. Instance variable (instance variable): or call instance fields, instance fields (instance field), or known as member variables (member variable)

  • local variables, global variables, instance variables 2010-03-23

    Local variables http://en.wikipedia.org/wiki/Local_variable global variables http://en.wikipedia.org/wiki/Global_variable instance variables http://en.wikipedia.org/wiki/Instance_variable

  • How to access the class variables ruby 2011-05-06

    In the class declaration, no definition of getter and setter, not the same as this and java Now know that there are two ways: 1. With instance variables class Test def initialize() @name ="yanzilee9292" end end puts Test.new.instance_eval { @nam

  • ruby syntax tutorials 2010-08-02

    The first part of the Ruby language based Chapter § 1.1 Ruby Ruby language overview of the history Ruby language is Japanese inventor Yukihiro Matsumoto (Matsumoto Yukihiro), we affectionately called him "Matz". May be contrary to everyone's sur

  • Static variables and instance variables initialization sequence problem 2010-09-26

    To reproduce the problem Let us first look at the following procedure: public class StaticInitSequence { //-------------------Static fields------------------- private static int staticIntVar = 10; private static int staticComputeIntVar = (int)(Math.r

  • Common abnormalities resolved 2011-09-18

    ConcurrentHashMap and CopyOnWriteArrayList comparison. Blog categories: Java ConcurrentHashMap ConcurrentHashMap introduced Segment, each Segment is a hashtable, the equivalent of two Hash table, and then lock in the Segment level, and increased conc

  • Ruby Symbol 2009-03-02

    Ruby is a completely cross-platform object-oriented scripting language, built on the Ruby-based Web framework Ruby on Rails (ROR) is the field of web development is gaining increasing acceptance. ROR substantial use of the Ruby language features, Sym

  • Ruby study notes based (pure object-oriented approach closure module variable scope) 2011-05-01

    -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --- # About objects, instance variables, and methods. Object's instance variables are stored in t

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.035 (s). 13 q(s)