3

I have a module called Sms that I'm defining in lib/sms.rb. Within it, I have a method called Sms.chunk that uses the method word_wrap. This is part of the TextHelper library, so I am including it at the beginning of the module with include ActionView::Helpers::TextHelper:

module Sms
  include ActionView::Helpers::TextHelper

  def Sms.chunk
  ...
  word_wrap
  ...

I am requiring this module during initialization with the line require "sms" in config/initializers/additional_libs.rb

I also have a Grape API class called TWILIO_API where I want to call Sms.chunk. However, when I do, I get undefined methodword_wrap' for Sms:Module`. I have tried including the TextHelper library in the TWILIO_API class itself, and various other ways of including it, but have had no success.

What am I doing wrong here?

4

1 に答える 1

3

問題は、wrap_wordが でありinstance method、クラス メソッドから呼び出していることですSms.chunk。パーツを落としてインスタンスメソッドにしますSms.。たとえば、次のように動作します。

require 'action_view'
class Test
  include ActionView::Helpers::TextHelper
  def test_method
    word_wrap('Once upon a time')
  end
end
o = Test.new
p o.test_method # "Once upon a time"
于 2013-04-15T05:29:19.243 に答える