当前位置: 移动技术网 > IT编程>脚本编程>Ruby > 21个你应该知道的Ruby编程技巧

21个你应该知道的Ruby编程技巧

2017年12月08日  | 移动技术网IT编程  | 我要评论

1. 快速获取正则表达式的匹配值

通常我们使用正则表达式,都是先match,然后再取结果,但是这样有时候会抛异常,看下面例子:

复制代码 代码如下:

email = "fred bloggs "

email.match(//)[1] # => "fred@bloggs.com"

email[//, 1] # => "fred@bloggs.com"

email.match(/(x)/)[1] # => nomethoderror [:(]

email[/(x)/, 1] # => nil

email[/([bcd]).*?([fgh])/, 2] # => "g"

上面例子中还有一种更简单的方法,就是使用 string#[]方法,可以直接匹配正则表达式,更简洁,虽然看起来使用了魔鬼数字。
当然你可以省略掉那个魔鬼数字,如果仅匹配一次的话:

复制代码 代码如下:

x = 'this is a test'

x[/[aeiou].+?[aeiou]/] # => 'is i'

这个例子中,我们匹配规则“先匹配一个元音,然后一个辅音,再接着一个元音”。

2. array#join!的快捷实现

我们知道array的*操作,是将数组里面的元素成倍的增加:

复制代码 代码如下:

[1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3]

但是当乘因子不是数字是字符串会出现什么效果?

复制代码 代码如下:

%w{this is a test} * ", " # => "this, is, a, test"

h = { :name => "fred", :age => 77 }

h.map { |i| i * "=" } * "n" # => "age=77nname=fred"

对了,这就是join!的效果。因此可以用这种方式来快捷地实现join!操作。

3. 快速格式化十进制数字

格式化浮点数字的精度显示通常使用sprintf这个函数,可是有一种更快捷的方式,使用字符串。

复制代码 代码如下:

money = 9.5

"%.2f" % money # => "9.50"

4. 快速解析字符串

在技巧3中我们看到了数字的格式化,这里就说一下字符串格式的快捷方式。

复制代码 代码如下:

"[%s]" % "same old drag" # => "[same old drag]"

这里的意思是将”same old drag”显示到[]中。
我们再看一下具体的格式化方法:

复制代码 代码如下:

x = %w{p hello p}

"%s" % x # => "

hello

5. 递归删除文件和目录

fileutils提供了这种方法:

复制代码 代码如下:

require 'fileutils'

fileutils.rm_r 'somedir'

还有一个方法是fileutils.rm_rf,与linux上的 rm -rf 对应。

6. 快速穷举可枚举对象

使用*操作可以快速的穷举出可枚举对象中的所有元素,像array,hash这种对象。

复制代码 代码如下:

a = %w{a b}

b = %w{c d}

[a + b]                              # => [["a", "b", "c", "d"]]

[*a + b]                             # => ["a", "b", "c", "d"]

这里*操作符的优先级低于+操作符。


复制代码 代码如下:

a = { :name => "fred", :age => 93 }

[a]                                  # => [{:name => "fred", :age =>93}]

[*a]                                 # => [[:name, "fred"], [:age, 93]]


a = %w{a b c d e f g h}

b = [0, 5, 6]

a.values_at(*b).inspect              # => ["a", "f", "g"]

7. 消除临时变量

我们有时候需要写一个临时变量如一个临时的array,有一种方式可以不用单独定义临时变量:

复制代码 代码如下:

(z ||= []) << 'test'

当然这不是一种很好的编程习惯,建议不要大量在代码中使用。

8. 使用非字符串或非symbol对象作为hash的key

或许你从来没有尝试过使用非string或非symbol对象作为hash的key,但是确实是可以的,有时候还蛮有用。

复制代码 代码如下:

does = is = { true => 'yes', false => 'no' }

does[10 == 50]                       # => "no"

is[10 > 5]                           # => "yes"

9. 使用and或or将多个操作组合到一行

这个技巧很多熟练的ruby程序员都会使用,用来替代if和unless这种短行代码。

复制代码 代码如下:

queue = []

%w{hello x world}.each do |word|

  queue << word and puts "added to queue" unless word.length <  2

end

puts queue.inspect


# output:

#   added to queue

#   added to queue

#   ["hello", "world"]

但是注意,这种方式,若and左边表达式“queue << word”返回nil则“puts "added to queue"”不会被执行,要慎用。不是geek建议不要用。

10. 判断当前ruby解析器是否在执行自己的脚本

有时候你可能需要判断当前的运行环境是否在自己的ruby脚本文件中,那么可以使用:

复制代码 代码如下:

if __file__ == $0

  # do something.. run tests, call a method, etc. we're direct.

end

11. 快速地批量给变量赋值

最常用的多个变量赋值方法是:

复制代码 代码如下:

a, b, c, d = 1, 2, 3, 4

在函数中可以批量赋值,通过传*的参数:

复制代码 代码如下:

def my_method(*args)

  a, b, c, d = args

end

还可以批量设置成员变量:

复制代码 代码如下:

def initialize(args)

  args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] }

