Ruby Classes and methods

Source: Internet
Author: User
Tags at sign

Ruby provides four types of variables

Local variable: Lowercase letter or _ Start

Instance variable: an instance variable can span a method of any particular instance or object, meaning that an instance variable from an object to an object is changed. The instance variable is preceded by the at sign @, followed by the variable name

Class variables: Class variables are available in a variety of different objects a class variable belongs to a class is a feature of the class they precede the symbol @@ followed by the class variable name

Global variables: Class variables cannot span classes if you want a single variable that can cross a class need to define a global variable before the global variable is always preceded by a dollar sign $

=end

=begin

Using the new method in Ruby to create an object object is an instance of a class

=end

Class Customer

@ @no_of_customers = 0 #使用类变量 @@ To determine the number of objects created

@no_of_wheels

def initialize #重写初始化方法

@no_of_wheels = "324243234"

End

DEF initialize (wheels) #自定义初始化方法 Incoming parameters Wheels

@no_of_wheels = Wheels

End

def hello

Puts "hello#{@ @no_of_customers}#{@no_of_wheels}"

End

End

#调用对象方法

Object = Customer.new ("Werrwerwrew")

Object.hello

Ruby Classes and methods

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.