我喜欢ruby的一点是,它基本上是一种可读性很强的语言(非常适合自文档化代码)

然而,受到这个问题的启发:Ruby Code explained

因此,我的问题是,与前面提到的问题类似,要成为一名真正熟练的ruby程序员,我需要了解哪些常见但不明显的ruby习惯用法?

顺便说一下,根据参考问题

a ||= b 

相当于

if a == nil || a == false
  a = b
end

(感谢伊恩·特雷尔的更正)

事实证明,这一点并非完全没有争议.正确的扩展实际上是

(a || (a = (b))) 

查看以下链接了解原因:

感谢Jörg W Mittag指出这一点.

推荐答案

magic if子句允许同一文件用作库或脚本:

if __FILE__ == $0
  # this library may be run as a standalone script
end

打包和解包数组:

# put the first two words in a and b and the rest in arr
a,b,*arr = *%w{a dog was following me, but then he decided to chase bob}
# this holds for method definitions to
def catall(first, *rest)
  rest.map { |word| first + word }
end
catall( 'franken', 'stein', 'berry', 'sense' ) #=> [ 'frankenstein', 'frankenberry', 'frankensense' ]

哈希作为方法参数的语法糖

this(:is => :the, :same => :as)
this({:is => :the, :same => :as})

哈希初始值设定项:

# this
animals = Hash.new { [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {}
# is not the same as this
animals = Hash.new { |_animals, type| _animals[type] = [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {:squirrels=>[:Rocket, :Secret], :dogs=>[:Scooby, :Scrappy, :DynoMutt]}

元类语法

x = Array.new
y = Array.new
class << x
  # this acts like a class definition, but only applies to x
  def custom_method
     :pow
  end
end
x.custom_method #=> :pow
y.custom_method # raises NoMethodError

类实例变量

class Ticket
  @remaining = 3
  def self.new
    if @remaining > 0
      @remaining -= 1
      super
    else
      "IOU"
    end
  end
end
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> "IOU"

块、进程和lambdas.活下go ,呼吸它们.

 # know how to pack them into an object
 block = lambda { |e| puts e }
 # unpack them for a method
 %w{ and then what? }.each(&block)
 # create them as needed
 %w{ I saw a ghost! }.each { |w| puts w.upcase }
 # and from the method side, how to call them
 def ok
   yield :ok
 end
 # or pack them into a block to give to someone else
 def ok_dokey_ok(&block)
    ok(&block)
    block[:dokey] # same as block.call(:dokey)
    ok(&block)
 end
 # know where the parentheses go when a method takes arguments and a block.
 %w{ a bunch of words }.inject(0) { |size,w| size + 1 } #=> 4
 pusher = lambda { |array, word| array.unshift(word) }
 %w{ eat more fish }.inject([], &pusher) #=> ['fish', 'more', 'eat' ]

Ruby相关问答推荐

有没有办法把条件语句写得更干净?

Ruby:程序终止后仅写入/保存/可见的文件数据

如何从外部获取给定 lambda 的参数值,而不显式返回其绑定?

Rspec: expectvsexpect什么区别?

如何从 Ruby 中的线程返回值?

Ruby语法

如何判断是否安装了gem?

Ruby - 查看端口是否打开

如何从 SystemStackError 中获取回溯:堆栈级别太深?

如何在 ruby​​ net/http 中实现 cookie 支持?

无法对 ruby​​ 哈希使用点语法

Ruby - 在模块/类之间共享记录器实例

从类对象获取类位置

如何在 Ruby 中找到除法的余数?

测试变量是否等于两个值之一

如何修复错误的 URI 不是 URI

我安装了一个 gem,但 require 'gemname' 不起作用.为什么?

何时在 Ruby 中使用 `require`、`load` 或 `autoload`?

通过 factory_girl 协会查找或创建记录

如何在 Ruby 中找到字符串中字符的索引?