ruby code block
程序员文章站
2022-07-14 12:32:56
...
find
find/detect => Object or nil
find_all/select => Array
any? => Boolean
all? => Boolean
delete_if => Array
search
merge后面的会覆盖前面的
h1.merge(h2) {|k, o, n| o < n ? o : n}
>{"a"=>111, "b"=>222, "c"=>444}
(1..5).find {|i| i == 5} >5 (1..10).find {|i| i % 3 == 0} >3 只返回第一个 (1..10).detect {|i| i % 3 == 0} >3 (1..10).detect {|i| (1..10).include?(i * 3)} >1 (1..10).find_all {|i| i % 3 == 0} >[3, 6, 9] (1..10).select {|i| (1..10).include?(i * 3)} >[1, 2, 3] (1..10).any? {|i| i % 3 == 0} >true (1..10).all? {|i| i % 3 == 0} >false [1..10].delete_if {|i| i % 3 == 0} >[1, 2, 4 ,5, 7, 8, 10]
find/detect => Object or nil
find_all/select => Array
any? => Boolean
all? => Boolean
delete_if => Array
search
h1 = { "a" => 111, "b" => 222} h2 = {"b" => 333, "c" => 444} h1.merge(h2) >{“a”=>111, "b"=>333, "c"=>444} h1.merge(h2) >{"a"=>111, "b"=>222, "c"=>444} h1.merge(h2) {|key, old, new| new} >{“a”=>111, "b"=>333, "c"=>444} h1.merge(h2) {|key, old, new| old} >{"a"=>111, "b"=>222, "c"=>444} h1.merge(h2) {|key, old, new| old * 5} >{"a"=>111, "b"=>1110, "c"=>444} h1.merge(h2) do |key, old, new| if old < new old else new end end
merge后面的会覆盖前面的
h1.merge(h2) {|k, o, n| o < n ? o : n}
>{"a"=>111, "b"=>222, "c"=>444}
上一篇: agile rails
下一篇: 并发编程(一)
推荐阅读
-
Ruby代码调整性能优化的几个Tip
-
Ruby小技巧:处理方法调用中的nil
-
EF core Code First 一些小结
-
Enterprise Library2.0(1):Data Access Application Block学
-
9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
-
8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
-
使用 Reek 检查 feature branch 相关文件的 code smell
-
[ Ruby on Rails ] 簡單好用的驗證碼Gem – Redis Captcha
-
php-Accessing Swift Code IFSC Codes API, Php Script Error
-
inline-block后margin失效的问题_html/css_WEB-ITnose