1. Constants
Although Ruby only requires uppercase letters of a constant, we recommend that you use uppercase letters of a constant name for better readability.
Class persons
Finger = 10; # define Constants
Puts "Everyone has" + finger. to_s + "root finger" # output
Finger = 11; # assign a value to a constant again
Puts "some people can also have" + finger. to_s + "root finger" # re-Output
End
Class Test
Const = "Hello world! "; # Define Constants
Puts const + "access in parent class" # internal class access constant
Def info # Creation Method
Puts const + "access in method" # access constants in class methods
End
Class testchild # create an internal class
Puts const + "access in internal classes" # access constants in internal classes
End
End
Class sub <test # create a subclass
Puts const + "access in subclass" # access to the parent class constant in subclass
End
Puts test: const + "Usage: Access to constants out of the range" # usage: To access constants in the test class
T = test. New # create a test object t
T.info # access the info method in test through T
Class first
Info = "Ruby"; # define Constants
End
Class second
Info = "hello" # define Constants
Class firstchild <first
Puts info # constant used to access external classes
Puts first: info # accessing constants in the parent class
End
End
2. Variables
In Ruby, local variables must be lowercase letters (~ Z) or begin with an underscore
The global variable starts with "$.
3. P and puts and print
P "\ ta"
Puts "\ ta"
Output:
"\ Ta"
A
In common: it is used for screen output.
Differences:
After puts outputs the content, it will automatically wrap the text (if the content parameter is blank, only one line feed is output). If the content parameter contains an escape character, the output will first process the escape and then output
P is basically the same as puts, but does not process escape characters in parameters.
Print is basically the same as puts, but the line break is not automatically added to the end after the output content
4. Ajax needs to be imported in the head.
<% = Javascript_include_tag "prototype" %>