strip_tags //格式化标签
[1,2,3,4,5,6,7,8,9,10].map{|e| e*3}
=> [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[1,2,3,4,5,6,7,8,9,10].collect{|e| e == 3}
=>[false, false, true, false, false, false, false, false, false, false]
eg:[1,2,3,4,5,6,7,8,9,10].each{|e| print e.to_s + "###"}
=> 1###2###3###4###5###6###7###8###9###10###
=>返回 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
eg:[1,2,3,4,5,6,7,8,9,10].select{|e| e == 3}
=> [3]
eg:[1,2,3,4,5,6,7,8,9,10].select{|e| e*3}
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
eg:[1,2,3,4,5,6,7,8,9,10].detect{|e| e == 3 || e == 2}
=> 2
eg:[1,2,3,4,5,6,7,8,9,10].inject{ |sum,e| sum += e } //其中sum为所声明的注入器 将累加后的结果最后注入到sum中并返回注入器最后结果
eg:[1,2,3,4,5,6,7,8,9,10].inject{|result,elem| result + elem} => 55 //此时inject没有带参数,这是result会为数组中第一个元素,elem为第二个元素,依次累加返回最后结果
eg: {1 => 'a',2 => 'b',3 =>'c'}.inject({}){|key,value| value} key => {} value => [3,'c']
eg:[1,2,3,4,5,6,7,8,9,10].reject{|e| e==2 || e==8}
=> [1, 3, 4, 5, 6, 7, 9, 10]
module Log
def class_type
"This class is of type: #{self.class}"
end
end
class TestClass
include Log
end
tc = TestClass.new.class_type
puts tc #This class is of type: TestClass
module Log
def class_type
"This class is of type: #{self.class}"
end
end
class TestClass
extend Log
# ...
end
tc = TestClass.class_type
puts tc # This class is of type: TestClass
当你在类中使用 Extend 来代替 Include, 如果你实例化 TestClass 并调用 class_type方法时,你将会得到 NoMethodError。再一次强调, 使用 Extend 时方法是类方法。