Hi,
Newbie question…
If I have hundreds of methods I need a model to call
on depending on the situation (only one at a time), where should they
be kept, and how to
call them?
Thanks,
DC
Hi,
Newbie question…
If I have hundreds of methods I need a model to call
on depending on the situation (only one at a time), where should they
be kept, and how to
call them?
Thanks,
DC
On 13 March 2011 23:36, Dave C. [email protected] 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?
On Mar 13, 11:36pm, Dave C. [email protected] wrote:
below) or, "undefined local variable or method `question_instance’ for
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
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
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
On 15 Mar 2011, at 00:49, Dave C. [email protected] 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 module
If 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
On Mar 15, 12:12pm, Dave C. [email protected] 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
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.
Thank you.
DC
Frederick C. wrote in post #987535:
On Mar 15, 12:12pm, Dave C. [email protected] 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.
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
On Mar 15, 1:09pm, Dave C. [email protected] wrote:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs