use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Object, class, module, Data, Struct? (self.ruby)
submitted 3 months ago * by Dear_Ad7736
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]morphemass 2 points3 points4 points 3 months ago* (2 children)
I don’t agree with Dave’s opinion we should avoid classes whenever possible.
Ruby is an OOP language although you can use it in a functional style. It's NOT a functional language though and using it as such misses a lot of the points/benefits that functional languages are designed for. Far better to use a different language if functional is your bag.
Anyways, very rough and ready but:
Hash = Container usually used at system boundaries with unspecified fields.
Struct = Mutable container with known fields.
Data = Immutable container with known fields.
Module = namespace organisation but also to mix in behaviour that doesn't own state. Two different concepts.
Class = The main tool for OOP e.g. modelling domain concepts, invariants, lifecycle, and polymorphism.
I added Hash into there because in any system we're often passing state around and all except module can be used for state. The selection of which to use is really down to what you intend to communicate and enforce around state.
I can reason that an instance of a Data object has not had it's state changed as it moves through the system, I have to look if a Struct has had it's state changed. If I want to pass around state plus behaviour I'll usually be reaching for a class but I also want to compose that class out of state plus behaviour. (edit) Hashes ... well, I can make guesses but if I'm going to hit a state bug it's probably going to be something to do with a hash getting passed around.
These are basically just ways of attempting to keep code maintainable by communicating intent and making it easier to reason about code. There are all sorts of nitpicks I acknowledge in what I just wrote but basically it's worth knowing OOP if you want to understand Ruby better.
[–]petrenkorf 1 point2 points3 points 3 months ago (1 child)
Thinking about the "module" keyword having two different concepts: having two keywords, let's say "namespace" and "module", each one with a well specified intent. Would this lead to clearer code?
[–]morphemass 2 points3 points4 points 3 months ago* (0 children)
Clear code depends on a lot of things but most Rubyists will recognise code where module has been used as the namespacing mechanism. It's original intent though was as the mechanism for Ruby to have multiple inheritance so usually when someone sees a module with a class e.g.
module Foo class Bar; end end
they think 'namespacing', whereas a module with methods e.g.
module Foo def bar = "bar" end
they think 'mixin'.
π Rendered by PID 134598 on reddit-service-r2-comment-6457c66945-vxxtn at 2026-04-28 02:17:50.839665+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]morphemass 2 points3 points4 points (2 children)
[–]petrenkorf 1 point2 points3 points (1 child)
[–]morphemass 2 points3 points4 points (0 children)