Call `load` method frequently

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
    if files modified
         load 'file.rb'
    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!

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.

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
BSD: kqueue - Wikipedia
Mac OS X: FSEvents - Wikipedia

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 :slight_smile:

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!

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

···

Subject: Re: call `load` method frequently
  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

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

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写道:

···

        Subject: Re: call `load` method frequently
        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

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * 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;

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/

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/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

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

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

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/

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 :slight_smile:

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
PGP: 0x6B3A101A

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 :slight_smile:

Regards,
Marcus

--
GitHub: 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;