User Controls

Why do you need to "include" imports in classes in Ruby?

  1. #1
    Sophie Pedophile Tech Support
    I was dabbling in Ruby and something struck me as odd and gay. Consider the following code:


    require 'celluloid'
    require 'socket'

    class ScanPort
    include Celluloid

    def initialize(port, host)
    @port = port
    @host = host
    end


    Why the fuck do i need an `include` statement in my class when i already imported the library with my `require` statement? This seems redundant and retarded to me. In Ruby do we import specific things from libraries with `include` statements like we would do `from datetime import datetime` in Python? This is the only way it makes sense to me.
  2. #2
    Lanny Bird of Courage
    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!
  3. #3
    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.
  4. #4
    Lanny Bird of Courage
    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!
  5. #5
    Sophie Pedophile Tech Support
    Originally posted by Lanny 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.

    Ah i see, thanks Lan. I feel like branching out to other languages, i looked into Go as well, and dabbled a bit. Seems like a dope language. Ruby seems alright as well. But i don't think there is anything in Ruby that Python can't do, so for now i am experimenting a little.
  6. #6
    Lanny Bird of Courage
    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.
Jump to Top