备忘---ruby


1, Ubuntu安装git
sudo apt-get install git-core

2, Redhat安装git
sudo yum install git-all

3, ruby和SHELL沟通

ifconfig = `/sbin/ifconfig` #``为反引号,1旁边的那个键,用来引用SHELL下的指令,非常方便,返回命令执行后的response给ifconfig
ip = /[d{2}.]{4}/.match(ifconfig)

4, 数组赋值

array = %w{it is like you are back from the dead}
=> ["it", "is", "like", "you", "are", "back", "from", "the", "dead"]

newline = [1,2,3,4,5,6,7]
=> [1, 2, 3, 4, 5, 6, 7]

a = Array.new(5){|i| i*4}
=> [0, 4, 8, 12, 16]

o = Array.new
=> []

a.clear 删除整个数组
a.delete(8) 删除数组里的第8个元素。

 5,HASH    KEY唯一, 数组是HASH的特例。

a, 以Hash关键字头,以[]包起来。
pry(main)> h = Hash["a", 10,"b",11,"c",12, "d",13]
=> {"a"=>10, "b"=>11, "c"=>12, "d"=>13}

b, 或者: h = Hash["a"=>10,"b"=>11,"c"=>12, "d"=>13]

c, 直接以{}包起来,并以=>赋值
pry(main)> h = {"a"=>10,"b"=>3,"c"=>4}
=> {"a"=>10, "b"=>3, "c"=>4}

d, 赋值也可以这样: 
h = Hash.new

e, 可以分别显示hash的key, value.
h.keys
h.values

 6, 类方法与实例方法要区分,还末找到很切实的规律。

原文地址:https://www.cnblogs.com/mover/p/3521346.html