1

I was building my own class that resides in /lib folder and debugging with rails console. I quickly comes to a problem, which I have to reload! my console everytime I modified my class file. Would like to know how to auto-reload this when file changed.

Following is my configuration:

Class Location

/lib/book.rb

Code

class Book  
  def hello
    puts 'hello'
  end
end

config/application.rb

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

Console

rails c
Book.new.hello
TonyTakeshi
  • 5,869
  • 10
  • 51
  • 72

2 Answers2

1

It doesn't look to me like guard will do this, but I could be wrong. My solution, since this is coming up a lot today is to add a load_lib function to my .irbrc file:

(note that this is Rails specific, perhaps someone has a suggestion about that)

 def load_lib
    path = File.join( Rails.root, 'lib') 
    failures = []
    Dir.glob("#{path}/**/*.rb").each {  |file|
    puts "loading: #{file} ... "
    begin
      load file
    rescue => ex
      failures << file 
    end
   } 
  # this second pass is here to try to catch anything that 
  # is dependent on something else
  # could be improved, but is working fine for my needs
  double_failures = []
  for file in failures 
    begin
      load file
    rescue => ex1
      double_failures << file 
    end
  end 

  if double_failures.size > 0 
    puts "these files failed twice"
    for file in double_failures
      puts file
    end
  end 
end

The in the Rails console I just need to run load_lib and my library code is loaded in. It's brute force and might not be perfect but it is meeting my needs. It also doesn't answer your question, but it's close enough?

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • I am using ruby on Windows, where is a place where I can put definition of method, so I can just trigger it from console? (that is avoid loading module for exampl) – Haris Krajina Feb 21 '12 at 09:33
  • @Dolphin I just had a scripts folder in my project ... so I could do `load './scripts/load_lib.rb` from Ruby console. My advice to you: do Rails in a Linux VM and forget about Windows. That's what I did. Rails in Windows is too much work. – jcollum Feb 21 '12 at 14:25
  • So true :) But with 1.9.3 it's not so bad. Made it work now I just call reload_lib method which has your code (thank you), code is in module and I make it available with initializer. Here is solution on this thread http://stackoverflow.com/questions/9375420/where-to-define-method-to-be-accessible-directly-from-rails-console/9375537#comment11841098_9375537. – Haris Krajina Feb 21 '12 at 16:10
  • @Dolphin I'm not sure it's a good idea to put console specific code anywhere in your app. I'd put stuff like that in the .irbrc file. It seems like it's crossing a boundary otherwise. – jcollum Feb 21 '12 at 18:21
0

I think the closest you can get is the guard gem. It'll watch your files and run commands when it sees them change. It's a great addition to a rails workflow.

Here's a rails cast episode for more info.

Edit:

Here's something I just found - a way to keep your irb history. I've never tried it but sounds like it works with rails console as well. With reload! and being able to keep irb history it should be a little more convenient now to reload the console.

Dty
  • 12,253
  • 6
  • 43
  • 61
  • Very useful indeed, but problem is even I can detect file changed, I do not know how to reload the rails console from a command prompt. – TonyTakeshi Nov 25 '11 at 17:02
  • @TonyMocha yeah understood. That's why I started off by saying "the closest you can get...". But is typing `reload!` that bad? I've never had a problem with it. – Dty Nov 26 '11 at 03:39