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.
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.
> 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:
--
* 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 ?
> > 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:
--
* 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)
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?
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.*
> 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?
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...
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...
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