Ruby Basic Types

Source: Internet
Author: User

#!/usr/bin/ruby

=begin

There are 5 types of variables supported by Ruby

Global variables: The value of the uninitialized global variable begins with $0 and generates a warning using the-w option the assignment of global variables changes global state deprecated global variables they make the meaning of the program fuzzy

=end

#以下为全局变量例子

=begin

$global _variable = 10

Class Class1

def Print_global

Puts "Global variable in Class1 is # $global _variable"

End

End

Class Class2

def Print_global

Puts "Global variable in Class2 is # $global _variable"

End

End

Class1obj = Class1.new

Class1obj.print_global

Class2obj = Class2.new

Class2obj.print_global

=end

The value of an uninitialized instance variable at the start of the #以下为实例变量例子 instance variable is 0 and produces the-W option

=begin

Class Customer

@cust_id

@cust_name

@cust_addr

def initialize (ID,NAME,ADDR)

@cust_id = ID

@cust_name = Name

@cust_addr = addr

End

def display_details ()

Puts "Customer ID # @cust_id"

Puts "Customer name # @cust_name"

Puts "Customer address # @cust_addr"

Puts "Customer address # @cust_addrs"

End

End

#Create Objects

Cust1 = Customer.new ("1", "John", "Wisdom Apartments,ludhiya")

Cust2 = Customer.new ("2", "Poul", "New Empire Road Khandala")

Cust1.display_details ()

Cust2.display_details ()

=end

=begin

#类变量 class variables start with @@ They can be used to initialize class variables that refer to uninitialized classes before the method definition produces an error class variable that shares the class variable in the class or module whose descendant is the class variable that always shares the same memory address for the same variable

Class Customer

@ @no_of_customers = 0

def initialize (ID,NAME,ADDR)

@cust_id = ID

@cust_name = Name

@cust_addr = addr

End

def display_details ()

Puts "Customer ID # @cust_id"

Puts "Customer name # @cust_name"

Puts "Customer address # @cust_addr"

End

def total_no_of_customers ()

@ @no_of_customers + = 1

Puts "total number of customers: #@ @no_of_customers"

End

End

#Create Objects

Cust1 = Customer.new ("1", "John", "Wiadom apartiments Ludhiya")

Cust2 = Customer.new ("2", "Poul", "New Empire Road Khandala")

Cust1.total_no_of_customers ()

Cust1.total_no_of_customers ()

=end

=begin

#常量 constants that begin with a capital letter in a class or module defined by a constant can be accessed by the class or module outside of the defined class or module can be globally accessible constants that cannot be defined within a method that are referenced as initialized constants will produce an error assignment initialized a constant will produce a warning

Class Example

VAR1 = 100

VAR2 = 200

Def show

Puts "Value of first Constant is #{var1}"

Puts "Value of second contant is #{var2}"

End

End

#Create Objects

Object = Example.new

Object.show

=end

#拟变量 They are special variable local variables but look like constants but cannot assign any values to these variables

=begin

Self: Receiver object for the current method

True: Represents a True Value

False: False value is indicated

Nil: denotes undefined value

__file_: The name of the current source file

__line_: The current line number in the source file

#基本常值

Ruby's rules for using literal literals are simple and intuitive as follows

Integer number: An integer can range from 2 to 30 to 2 29 or from 2 to 62 for 2 of the order of the time. An integer in this range is an integer of the Fixnum class that is stored in an object of the class of bignum in an optional radix notation (08 binary, 0x for hexadecimal or binary 0b) using an optional leading symbol, followed by a string of numbers in the corresponding cardinality. An underscore character is ignored by a string of digits

123 #普通整形

1_234 #带下划线

-500 #负数

0377 #八进制

0xFF #十六进制

0b1011 #二进制

? A # ' A ' ASC code

1234567894345342342333 #bignum类型整数

#浮点数 floating-point number is a float object

#字符常值 string is a simple 8-bit byte sequence they are string class object double quote string can be substituted and backslash symbol but not allow single quote substitution and only allow backslash symbol and ' can also use #{expr} sequence to represent any Ruby expression value Expression expr can be any ruby expression

An array of #数组 Ruby is a comma-delimited series of literal comma-separated ends of objects referenced by the drop object can be any type

#哈希 literally Ruby creates a hash to place a list of key-value pairs between parentheses, with a comma or sequence of key values between the comma end is ignored

#范围 Range represents the start and end of the interval for a set of values that may be used by S: E and s...e text or with Range.new range range: Including running from now to end closed interval use ... Excluding the final worth as an iterator range for a half interval each value in the sequence will return a range (1..5) representing the 1,2,3,4,5 value

=end

arr = ["fewwe", 10,3.2,-32, "This was a string", "last element",]

Arr.each do |i|

Puts I

End

HSH = colors = {"Red" + 0xf00, "green" =>0x0f0, "Blue" =>0x00f}

Hsh.each do |key,value|

#print key, "is", Value, "

#"

Puts ("#{key} is #{value}")

End

(1..10). Each do |n|

Puts ("#{n}")

End

Ruby Basic Types

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.