# Call \`load\` method frequently

**URL:** <https://rubytalk.org/t/call-load-method-frequently/73693>\
**Category:** ruby-talk\
**Created:** [4 July 2016 03:21 UTC](https://rubytalk.org/t/call-load-method-frequently/73693 "2016-07-04T03:21:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![timlen\_tse](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/timlen_tse/32/2019_2.png) [@timlen\_tse](https://rubytalk.org/u/timlen_tse)\
**Post date:** [4 July 2016 03:21 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/1 "2016-07-04T03:21:02Z")

</div>

Hi, there!  
I meet a problem in my work how to track files if modified in runtime. If  
some files change, reload them and pickup the new code in runtime.  
The idea comes up is to use `load` method. For example:

loop do  
&nbsp;&nbsp;&nbsp;&nbsp;if files modified  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;load 'file.rb'  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

But I stuck at how to check if a file was modified ? Is ruby has some  
tricks for that ?

Another concern is that whether it will take up the menory if i load the  
same file many times.

Best regard!

---

<div class="post-metadata">

**Author:** ![Michael\_Schwarze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/michael_schwarze/32/1876_2.png) [@Michael\_Schwarze](https://rubytalk.org/u/Michael_Schwarze)\
**Post date:** [4 July 2016 04:36 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/2 "2016-07-04T04:36:53Z")

</div>

Hi,

> **···**
>
> > Am 04.07.2016 um 05:21 schrieb timlen tse \<tinglenxan@gmail.com\>:
> > 
> > But I stuck at how to check if a file was modified ? Is ruby has some tricks for that ?
> 
> Seems to be more of an operating system issue; in Ruby I found the Fingerprint Gem [1] which you might want to try out.
> 
> Cheers,  
> Michael
> 
> [1] [GitHub - ioquatix/fingerprint: Fingerprint is a simple tool that can be used to verify the contents of a directory.](https://github.com/ioquatix/fingerprint)

---

<div class="post-metadata">

**Author:** ![Recursive\_Madman](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Recursive\_Madman](https://rubytalk.org/u/Recursive_Madman)\
**Post date:** [4 July 2016 21:14 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/3 "2016-07-04T21:14:11Z")

</div>

> But I stuck at how to check if a file was modified ? Is ruby has some  
> tricks for that ?

Ruby itself does not have tricks for that. There are various OS specific techniques for watching filesystem changes without polling, for example:

Linux: [inotify - Wikipedia](https://en.wikipedia.org/wiki/Inotify)  
BSD: [kqueue - Wikipedia](https://en.wikipedia.org/wiki/Kqueue)  
Mac OS X: [FSEvents - Wikipedia](https://en.wikipedia.org/wiki/FSEvents)

And of course there are ruby bindings for all of those.

> Another concern is that whether it will take up the menory if i load the  
> same file many times.

My intuition says that as soon as a method is no longer reachable, the GC cleans it up (and I haven't read anything to the contrary).  
As others already suggested, trying it out would give a definite answer 🙂

I'm pretty sure btw, there are a bunch of libraries / tools in the rubyverse already, implementing just what you are trying to do.  
And if you don't find anything, you can always peak at the rails code, to see how they do it.

cheers,  
-recmm

> **···**
>
> On 07/04/2016 05:21 AM, timlen tse wrote:
> 
> > Best regard!

---

<div class="post-metadata">

**Author:** ![Carlo\_E\_Prelz](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Carlo\_E\_Prelz](https://rubytalk.org/u/Carlo_E_Prelz)\
**Post date:** [4 July 2016 05:09 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/4 "2016-07-04T05:09:19Z")

</div>

Quoting Michael Schwarze (michael@schwarze-web.de):

> **···**
>
> Subject: Re: call `load` method frequently  
> &nbsp;&nbsp;Date: lun 04 lug 16 06:36:53 +0200
> 
> > \> Am 04.07.2016 um 05:21 schrieb timlen tse \<tinglenxan@gmail.com\>:  
> > \>  
> > \> But I stuck at how to check if a file was modified ? Is ruby has some tricks for that ?
> > 
> > Seems to be more of an operating system issue; in Ruby I found the Fingerprint Gem which you might want to try out.
> 
> A method of the File class returns the modification time of a file:
> 
> $ irb  
> irb(main):001:0\> File::mtime('/etc/passwd')  
> =\> 2016-06-29 18:18:39 +0200  
> irb(main):002:0\>
> 
> Carlo
> 
> --  
> &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

---

<div class="post-metadata">

**Author:** ![timlen\_tse](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/timlen_tse/32/2019_2.png) [@timlen\_tse](https://rubytalk.org/u/timlen_tse)\
**Post date:** [4 July 2016 06:44 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/5 "2016-07-04T06:44:50Z")

</div>

Thanks a lot!  
The replies gave me inspiration to do the trick.

But i still concern about whether it will take up the memory if i load the  
same file many times . Note that i just modified the methods implement inside  
the file and will not add new methods. Is ruby smart enough to just  
overwrite the old methods and will not take up a lot memory ?

Carlo E. Prelz \<fluido@fluido.as\>于2016年7月4日周一 下午1:17写道：

> **···**
>
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject: Re: call `load` method frequently  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date: lun 04 lug 16 06:36:53 +0200
> > 
> > Quoting Michael Schwarze (michael@schwarze-web.de):
> > 
> > \> \> Am 04.07.2016 um 05:21 schrieb timlen tse \<tinglenxan@gmail.com\>:  
> > \> \>  
> > \> \> But I stuck at how to check if a file was modified ? Is ruby has some  
> > tricks for that ?  
> > \>  
> > \> Seems to be more of an operating system issue; in Ruby I found the  
> > Fingerprint Gem which you might want to try out.
> > 
> > A method of the File class returns the modification time of a file:
> > 
> > $ irb  
> > irb(main):001:0\> File::mtime('/etc/passwd')  
> > =\> 2016-06-29 18:18:39 +0200  
> > irb(main):002:0\>
> > 
> > Carlo
> > 
> > --  
> > &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> > \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> > &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 July 2016 08:08 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/6 "2016-07-04T08:08:40Z")

</div>

> But i still concern about whether it will take up the memory if i load the  
> same file many times .

That completely depends on what code is in the file.

> Note that i just modified the methods implement  
> inside the file and will not add new methods. Is ruby smart enough to just  
> overwrite the old methods and will not take up a lot memory ?

Methods will be overridden. Not sure though whether old versions will  
be GCed. You can easily try this out, right?

I do question your approach though to modify methods of a class in the  
file while the application is running. What are you trying to achieve  
with this?

Kind regards

robert

> **···**
>
> On Mon, Jul 4, 2016 at 8:44 AM, timlen tse \<tinglenxan@gmail.com\> wrote:
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![timlen\_tse](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/timlen_tse/32/2019_2.png) [@timlen\_tse](https://rubytalk.org/u/timlen_tse)\
**Post date:** [4 July 2016 08:51 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/7 "2016-07-04T08:51:00Z")

</div>

Hi, Robert Klemme.  
\*Yeah, it did pick up the new version.\*  
\*My approach is that i don't want to restart the rake task each time i  
modified the code, like development mode in rails.\*  
\*The rake task is a capybara test which will launch a web browser when  
started.\*  
\*But i don't know to open web browser(when rake task start or restart)  
again and again each time i tweaked the code, which is an annoying thing.\*

Robert Klemme \<shortcutter@googlemail.com\>于2016年7月4日周一 下午4:08写道：

> **···**
>
> > On Mon, Jul 4, 2016 at 8:44 AM, timlen tse \<tinglenxan@gmail.com\> wrote:
> > 
> > \> But i still concern about whether it will take up the memory if i load  
> > the  
> > \> same file many times .
> > 
> > That completely depends on what code is in the file.
> > 
> > \> Note that i just modified the methods implement  
> > \> inside the file and will not add new methods. Is ruby smart enough to  
> > just  
> > \> overwrite the old methods and will not take up a lot memory ?
> > 
> > Methods will be overridden. Not sure though whether old versions will  
> > be GCed. You can easily try this out, right?
> > 
> > I do question your approach though to modify methods of a class in the  
> > file while the application is running. What are you trying to achieve  
> > with this?
> > 
> > Kind regards
> > 
> > robert
> > 
> > --  
> > [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> > - without end}  
> > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![A\_Berger](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@A\_Berger](https://rubytalk.org/u/A_Berger)\
**Post date:** [4 July 2016 09:31 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/8 "2016-07-04T09:31:51Z")

</div>

Hi  
Isnt there a gem which informs you about filechanges (not polling)?

Start the program loading your code every second, and look at the output of  
ps | grep ruby (if you're on Unix)

Great to get answers that look beyond the question! :))

Berg

---

<div class="post-metadata">

**Author:** ![A\_Berger](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@A\_Berger](https://rubytalk.org/u/A_Berger)\
**Post date:** [4 July 2016 09:38 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/9 "2016-07-04T09:38:08Z")

</div>

Possibly "load" already checks if the file(timestamp) has changed?  
I remember such thing, but dont find the location again.  
We should look at the source-code...

Berg

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [4 July 2016 10:16 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/10 "2016-07-04T10:16:49Z")

</div>

That would be pointless because the contract of load is to load and  
execute the code whenever load is invoked.

robert

> **···**
>
> On Mon, Jul 4, 2016 at 11:38 AM, A Berger \<aberger7890@gmail.com\> wrote:
> 
> > Possibly "load" already checks if the file(timestamp) has changed?
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [5 July 2016 05:17 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/11 "2016-07-05T05:17:05Z")

</div>

A brief glance into the docs should be enough...

$ ri load

Loads and executes the Ruby program in the file \_filename\_.  
[...unrelated details...]

There is no mention of checking for file changes in the docs.  
And this would be very unlikely due to an accidental omission  
in the docs, since such a drastic difference in behaviour  
certainly should have been noticed during the last 20 years 🙂

Regards,  
Marcus

> **···**
>
> Am 04.07.2016 um 11:38 schrieb A Berger:
> 
> > Possibly "load" already checks if the file(timestamp) has changed?  
> > I remember such thing, but dont find the location again.  
> > We should look at the source-code...
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![Noah\_Gibbs](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/noah_gibbs/32/1839_2.png) [@Noah\_Gibbs](https://rubytalk.org/u/Noah_Gibbs)\
**Post date:** [5 July 2016 17:54 UTC](https://rubytalk.org/t/call-load-method-frequently/73693/12 "2016-07-05T17:54:01Z")

</div>

Yeah, "load" always loads. "Require" may be what you're thinking of, which  
loads conditionally. That's just on the filename, though, not on the  
modification date.

> **···**
>
> On Mon, Jul 4, 2016 at 10:17 PM, \<sto.mar@web.de\> wrote:
> 
> > Am 04.07.2016 um 11:38 schrieb A Berger:  
> > \> Possibly "load" already checks if the file(timestamp) has changed?  
> > \> I remember such thing, but dont find the location again.  
> > \> We should look at the source-code...
> > 
> > A brief glance into the docs should be enough...
> > 
> > $ ri load
> > 
> > Loads and executes the Ruby program in the file \_filename\_.  
> > [...unrelated details...]
> > 
> > There is no mention of checking for file changes in the docs.  
> > And this would be very unlikely due to an accidental omission  
> > in the docs, since such a drastic difference in behaviour  
> > certainly should have been noticed during the last 20 years 🙂
> > 
> > Regards,  
> > Marcus
> > 
> > --  
> > GitHub: [https://github.com/stomar/](https://github.com/stomar/)  
> > PGP: 0x6B3A101A
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