end

12. 使用range替代复杂的数字大小比较

如果要比较if x > 1000 && x < 2000 ,可以使用如下方式替代:

复制代码 代码如下:

year = 1972

puts  case year

        when 1970..1979: "seventies"

        when 1980..1989: "eighties"

        when 1990..1999: "nineties"

      end

13. 使用枚举操作替换重复代码

写代码最忌讳“重复”,在ruby中有时候会require很多文件,可以使用下面的方式省去重复的require:

复制代码 代码如下:

%w{rubygems daemons eventmachine}.each { |x| require x }

14. 三元操作

ruby和其他语言一样,有三元操作:

复制代码 代码如下:

puts x == 10 ? "x is ten" : "x is not ten"


# or.. an assignment based on the results of a ternary operation:

log.sev_threshold = environment == :development ? logger::debug : logger::info

15. 嵌套的三元操作


复制代码 代码如下:

qty = 1

qty == 0 ? 'none' : qty == 1 ? 'one' : 'many'

# just to illustrate, in case of confusion:

(qty == 0 ? 'none' : (qty == 1 ? 'one' : 'many'))

16. 几种返回true,false的实现

最普通的是:

复制代码 代码如下:

def is_odd(x)

  # wayyyy too long..

  if x % 2 == 0

    return false

  else

    return true

  end

end

可以简洁一点是:

复制代码 代码如下:

def is_odd(x)

  x % 2 == 0 ? false : true

end

还可以更简洁:

复制代码 代码如下:

def is_odd(x)

  # use the logical results provided to you by ruby already..

  x % 2 != 0

end

当然,有时候你担心返回的nil(ruby中的判定规则是nil为false,其他都为true),那么可以显示转换:

复制代码 代码如下:

class string

  def contains_digits

    self[/d/] ? true : false

  end

end

17. 查看异常堆栈


复制代码 代码如下:

def do_division_by_zero; 5 / 0; end

begin

  do_division_by_zero

rescue => exception

  puts exception.backtrace

end

18. 将一个对象转换为数组

*操作符可以将一个对象转换为数组对象

复制代码 代码如下:

1.9.3p125 :005 > items = 123456

 => 123456

1.9.3p125 :006 > [*items]

 => [123456]

1.9.3p125 :007 > [*items].each do | i | puts i end

123456

 => [123456]

19. 没有begin的rescue块

ruby中捕获异常的代码是begin...rescue...:

复制代码 代码如下:

def x

  begin

    # ...

  rescue

    # ...

  end

end

但是rescue可以没有begin:

复制代码 代码如下:

def x

  # ...

rescue

  # ...

end

20. 块注释

c和java中的块代码注释是/**/,ruby中也有类似的块注释:

复制代码 代码如下:

puts "x"

=begin

  this is a block comment

  you can put anything you like here!


  puts "y"

=end

puts "z"

21. 抛出异常

java中抛异常是使用throw,ruby中更灵活,可以在一行代码中直接抛异常而不中断当前调用栈:

复制代码 代码如下:

h = { :age => 10 }

h[:name].downcase                         # error

h[:name].downcase rescue "no name"        # => "no name"

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网