Hi, Question... I want a model to use info from a file (parks_bielkowski_test.rb) residing in lib (standard ruby file containing a method called "generated_question", that returns a hash). The way I have it set up, I get "You have a nil object when you didn''t expect it!" if I use the symbol :generated_question in the model (see below) or, "undefined local variable or method `question_instance'' for #<Question:0x00000100b35be0>" if I try it without the preceding colon (generated_question) elsif self.question_type == "generator" require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb" formatted = { :complete_question => :generated_question} end I dont seem to be able to get the model to see the hash created in the "parks_bielkowski_test" file, and any suggestions would be helpful. Thanks, DC -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 13 March 2011 23:36, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I want a model to use info from a file (parks_bielkowski_test.rb) > residing in lib (standard ruby file containing a method called > "generated_question", that returns a hash).Can you make the file a module and mix it in? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
On Mar 13, 11:36 pm, Dave Castellano <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > Question... > > I want a model to use info from a file (parks_bielkowski_test.rb) > residing in lib (standard ruby file containing a method called > "generated_question", that returns a hash). > > The way I have it set up, I get "You have a nil object when you didn''t > expect it!" if I use the symbol :generated_question in the model (see > below) or, "undefined local variable or method `question_instance'' for > #<Question:0x00000100b35be0>" if I try it without the preceding colon > (generated_question) > > elsif self.question_type == "generator" > > require > "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb" > > formatted = { :complete_question => :generated_question} > end > > I dont seem to be able to get the model to see the hash created in the > "parks_bielkowski_test" file, and any suggestions would be helpful.What does parks_bielkowski_test.rb look like? ( require ''generators/ parks_bielkowski_test'' should be enough - your app''s lib folder should be in ruby''s load path) Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
I will have hundreds of methods I need the model to call on depending on the situation (only one at a time), where should they be kept, and how to call them? I have tried... require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb" in the model to access the test file but it does not seem to return a value. Is this the correct use of "require"? The test file contains a module DC -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 Mar 2011, at 00:49, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I will have hundreds of methods I need the model to call > on depending on the situation (only one at a time), where should they > be kept, and how to call them? > > I have tried... > > require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb" in the > model to access the test file but it does not seem to return a value. > Is this the correct use of "require"? >Require just loads the file. You don''t get a meaningful return value from it> The test file contains a moduleIf you''ve defined your method in a module then in order to call that method you need to include it in the current object (alternatively you could make it a module method and then call SomeModule.method_name) You might do well to do a little reading on how method lookup, modules, inheritance etc work in ruby Fred> > DC > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Fred, I am reading about inheritance and modules. I think using the module is the correct way to do this but... In model... class Question < ActiveRecord::Base require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb" include P_b In parks_bielkowski_test.rb module Parks_Bielkowski_test def P_b # Parks-Bielkowski step 1 result: i = rand if i < 0.5 ect... I get the ERROR: uninitialized constant Question::P_b Doesn''t seem to be including P_b Thanks, DC -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 15, 12:12 pm, Dave Castellano <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks Fred, I am reading about inheritance and modules. I think using > the module is the correct way to do this but... >What you include is the module, not the methods in it Fred> Thanks, > DC > > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 wrote in post #987535:> On Mar 15, 12:12pm, Dave Castellano <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Thanks Fred, I am reading about inheritance and modules. I think using >> the module is the correct way to do this but... >> > > What you include is the module, not the methods in it > > FredThanks Fred, Same error when I tried that: uninitialized constant Question::Parks_Bielkowski_test I''ll do some more research. I really appreciate your help and guidance but obviously am missing something simple. PS I am actually a non-programmer (physician) who is trying to write a single program-finding this is a steep learning curve!! Thanks, DC -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The book Metaprogramming in Ruby teaches everything you need to know about modules and more. Also, you might the book Eloquent Ruby a very nice introduction to Ruby and its finer points. It''s well-written and dives into all facets (no pun intended) of Ruby. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you. DC -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 15, 1:09 pm, Dave Castellano <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #987535: > > > On Mar 15, 12:12pm, Dave Castellano <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> Thanks Fred, I am reading about inheritance and modules. I think using > >> the module is the correct way to do this but... > > > What you include is the module, not the methods in it > > > Fred > > Thanks Fred, Same error when I tried that: > > uninitialized constant Question::Parks_Bielkowski_test > > I''ll do some more research. I really appreciate your help and guidance > but obviously am missing something simple. >Have you still got the require there? Fred> PS I am actually a non-programmer (physician) who is trying to write a > single program-finding this is a steep learning curve!! > > Thanks, > DC > > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.