[HN Gopher] Better Know a Ruby Thing: Singleton Classes
___________________________________________________________________
Better Know a Ruby Thing: Singleton Classes
Author : mooreds
Score : 102 points
Date : 2025-01-27 20:18 UTC (4 days ago)
(HTM) web link (noelrappin.com)
(TXT) w3m dump (noelrappin.com)
| xutopia wrote:
| I love it when someone dives a bit deeper behind something pretty
| common for most Ruby devs. It's actually a really elegant pattern
| that is used and it's everywhere in Ruby.
| empw wrote:
| I haven't written any ruby for over a decade and a half, when
| ruby 2.x was still on the horizon. But I do know the source of
| "eigenclass". It was first jokingly used in an odd instructional
| programming book / web comic / experimental art piece by "why the
| lucky stiff" who was briefly prominent in ruby-land then erased
| himself from the internet. It's funny that it has now become an
| established term of art for Ruby people.
| nixpulvis wrote:
| I remember reading WPGTR in college. Really inspired me to play
| around and learn the language. I've always kept my printed copy
| in a safe place.
| adriand wrote:
| I read it after college when I first started working with
| Rails 1 after I joined a friend's company. Funny to think
| that was almost two decades ago. I still remember the quirky
| art style, although I will admit to preferring a more prosaic
| book when I learned the language. But it was a fun community
| and the sense of creativity was really apparent.
| KerrAvon wrote:
| _why's influence is indelible.
| insane_dreamer wrote:
| loved _why's stuff back when he was still around.
| lcnPylGDnU4H9OF wrote:
| > There are at least three ways to define a method in a Ruby
| singleton class.
|
| It's worth noting that two of such ways will, perhaps
| unintuitively, ignore the `private` keyword.
| private def self.foo # ... end def
| x.foo # ... end
|
| That will define a public method `:foo` on each of `self`'s and
| `x`'s singleton classes. If you want the method to be private,
| either 1) explicitly make the method private using
| `Class#private` or `Class#private_class_method` or 2) use the
| `class << self` (or `class << x`) syntax in order for the
| `private` keyword to work as expected.
| private_class_method def self.foo # ... end
| def self.foo # ... end private_class_method
| :foo # `def` will return a symbol of the defined method name so
| the above syntax is usually sufficient. def x.foo
| # ... end x.singleton_class.send :private, :foo #
| Class#private is a private method(!) so must be called using
| Object#send. class << x private def foo
| # ... end end
|
| > The eternal question... Why is Ruby like this?
|
| Joy! :) I'm pretty sure matz would agree.
| dragonwriter wrote:
| > It's worth noting that two of such ways will, perhaps
| unintuitively, ignore the `private` keyword.
|
| No, this explanation is wrong, because _there isn 't actually a
| `private` keyword_, only the Module#private method [0] which,
| when called with no arguments, changes the visibility of future
| methods defined in the target module.
|
| Those examples call the `Module#private` method with no
| arguments in one context changing the visibility of future
| methods defined in that context, and then use an explicit
| prefix to _define methods in a different context_.
|
| When you don't forget that `private` is a private instance
| method on Module and _not_ a keyword, the behavior is much
| simpler to understand.
|
| [0] https://ruby-doc.org/3.4.1/Module.html#method-i-private
| corytheboyd wrote:
| Went in expecting a basic how-to about the Singleton module, but
| this is so much more, you clearly know your shit, and I love deep
| dives like this!
| Alifatisk wrote:
| I have to admit, years back when I learned Ruby I found this
| syntax to be very bizzare class User
| class << self def create_from_data end
| end end
|
| Specifically, this: class << self
|
| But when reading about it, it made sense
| Lammy wrote:
| > What we've been calling "class methods" are actually instance
| methods of the metaclass.
|
| This also shows up if you ever want to use a Refinement on a
| "class method". The thing you must `refine` is the
| `singleton_class`.
|
| Here's a live example from my UUID library where I refine
| `Time::now` and one of my own utility class' methods to test the
| time-went-backwards and network-card-changed cases for
| incrementing the sequence value while generating time-based
| UUIDs:
| https://github.com/okeeblow/DistorteD/blob/3e9bbc744479afd3e...
| mrinterweb wrote:
| Don't forget about the Singleton module ("include Singleton").
| This one trips me up in terms of what is being referred to as a
| "singleton". This is more of an singleton class instance where
| there can only be only one class instance per-class.
| https://ruby-doc.org/3.4.1/stdlibs/singleton/Singleton.html
| rubyfan wrote:
| Great read, I've written several ruby extensions in c and this
| write up made total sense.
| andrekandre wrote:
| > In Smalltal, you interact with your code through an code
| browser that is part of the system.
|
| small spelling mistake: Smalltal - Smalltalk
___________________________________________________________________
(page generated 2025-02-01 08:00 UTC)