小编sai*_*ala的帖子

为什么"❨╯°□°❩╯(┻┻┻"这样的编码用于方法名称?

我在sidekiq gem中遇到了以下方法.它刚刚从test_sidekiq.rb调用.

def self.??°?°??????
  puts "Calm down, bro"
end
Run Code Online (Sandbox Code Playgroud)

是我能在SO上找到的唯一链接.

  1. 谷歌无法理解??°?°??????.为什么Ruby没有抱怨这种编码?
  2. 这种方法的目的是什么(不太看它的身体)?
  3. 为什么作者@ mike-perham使用这个名字?只是为了好玩,或测试一些界限?

ruby sidekiq

24
推荐指数
3
解决办法
4263
查看次数

为什么Array.reverse_each比Array.reverse.each更快

我几乎不使用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 arrays

16
推荐指数
2
解决办法
5438
查看次数

何时使用关键字参数也称为Ruby中的命名参数

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)

ruby named-parameters keyword-argument

16
推荐指数
3
解决办法
1万
查看次数

为什么模块的单例方法在混合的下游特征类中不可见?

我理解常规方法查找路径即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)

ruby inheritance mixins eigenclass

7
推荐指数
1
解决办法
1012
查看次数

为什么object_id会在windows上持续存在而不在linux上?

这个计划

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_ids都不会改变.

我还注意到,object_id如果我更改了程序的内容(甚至是一个无关紧要的更改,如添加新行或注释),那么在Windows上进行更改.

为什么Windows和Linux实现之间存在这种差异?由于我无法访问OS X,有人可以在Mac上运行它并记录结果.

我在Windows上使用Ruby 1.9.2-p136,在Linux上使用Ruby 1.9.2-p180.

ruby windows objectid

6
推荐指数
1
解决办法
208
查看次数