`
dondon
  • 浏览: 10925 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Ruby Trick

阅读更多
很喜欢http://www.rubyinside.com这个网站,里面记载了一些Ruby相关的新闻,以及技术相关的,这里爬几个可能会用到的Ruby Trick.

1. 分解枚举

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"]

2. 不用String 或是 Symbol做hash的键

does = is = { true => 'Yes', false => 'No' }
does[10 == 50]                       # => "No"
is[10 > 5]                           # => "Yes"

3.快速赋值

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

4. 使用Range

# if x > 1000 && x < 2000
year = 1972
puts  case year
        when 1970..1979: "Seventies"
        when 1980..1989: "Eighties"
        when 1990..1999: "Nineties"
      end

5. DRY很彻底

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

6. 使用三目运算符(有一天我居然听说一个我的同事工作了六七年,说没用用过这个操作符,天啊,什么世道?什么公司敢用这样的程序员? 和这样的工作,很危险)

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

7. 写程序要捕捉异常

def do_division_by_zero
   5 / 0
end

begin
  do_division_by_zero
rescue => exception
  puts exception.backtrace
end

8. 确保有东西可迭代

items = "hello"
[*items].each do |item|
  # ...
end

9 捕获异常不一定非要有begin

def x
  begin
    # ...
  rescue
    # ...
  end
end

=>

def x
  # ...
rescue
  # ...
end

10. 块注释

puts "x"
=begin
  this is a block comment
  You can put anything you like here!

  puts "y"
=end
puts "z"

11 rescue to the rescue

h = { :age => 10 }
h[:name].downcase                         # ERROR
h[:name].downcase rescue "No name"        # => "No name"
分享到:
评论

相关推荐

    trick_bag:各种Ruby类和方法来简化和增强您的代码

    特技袋各种Ruby类,模块和方法,以简化和增强您的代码。安装将此行添加到您的应用程序的Gemfile中: gem 'trick_bag'然后执行: $ bundle或将其自己安装为: $ gem install trick_bag用法理解用法的最佳方法是查看a...

    booking_trick

    将您的Ruby代码放在文件lib/booking_trick 。要试验该代码,请运行bin/console进行交互式提示。待办事项:删除此内容和上面的文字,并描述您的宝石安装将此行添加到您的应用程序的Gemfile中: gem 'booking_trick'...

    rschema:Ruby数据结构的基于模式的验证和强制

    Ruby数据结构的基于模式的验证和强制。 强烈地受到启发。 认识RSchema RSchema提供了一种描述,验证和强制数据“形状”的方法。 首先,您创建一个架构: blog_post_schema = RSchema.define_hash {{ title: _...

    card_game:用于打牌游戏的随机Ruby实用程序库

    用于各种纸牌游戏的Ruby实用程序库。 真的只有我在玩设计思想。 概要 您可以比较扑克手。 require 'card_game/poker' include CardGame Poker . classify ( Hand . build ( "10H JH QH KH AH" ) ) &gt; Poker . ...

    英文原版-Instant Puppet 3 starter 1st Edition

    A short, fast, focused guide delivering immediate results.Learn how deterministic results can vastly reduce your work load.Deploy Puppet Server as a Ruby-on-Rails application to handle thousands of ...

Global site tag (gtag.js) - Google Analytics