Forum: Ruby on Rails using methods in /lib while in script/console session

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.
John S. (Guest)
on 2008-10-22 10:15
While I'm in a script/console session I want access to methods I've
defined in a utilities unit I've put in the project/lib directory. I've
tried requiring the unit and loading the unit but still don't get the
methods. What's the technique to use in this case?
Daniel B. (Guest)
on 2008-10-22 11:20
(Received via mailing list)
On Oct 22, 5:15 pm, John S. <removed_email_address@domain.invalid>
wrote:
> While I'm in a script/console session I want access to methods I've
> defined in a utilities unit I've put in the project/lib directory. I've
> tried requiring the unit and loading the unit but still don't get the
> methods. What's the technique to use in this case?
> --
> Posted viahttp://www.ruby-forum.com/.

Put them into a module.
For instance, I have lib/image_handler.rb.
  module ImageHandler
     def self.foo
       puts 'foo'
     end
     class Image
       ...
     end
     ....
  end

$ script/console
Loading development environment (Rails 2.0.2)
>> ImageHandler
=> ImageHandler
>> ImageHandler::Image
=> ImageHandler::Image
>> ImageHandler.foo
=> "foo"

--
Daniel B.
John S. (Guest)
on 2008-10-22 12:47
Daniel B. wrote:

> Put them into a module.
> For instance, I have lib/image_handler.rb.
>   module ImageHandler
>      def self.foo
>        puts 'foo'
>      end
>      class Image
>        ...
>      end
>      ....
>   end
>
Daniel

Ok thanks that worked.

John S.
This topic is locked and can not be replied to.