加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

21个Ruby开发编程技巧

发布时间:2020-12-16 22:04:43 所属栏目:百科 来源:网络整理
导读:1. 快速获取正则表达式的匹配值 通常我们使用正则表达式,都是先match,然后再取结果,但是这样有时候会抛异常,看下面例子: email=FredBloggsemail.match(//)[1]#=fred@bloggs.comemail[//,1]#=fred@bloggs.comemail.match(/(x)/)[1]#=NoMethodError[:(]em

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, 3, 1, 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", "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, 4

 

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

def my_method(*args)

  a, 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"

 

原文链接:    21 Ruby Tricks You Should Be Using In Your Own Code  

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读