• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > 几个Ruby小技巧分享

几个Ruby小技巧分享

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了ruby,ruby碧玺,ruby woo口红,mac ruby woo口红,ruby教程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

代码块的序列调用
def touch_down 
  yield [3, 7] 
  puts "touchdown!" 
end 
 
touch_down do |(first_down, second_down)| 
  puts "#{first_down} yards on the run" 
  puts "#{second_down} yards passed" 
end 
 
=> "3 yards on the run" 
=> "7 yards passed" 
=> "touchdown!" 
</div>

主要是说array在block中的使用

从array中取出元素
>> args = [1, 2, 3] 
>> first, rest = args 
 
>> first 
=> 1 
 
>> rest 
=> [2, 3] 
</div>

之前只是清楚split序列的用法,没有注意到实际上,我们可以方便的得到剩余的序列。

Hash#fetch

>> items = { :apples => 2, :oranges => 3 } 
=> items = {:apples=>2, :oranges=>3} 
 
>> items.fetch(:apples) 
=> 2 
 
>> items.fetch(:bananas) { |key| "We don't carry #{key}!"} 
=> We don't carry bananas! 
</div>

在散列的使用的时候,fetch可能会比检查是否存在值要方便一些。

创建代码段的散列
>> smash = Hash.new { |hash, key| hash[key] = "a #{key} just got SMASHED!" } 
=> {} 
 
>> smash[:plum] = "cannot smash." 
=> {:plum=>"cannot smash."} 
 
>> smash[:watermelon] 
=> {:plum=>"cannot smash.", :watermelon=>"a watermelon just got SMASHED!"}
</div>

将代码段用于生产散列可以方便的保持一些未定义的初始值,特别是在斐波纳契计算中很适合(我没有看出来怎么用)

Array#sort_by
>> cars = %w[beetle volt camry] 
=> ["beetle", "volt", "camry"] 
 
>> cars.sort_by { |car| car.size } 
=> ["volt", "camry", "beetle"] 
</div>

序列的sort_by方法用来对代码段的返回值排序,就如同对于Symbol#to_proc进行map或者sort

String#present?
>> "brain".present? 
=> true 
 
>> "".present? 
=> false 
</div>

Rails的开发者可能对于blank?比较熟悉,然而对于present呢?实际上判断返回值是否正确这也是很好用的方法。

这里我确实想起来,对于find(:all)和find(:first)是否有返回值的判断的不同。还有一个

.exists?
.empty?
.blank?
.nil?

比较多见到吧

</div>

您可能想查找下面的文章:

  • ruby迭代map的简洁写法实现原理分析
  • Ruby中访问SQL Server数据库的配置实例
  • ruby声明式语法的实现例子
  • Ruby中的Range对象学习笔记
  • Ruby中的String对象学习笔记
  • Ruby的基本语法学习总结
  • Ruby中的方法(函数)学习总结
  • Ruby中的变量学习总结
  • Ruby数组(Array)学习笔记
  • Ruby和元编程之万物皆为对象

相关文章

  • Ruby中的类Google Map/Reduce框架Skynet介绍
  • 比较不错的关于ruby的电子书下载地址集合
  • Ruby迭代器的7种技巧分享
  • 提升Ruby on Rails性能的几个解决方案
  • Ruby中遍历目录的简洁方法
  • Windows下Ruby+Watir自动化测试的环境搭建及数据读取
  • Ruby多线程编程初步入门
  • Ruby的字符串与数组求最大值的相关问题讨论
  • 举例讲解Ruby中迭代器Iterator的用法
  • Ruby 魔法 学习笔记之一

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • ruby实现github第三方认证
    • 使用Ruby实现简单的事物驱动的web应用的教程
    • Ruby on Rails中Rack中间件的基础学习教程
    • windows和linux下Ruby的下载与安装
    • Ruby中case表达式详解
    • 在Ruby中处理XML和XSLT以及XPath的简单教程
    • Java版的Ruby解释器 JRuby简介
    • Terry七月Ruby读书笔记(比较详细)第1/4页
    • 实例解析Ruby程序中调用REXML来解析XML格式数据的用法
    • Ruby中操作字符串的一些基本方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有