A little more complexCodeIt is easy to get dizzy by the Representation Methods of various Ruby conventions.
(If the first part of the identifier is a lowercase letter or "_", the identifier is a local variable or method call .)
(An identifier starting with an uppercase letter ([A-Z]) is a constant, class name, or module name)
A variable starting with @ is an instance variable, which belongs to a specific object. Instance variables can be referenced in class or subclass methods.
If you reference an instance variable that has not been initialized, its value is nil.RubyThe instance variable does not need to be declared. Every instance variable is dynamically added to the object when it appears for the first time.RubyThe instance variable is usually defined in the method-when the instance variable is declared in the method, the instance variable actually belongs to the instance of the class where the method is located, rather than the method. For example
Class Apple # define the first method def info1 # output instance variable @ A puts @ A end # define the second method def info2 # assign a value to the instance variable @ A = "hello "; end end # create an apple instance Apple = apple. new # Call method apple. info2 apple. info1
Variables starting with @ are class variables. Define class variables in the class definition. You can reference or assign values to class variables in special methods and instance methods of the class:
Class Foo @ Foo = 1 def bar puts @ Foo endend
The differences between class variables and constants are as follows.
- Values can be assigned repeatedly (a constant generates a warning)
- It cannot be referenced outside the class (it can be referenced or assigned a value in the inherited class)
The differences between class variables and class instance variables are as follows.
- You can reference/assign values to child classes.
- You can reference/assign values in the instance method.
Class variables can be viewed as global variables shared by classes, subclasses, and their instances.
Class Foo @ Foo = 1 endclass bar <Foo P @ Foo + = 1 # => 2 endclass Baz <bar p @ Foo + = 1 # => 3end
The class variables defined in the module are shared by all classes that contain the module.
Module Foo @ Foo = 1 endclass Bar include Foo P @ Foo + = 1 # => 2 endclass Baz include Foo P @ Foo + = 1 # => 3end
The variable starting with $ is a global variable.Program(So pay special attention to it ). No variable declaration is required for global variables. When Referencing uninitialized global variables, the value is nil.
% Notation (percent Notation ):
% {String} is used to create a string enclosed by double quotation marks.
% Q {string} is used to create a string enclosed by double quotation marks.
% Q {string} is used to create a string enclosed by single quotes.
% R {string} is used to create a regular expression literal value.
% W {string} is used to split a string into a string array with blank characters for less replacement.
% W {string} is used to split a string into a string array with white space characters for replacement.
% S {string} is used to generate a symbolic object.
% X {string} is used to execute the command represented by string.
"% 05d" % 123» "00123"
"%-5 S: % 08x" % ["ID", self. ID]» "ID: 200e1670"
# Used to call a method,
* No:
If the last expression on the left contains *, the redundant elements on the right are substituted into the expression with * as an array. If there are no redundant elements on the right, the empty array is substituted into it.
* Foo = 1, 2, 3 # Foo = [1, 2, 3] Foo, * bar = 1, 2, 3 # Foo = 1; bar = [2, 3]
* Variables used in method definition to indicate variable lengths:
[From "programmingruby "]
Variable-length argument lists
But what if you want to pass in a variable number of arguments, or want to capture multiple arguments into a single parameter? Placing an asterisk before the name of the parameter after the ''normal ''parameters does just that.
Def varargs (arg1, * rest) "got # {arg1} and # {rest. join (',')} "endvarargs (" one ")»" got one and "varargs (" one "," two ")» "Got One and Two" varargs "one", "two", "three"» "got one and two, three"
IRB (main): 005: 0> def call_back (* A) IRB (main): 006: 1>. each do | arg | IRB (main): 007: 2 * puts argirb (main): 008: 2> endirb (main): 009: 1> end => nilirb (main): 011: 0> call_back (["hello", 99]) hello99 => [["hello", 99] IRB (main): 012: 0> call_back (["hello", 99], "hello", 99) hello99hello99 => [["hello", 99], "hello", 99]
Other usage:
C: \ Users \ Administrator> IRB
IRB (main): 001: 0> [1, 2, 3] * "hi"
=> "1hi2hi3"
IRB (main): 002: 0> [1, 2, 3] * ";"
=> "1; 2; 3"
IRB (main): 003: 0> [1, 2, 3] * 3
=> [1, 2, 3, 1, 2, 3, 1, 2, 3]
IRB (main): 004: 0> "hi" * 3
=> "Hihihi"