ArticleDirectory
- Variables
- Conditions
- Loops
- Error Handling
- Methods
- Classes
- Using code from other files
In the previous blogs, I introduced some ironruby-related content, because ironruby is in ruby. net, so its basic syntax and usage are similar to Ruby. To learn how to use ironruby, we must first quickly understand how to use ruby, this article mainly introduces some basic Ruby knowledge, which is also the main things I have used over the past few days. I hope it will be helpful to beginners.
Variables
Defining variables is simple. You only need to write a lower-case code name followed by the equal sign and value, as shown in figure
STR = "hello"
Num = 1
Arr = [1, 2, 3]
Conditions
- If XXX then XXX elseif XXX then XXX end
- Title = "login" If (Title = "")
- Unless (Password = "") the xxx end
Loops
- While XXX do XXX end
- Until XXX do XXX end
- For item in XXX do XXX end
- 10. Times do | I | print I end
- A. Each do | item | puts item end
Error Handling
Begin
# Something risky
Rescue someexception
Xxx
Rescue exception
Xxx
Rescue
Xxx
End
Methods
Def add (A, B)
A + B
End
- The Return Value of the method can be returned through return XXX, or directly write the value to be returned at the end of the method.
- Result1 = add (1, 2) or result2 = Add 1, 2
Classes
Class testapplication
Def initialize # Constructor
Self. runing = false # assign values to attributes
End
Attr_accessor: runing # Attribute attr_reader attr_writer
@ Instance = nil # class variable
@Instance_variable = 0 # instance variable
Def self. Instance
@ Instance = self. New if @ instance = Nil
@ Instance # function return Singleton
End
# Instance method
Def run
Return if self. runing
Xxx
Self. runing = true
End
# Class methods
Def self. say_hello
Puts "hello"
End
End
- Generate testapplication. New
- UseSuperCall the parent class method. If the parameters are the same, the parameter can be left blank.
Using code from other files
Recommended:Online e-books you may need
Agile individual Sina microjournal: http://kan.weibo.com/kan/3483302195814612
You are welcome to reprint it. Please note: Reprinted fromAgile personal website