为什么我收到了#34;未检测到的tLABEL,期待')'错误信息

时间:2017-11-28 19:17:43

标签: ruby-on-rails

在第4行,我收到错误消息"unexpected tLABEL, expecting ')'

模块占位符      扩展ActiveSupport :: Concern

 def self.image_generator(height: , width:)
 "http://via.placeholder.com/#{height}x#{width}"
 end

2 个答案:

答案 0 :(得分:2)

你有2个额外的“:”不知道为什么......

module Placeholder
  extend ActiveSupport::Concern

  def self.image_generator(height , width)
    "http://via.placeholder.com/#{height}x#{width}"
  end
end

除非您想传递关键字参数。看起来像这样:

module Placeholder
  extend ActiveSupport::Concern

  def self.image_generator(**args)
    "http://via.placeholder.com/#{args[:height]}x#{args[:width]}"
  end
end

答案 1 :(得分:2)

除非您想使用关键字参数,否则您不需要冒号:

class Foo
  def image_generator(height: '60', width: '60')
    "http://via.placeholder.com/#{height}x#{width}"
  end
end

foo = Foo.new
p foo.image_generator # arguments aren't passed, it uses the "default" values
p foo.image_generator(height: 640, width: 480) # arguments are passed, it uses them

class Foo
  def image_generator(height, width)
    "http://via.placeholder.com/#{height}x#{width}"
  end
end

foo = Foo.new
p foo.image_generator # arguments not passed: it raises "wrong number of arguments"
p foo.image_generator(640, 480) # arguments are passed, it uses them