3

So idea is to define

def foo
  puts "Works!"
end

and directly from the console without loading anything I write

irb(main):001:0>foo()
=> "Works!"
irb(main):002:0>

I am using 1.9.3 on Windows. I want to use this in order to have a method which will reload lib/* so that I don't need to restart the console. Thank you.

Community
  • 1
  • 1
Haris Krajina
  • 14,824
  • 12
  • 64
  • 81

2 Answers2

10

I think this is what you're asking... I have the following code in an initializer:

if defined?(Rails::Console)
  require "util/console_extensions"
  include ConsoleExtensions
end

and any extra methods I want in the console defined in lib/util/console_extensions.rb

module ConsoleExtensions
  def foo
    puts "Works!"
  end
end

This automatically requires and includes the ConsoleExtension module when loading the rails console and makes the methods defined in it available without the need to manually load anything.

Russell
  • 12,261
  • 4
  • 52
  • 75
0

If this is only for testing purpose then define those files inside models :) and afterwords move them to lib directory

Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48