Discussion:
"Circular require considered harmful"
Leam Hall
2018-09-21 08:48:13 UTC
Permalink
Trying to figure out how to resolve this error:

/usr/local/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:59:
warning:
/usr/local/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:59:
warning: loading in progress, circular require considered harmful

This is the file it is referencing:

https://github.com/makhidkarun/ftl_chargen/blob/master/lib/ftl_chargen.rb

How do I resolve the errors? Should only some files require the
ftl_chargen.rb file for loading?

Thanks!

Leam

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Andy Jones
2018-09-21 08:57:40 UTC
Permalink
Under normal circumstances it's fine to require the same file twice, because require keeps a record of the libraries it has loaded and ignores a second call.

It looks as if you get this error if the library you are requiring requires itself somehow, which is not something that require can cope with -- the require operation presumably needs to complete before the library is added to the list.

So if in a.rb you require b.rb and in b.rb you require a.rb. then when you write `require "a"` Ruby will

* start to require a.rb
* start to require b.rb
* start to require a.rb again and realise there is something terribly wrong.




Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Leam Hall
2018-09-21 09:22:33 UTC
Permalink
Hey Andy, thanks!

I'm reading Ousterhout's "A Philosophy of Software Design" and seeing
the need to reduce the complexity of my program. Looks like this is
another area to work on!

Leam
Post by Andy Jones
Under normal circumstances it's fine to require the same file twice, because require keeps a record of the libraries it has loaded and ignores a second call.
It looks as if you get this error if the library you are requiring requires itself somehow, which is not something that require can cope with -- the require operation presumably needs to complete before the library is added to the list.
So if in a.rb you require b.rb and in b.rb you require a.rb. then when you write `require "a"` Ruby will
* start to require a.rb
* start to require b.rb
* start to require a.rb again and realise there is something terribly wrong.
Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>
Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Dmitriy Non
2018-09-21 09:24:22 UTC
Permalink
Hello there.

Andy Jones is right. This happens when two files require each other,
which does not make any sense really.

This is pretty common mistake (I think) and is a signal that your code is too
coupled.
/usr/local/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:59: warning: /usr/local/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:59: warning: loading in progress, circular require considered harmful
https://github.com/makhidkarun/ftl_chargen/blob/master/lib/ftl_chargen.rb
How do I resolve the errors? Should only some files require the ftl_chargen.rb file for loading?
Thanks!
Leam
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Andy Jones
2018-09-21 10:04:03 UTC
Permalink
This is pretty common mistake (I think) and is a signal that your code is too
coupled.
<<<<<<<<

This. In my case the thing that got me thinking clearly about dependency and coupling was POODR ("Practical Object Oriented Design in Ruby", by Sandi Metz).

Short version: class B shouldn't really care whether that object passed as a method parameter is a class A object or not. It should only care that whatever the object is, it can behave in the right way...


Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Leam Hall
2018-09-21 11:33:24 UTC
Permalink
Post by Dmitriy Non
This is pretty common mistake (I think) and is a signal that your code is too
coupled.
<
This. In my case the thing that got me thinking clearly about dependency and coupling was POODR ("Practical Object Oriented Design in Ruby", by Sandi Metz).
Short version: class B shouldn't really care whether that object passed as a method parameter is a class A object or not. It should only care that whatever the object is, it can behave in the right way...
Agreed. The issue came from moving everything into a module before I
really understood the full level of modularity needed. I've read POODR a
couple times but didn't "get" it; now that I have a need perhaps another
re-read is in order.

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

Continue reading on narkive:
Loading...