2017-02-01 at 9:29 AM UTC
Require is your mechanism for loading another file, like import in Python. Include is a mechanism for incorporating one class's properties into another. So like the analogous code in python would be:
import celluloid
import socket
class ScanPort(celluloid.Celluloid):
def initialize(self, host, post):
self.port = port
self.host = host
In this case include is indicating ScanPort should inherit methods from Celluloid. It's possible to require a module without making a class to extend it, like for example somewhere down the like you probably create a socket somewhere without `include`ing it.
The following users say it would be alright if the author of this
post didn't die in a fire!
2017-02-01 at 9:31 AM UTC
Is importing a libray and its inclusion the same thing? I dont know much about comp sci but I have dabbled with enough languages to get that some nuance is required.
2017-02-01 at 9:52 AM UTC
In spirit `require` in Ruby is a way of saying "I want some other thing in some other file to be available to me". You could say the file being required is "included" in the dependency graph of the code requiring it.
In interpreted languages "make something available to me" usually means "execute this file" although there is nuance about execution order, how multiple-"requirement" works, and namespacing. In some languages (C and I think COBOL come to mind) "make something available to me" is much closer to lexical inclusion. People will argue about which is "simpler", in some sense lexical inclusion is very simple to implement but execution is more elegant. Most everyone agrees namespaces are a good thing. As in most things Java splits the difference by having namespaces (although of a pretty infuriatingly ridget variety) with a mechanism for execute-on-load but it's pretty rarely used and there are a lot of restrictions on what you can do. Perhaps a good way to conceptualize the different approaches is to look at header guards in C, what they do, and why they're not a thing in dynamic languages.
In Ruby the `include` keyword is the primary mechanism for class inheritance. Think "extend" in Java. There is no direct analog to inheritance in C, it wasn't a popular concept when C was designed, whether or not it's a useful tool has become controversial again in recent years although orthodoxy remains firmly positive.
The following users say it would be alright if the author of this
post didn't die in a fire!
2017-02-01 at 6:41 PM UTC
Yeah, in a lot of ways python and ruby are the same language. The syntax is pretty different but when you get down to it they're deeply similar.