小编Dan*_*sta的帖子

代码优化/更好的操作类名的方法

我有这段代码很长时间了,我想知道是否有更短或更好的方法来做到这一点。我们有匹配的服务和工作人员,此代码为某些工作人员找到匹配的服务。

def FooWorker
  def service_klass
    self.class.name
        .then(&:underscore)
        .then { |name| name.split('_') }
        .then { |name| name[0...-1] << :service }
        .then { |name| name.join('_') }
        .classify
        .safe_constantize
  end
end

FooWorker.service_klass # -> FooService
Run Code Online (Sandbox Code Playgroud)

ruby optimization ruby-on-rails

0
推荐指数
1
解决办法
31
查看次数

标签 统计

optimization ×1

ruby ×1

ruby-on-rails ×1