Forum: Ruby how to call a class from another file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Huseyin P. (Guest)
on 2006-05-08 14:31
Hi everyone, I had this little problem and having no luck so far. I have
external file called

require 'query-eval'  # here the name of the file required query-eval.rb

then there is this class in query-eval.rb that I want to call from my
program that is saved as a different file and IS in the same directory.
Following the instruction in file I had following three line code that
gives error. I think it is because QueryEval is a class not a method. is
there a way to call class without hassle? thank you

 filename='rlv-ass'    # another external file
 qe = QueryEval(filename)
 qe.process_query_results(sorteddocs,1)

ERROR :
project4.rb:243: undefined method `QueryEval' for #<Object:0xb7f4c970>
(NoMethodError)
Robert K. (Guest)
on 2006-05-08 15:16
(Received via mailing list)
2006/5/8, huseyin polat <removed_email_address@domain.invalid>:
>
>  filename='rlv-ass'    # another external file
>  qe = QueryEval(filename)
>  qe.process_query_results(sorteddocs,1)
>
> ERROR :
> project4.rb:243: undefined method `QueryEval' for #<Object:0xb7f4c970>
> (NoMethodError)

You're problem is almost certainly connected to the fact that there's
no "new" in your code.  You probably meant

qe = QueryEval.new filename

Kind regards

robert
Dick D. (Guest)
on 2006-05-08 15:16
(Received via mailing list)
On 08/05/06, huseyin polat <removed_email_address@domain.invalid> wrote:


>  filename='rlv-ass'    # another external file
>  qe = QueryEval(filename)
>  qe.process_query_results(sorteddocs,1)
>
> ERROR :
> project4.rb:243: undefined method `QueryEval' for #<Object:0xb7f4c970>
> (NoMethodError)

This has nothing to do with external files.
You don't call classes  - you call methods.

That method could be a constructor if (as it looks above) you want a
QueryEval object to work with i.e.

class QueryEval
  def initialize(filename)
    # make a new object....
  end

  def process_query_results name, number
    # do whatever
  end

end

and then later you could call that as

  qe = QueryEval.new(filename)
  qe.process_query_results(sorteddocs,1)

Does that help at all?
Huseyin P. (Guest)
on 2006-05-08 15:45
Robert K. wrote:
> 2006/5/8, huseyin polat <removed_email_address@domain.invalid>:
>>
>>  filename='rlv-ass'    # another external file
>>  qe = QueryEval(filename)
>>  qe.process_query_results(sorteddocs,1)
>>
>> ERROR :
>> project4.rb:243: undefined method `QueryEval' for #<Object:0xb7f4c970>
>> (NoMethodError)
>
> You're problem is almost certainly connected to the fact that there's
> no "new" in your code.  You probably meant
>
> qe = QueryEval.new filename
>
> Kind regards
>
> robert

Robert, that was it, I have forgotten the "new" as you have said.. thank
you so much.. thank you///
This topic is locked and can not be replied to.