# Problem with extend rails(add new function in lib)

**URL:** <https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612>\
**Category:** Rails\
**Created:** [December 2, 2008, 5:04am UTC](https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612 "2008-12-02T05:04:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [December 2, 2008, 5:04am UTC](https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612/1 "2008-12-02T05:04:59Z")

</div>

I do things below:

In lib/translater.rb  
class Translater  
require “rubygems”  
require “rmmseg”  
include “RMMSeg”  
…  
def segment(text)  
RMMSeg::segment(text)  
end  
end

in helper/xx/xxx.rb  
module xx  
require “translater.rb”  
def translate(text)  
Translater.run(text)  
end  
end

when i use helper method tranlate, erros:  
undefined method `segment’ for RMMSeg:Module  
But when I test these in a simple ruby program it works well  
Can anyone help me ?

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [December 2, 2008, 7:00am UTC](https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612/2 "2008-12-02T07:00:13Z")

</div>

Can anyone help me ~~

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [December 2, 2008, 10:52am UTC](https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612/3 "2008-12-02T10:52:17Z")

</div>

On Dec 2, 5:59 am, hei [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone help me ~~

What’s the run method in translater? ALso it looks like this isn’t the  
exact code that is running - for example include “RMMSeg” is almost  
certainly include RMMSeg; it’s very hard to say anything about code  
when it has been changed in small ways in between what you’re running  
and this mailing list - those changes could be key or could be hiding  
the real problem.

Fred

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [December 2, 2008, 11:14am UTC](https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612/4 "2008-12-02T11:14:12Z")

</div>

Thanks!  
class Translater  
require “rubygems”  
require “rmmseg”  
include RMMSeg

RMMSeg::Config.max\_word\_length = 7  
RMMSeg::Config.algorithm = :complex  
RMMSeg::Config.dictionaries = [[“dict/chars.dic”, true],  
[“dict/words.dic”, false],  
[“dict/desc.dic”, false],  
[“dict/identity.dic”, false],  
[“dict/name.dic”, false],  
]

Word = {}  
File.open(File.join(File.dirname( **FILE** ), “dict”,  
“word\_china\_english.trans”)).each do |line|  
arr = line.split(" ")  
arr[1] = arr[0] unless arr[1]  
Word[arr[0]] = Word[arr[1]]  
end  
def self.segment(phrase)  
p RMMSeg::Config.max\_word\_length  
RMMSeg::segment(phrase)  
end

def self.run(phrase)  
words = segment(phrase)  
translation = “”  
words.each do |word|  
translation += Word[word].to\_s + " "  
end  
translation  
end

end

The actual problem is : why can not i use methods of some libray  
(rmmseg in this exam) in rails.  
I can use it in simple ruby program.  
In rails, I encapsulate that method(segment) by a ruby class  
(Translater), and put the source in folder lib/  
when using it in helper functions, problem occurs like this:  
undefined method `segment’ for RMMSeg:Module

Thanks for your help again!

On 12ÔÂ2ÈÕ, ÏÂÎç5Ê±51·Ö, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [December 3, 2008, 3:35am UTC](https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612/5 "2008-12-03T03:35:39Z")

</div>

It works, using extend RMMSeg  
thank you ~~

On 12ÔÂ2ÈÕ, ÏÂÎç6Ê±37·Ö, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hei](https://www.ruby-forum.com/u/hei)\
**Post date:** [December 2, 2008, 11:37am UTC](https://www.ruby-forum.com/t/problem-with-extend-rails-add-new-function-in-lib/154612/6 "2008-12-02T11:37:45Z")

</div>

On 2 Dec 2008, at 10:13, hei wrote:

> ```
> ["dict/words.dic", false],
> 
> ```
> 
> Word[arr[0]] = Word[arr[1]]  
> translation += Word[word].to\_s + " "  
> end  
> translation  
> end
> 
> end
> 
> The actual problem is : why can not i use methods of some libray  
> (rmmseg in this exam) in rails.  
> I can use it in simple ruby program.

You must be doing something slightly different in your standalone ruby  
script. The segment method from the RMMSeg module can’t be called on  
RMMSeg itself. You need to include the module in something

if you were to remove the segment method you’ve defined and replace it  
with  
extend RMMSeg  
then it would probably work.  
or equivalently

class Translater  
…  
class \<\< self  
include RMSSeg  
def run(phrase)  
…  
end  
end  
end

Fred
