Ruby入门
-
- ability to make operating system calls directly
- powerful string operations and regular expressions
- immediate feedback during development
-
- variable declarations are unnecessary
- variables are not typed
- memory management is automatic
-
- everything is an object
二、Variables & Constant :
1.Variables
- Local variables:
- Global variables:
- Instance variables:
- Class variables:
2.Constant
- A constant has a name starting with an uppercase character.
- Constants may be defined within Classes or Module, never in method.
- It should be assigned a value at most once.
e.g.:
class Demo
PI=3.1415
#PI=3.15214 #warning: already initialized constant PI
def hello
PI=3.1415 #wrong
end
end
demo = Demo.new
demo.hello
三、Comment
#(Ctrl + /)
=begin
…
…
=end
四、Numeric:
- Methods:
- to_f() #Integer -> Float
- to_i() # Float -> Integer, directly delete the part of decimal, if don’t want this, you can use the round method
- round
-
n.times{|i | …}
-
from.upto(to) {|i| …}
-
from.downto(to){|i| …}
-
from. step(to, step) {|i| …}
a=01123 #0=> Octal
B=-0x23 #0X=> Hex
C=+0b1010 #0=> Binary
五、Range:
-
- val1.. val2 #contains: val1,…, va2
- val1…val2 #contains: val1,…, val2-1
2.Methods:
-
- to_a() #convert to array
- Include?(targetValue)/===(targetValue) #judge whether contains the targetValue
- min()/begin()/first() #Get the minimum value
- max()/end()/last #Get the maximum value
- reject: Convert to an array and select some element whichdon’t satisfy the conditional
- select: Convert to an array and only select some element whichsatisfy the conditional
- each: Iterates over the elements and passing each in turn to the block.
e.g.
a=10..20
puts a. reject{|x| x<15}
puts
puts a. select{|x|x<15}
puts
a. each{|x| puts x}
作者:Glen.He
出处:http://www.cnblogs.com/puresoul/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。