Ruby Getting Started 1

1, Constant & variable

# 1, the constant 
# is generally begin with a capital letter 
# we generally used to define a constant all uppercase 
# 

# define a constant 
VAR1 = 100 
VAR2 = 20 is 

# print this string constants in 
the puts "# {} VAR1" 

# conversion constants string 
the puts "VAR1.to_s # {}" 


# 2, the variable 
# global variable is a variable top, beginning with the $ symbol, can be used in a class, the function may be used in the method may be used in the module 
# 

$ info = "I this is a global variable" 

# Ruby language that comes with global variables 


# $! last error message 
# $ @ errors generated position 
# $ _ gets the most recently read a string 
# $ interpreter recently read line number 
# $ & recent and regular expression matching string 
# $ ~ as a subexpression matches the recent set 
# $ n the n-th nearest match for the expression 
# $ case sensitivity flag = 
# $ / input record separator 
# $ \ output record separator 
# $ 0 script file name 
# $ * command line parameters 
# $$ interpreter process id
# $? Last child process exits state 

#
# Local variables can only use scope 

must be lowercase letters or # begins with an underscore 

_info = "me this is a local variable" 

the puts _info 

# class definitions in a local variable 
class TestClass 
  _info = "class local variable" 
  the puts the _info 
  the puts $ info 
End 

# define a local variable in a module 
Module1 TestModule 
  the _info = "module local variable" 
  the puts the _info 
End 

# defined in the method local variables Te 
DEF testFunc 
  the _info = "this is the method of local variable" 
  the puts the _info 

End 


the puts $$

  

2, numeric

Floating-point integer numeric # 
# 
# Integer There are two types, the Fixnum a Bignum 
# 
# 30 when the power of an integer greater than 2, the type of a Bignum 
# integer less than 30 when the power of 2, the type of the Fixnum 

I = 2 

# .class method, see the type of the variable 
# 
the puts i.class 
# Integer 
# 
f = 12.33 
the puts f.class 
# Float

  

3, range

# Range: it defines the two boundary 
# 

R1 # = 3..5 comprising. 5 
R2 =. 5. 3 ... # does not include. 5 


the puts R1. 5 === 
the puts R2. 5 === 
the puts R1. 3 === 
the puts === 3 r2 


# to true 
# false 
# to true 
# to true 

# === determine whether the right of the target object in the left 
# 
# range is an object, the object we look at what method 
# 


a1 = r1.to_a 
# to_a the method of converting an array of a range of 
# 
the puts a1.class 
# the array 
# 

the puts r1.max 
seeking a maximum range of # 
the puts r1.min 
# calculating a minimum range of 
the puts r1.first 
# calculates a first range value 
# 
the puts r1.last 
# last calculated value of a range of 

the puts r1.end 
# calculating a range of the last value  
# 
# 
#
puts a1.size 
# calculating the length of the array of 
the puts a1.length 
# calculating the length of the array 
# 

# range there are three commonly used iterator method 
# # regect this range is converted into an array, and remove the condition of the element 
## SELECT this range is converted into an array of elements and keep the condition of 
iterative ranges # # of each output of each element 
#

 

Three iterator usage

# Range there are three commonly used iterator method 
# # regect this range is converted into an array, and delete elements to meet the conditions 
# # SELECT this range is converted into an array of elements and keep the condition of 
# # output each iteration each element in the range of 
# 

COUNT = 1..10 

Print count.reject {| I | I% 2 == 0} 

Print "\ n-" 
Print count.select {| I | I% 2 == 0} 

Print "\ the n-" 

count.each do | i | 
  the puts i 
End

  

  

 

 

4, symbol

# Sign 
# symbol object is a string, a class, a method or an internal representation of an identifier, create a symbolic object is a colon in front of the objects on it 
# 

DCY = "I am your fan" 

LY =: dcy 

the puts ly ==: "dcy" 

# ly determine whether the variable dcy corresponding symbol object 
# 
DEF fangfa 
  the puts "I remember you" 
End 

fy =: fangfa 

the puts == fy: "fangfa" 

# as long as the same object name, symbol object generated by the same, because the objects between the strings and symbols are one to one, so the strings and symbolic objects on the forehead can be converted by to_s and to_sym each other 

dcy = "I am your sealed" 
dy =: "I am your sealed" 

the puts dcy.to_sym == dy 

the puts dy.to_s == DCY 


DCY = "I am your sealed" 
LY =: DCY 

# symbol object's methods to build a 2 
dcyandy% S = {DCY } 

the puts LY == dcyandy

  

5, operators, assignment operators, and

# Arithmetic operators and assignment operators 

# arithmetic operators 

# * 
# * 
# + 
# - 
# / 
#% 
# 
# assignment operator 
# = 
# + = 
# = - 
# * = 
# / = 
# =% 
# ** = 
#

  

Guess you like

Origin www.cnblogs.com/bainianminguo/p/11094570.html