我在sidekiq gem中遇到了以下方法.它刚刚从test_sidekiq.rb调用.
def self.??°?°??????
puts "Calm down, bro"
end
Run Code Online (Sandbox Code Playgroud)
这是我能在SO上找到的唯一链接.
??°?°??????
.为什么Ruby没有抱怨这种编码?我几乎不使用reverse_each
方法,而是在reverse.each
需要向后遍历数组时调用.所以我只做了一些基准测试,显然reverse_each
速度明显快于reverse.each
.
reverse.each
吗?然而,在我的示例(下面)中,TIME(reverse) + TIME(each) - TIME(reverse.each) ~ 1.2 seconds
对于大小为4的数组,进行了1000万次迭代.无论数组的大小如何,这个时间差都或多或少保持稳定.我已经测试了多达100个元素.
require 'benchmark'
number = 10000000
arr = (1..4).to_a
Benchmark.bm(13) do |x|
x.report("reverse.each") { number.times { arr.reverse.each {|x| x} } }
x.report("reverse_each") { number.times { arr.reverse_each {|x| x} } }
x.report("reverse") { number.times { arr.reverse } }
x.report("each") { number.times { arr.each {|x| x} } }
end
Run Code Online (Sandbox Code Playgroud) Ruby 2.0.0支持关键字参数(KA),我想知道在纯Ruby的上下文中这个特性的好处/用例是什么,特别是考虑到由于关键字匹配导致的性能损失,需要每个时间调用带有关键字参数的方法.
require 'benchmark'
def foo(a:1,b:2,c:3)
[a,b,c]
end
def bar(a,b,c)
[a,b,c]
end
number = 1000000
Benchmark.bm(4) do |bm|
bm.report("foo") { number.times { foo(a:7,b:8,c:9) } }
bm.report("bar") { number.times { bar(7,8,9) } }
end
# user system total real
# foo 2.797000 0.032000 2.829000 ( 2.906362)
# bar 0.234000 0.000000 0.234000 ( 0.250010)
Run Code Online (Sandbox Code Playgroud) 我理解常规方法查找路径即class, superclass/module, all the way up to BasicObject
.我认为对于单链版本的链也是如此,但是当您在元链中混合模块时似乎并非如此.我很感激,如果有人能解释为什么在下面的示例中,当我将此模块包含在Vehicle的本征类中时,调用Automobile
模块的banner
方法而不是单例版本.
module Automobile
def banner
"I am a regular method of Automobile"
end
class << self
def banner
"I am a singleton method of Automobile"
end
end
end
class Vehicle
def banner
"I am an instance method of Vehicle"
end
class << self
include Automobile
def banner
puts "I am a singleton method of Vehicle"
super
end
end
end
class Car < Vehicle
def banner
"I am …
Run Code Online (Sandbox Code Playgroud) 这个计划
class ObjectGarden
class << self.clone
puts self.object_id
end
end
puts ObjectGarden.clone.object_id
Run Code Online (Sandbox Code Playgroud)
在Linux上运行(已在RHEL上测试)object_id
在多次运行中生成不同的s,如我所料.但是,当我在Windows上运行它时,我会在多次运行中获得相同的输出.而且不管我做什么(休眠/关机/"臭名昭着的蓝屏"并重启)object_id
s都不会改变.
我还注意到,object_id
如果我更改了程序的内容(甚至是一个无关紧要的更改,如添加新行或注释),那么在Windows上进行更改.
为什么Windows和Linux实现之间存在这种差异?由于我无法访问OS X,有人可以在Mac上运行它并记录结果.
我在Windows上使用Ruby 1.9.2-p136,在Linux上使用Ruby 1.9.2-p180.