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 ? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Can anyone help me ~~ On 12月2日, 下午12时04分, hei <cy...-KN7UnAbNpbg@public.gmane.org> wrote:> 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 ?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-02 09:51 UTC
Re: Problem with extend rails(add new function in lib)
On Dec 2, 5:59 am, hei <cy...-KN7UnAbNpbg@public.gmane.org> 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> On 12月2日, 下午12时04分, hei <cy...-KN7UnAbNpbg@public.gmane.org> wrote: > > > 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 ?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 2, 5:59 am, hei <cy...-KN7UnAbNpbg@public.gmane.org> 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 > > > On 12月2日, 下午12时04分, hei <cy...-KN7UnAbNpbg@public.gmane.org> wrote: > > > > 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 ?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-02 10:37 UTC
Re: Problem with extend rails(add new function in lib)
On 2 Dec 2008, at 10:13, hei wrote:> > 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.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> > 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 Cheung > <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On Dec 2, 5:59 am, hei <cy...-KN7UnAbNpbg@public.gmane.org> 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 >> >>> On 12月2日, 下午12时04分, hei <cy...-KN7UnAbNpbg@public.gmane.org> wrote: >> >>>> 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 ? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It works, using extend RMMSeg thank you ~~ On 12月2日, 下午6时37分, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2 Dec 2008, at 10:13, hei wrote: > > > > > > > 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. > > 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 > > > > > 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 Cheung > > <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> On Dec 2, 5:59 am, hei <cy...-KN7UnAbNpbg@public.gmane.org> 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 > > >>> On 12月2日, 下午12时04分, hei <cy...-KN7UnAbNpbg@public.gmane.org> wrote: > > >>>> 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 ?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---