News for hackers. Stuff that matters.
Re:Why Ruby Is More Readable Than Python by jez on Tuesday July 19, 2022 @08:48PM (#32159919)
There's more than one way to write the corresponding Ruby snippet. If I had been writing it, I might have translated it something like this:

    class BlogPost
      class << self
        attr_accessor :count
      end
      self.count = 0

      attr_accessor :title, :body

      def initialize(title, body)
        @title = title
        @body = body
        @published = false
        BlogPost.count += 1
      end

      def publish!
        @published = true
      end

      def to_s = @title
      def count = self.class.count
    end
This avoids needing to understand what `@@count` means, and lets you use the same syntax as in python: `BlogPost.count += 1`.

Or, if I were in a codebase using static type annotations with Sorbet, I'd do something like this[0]. To each their own, write code in the language and style that makes the most sense to you.

[0] https://sorbet.run/#%23%20typed%3A%20true%0Aclass%20Module%3...

Re:Why Ruby Is More Readable Than Python by gmalette on Tuesday July 19, 2022 @08:53PM (#32159946)
That’s without even mentioning the footguns available in @@count

Jez!

Re:Why Ruby Is More Readable Than Python by jez on Tuesday July 19, 2022 @09:07PM (#32160043)
Ah sure, this alternate implementation assumes that the BlogPost class is final and never subclassed.

If you plan to subclass BlogPost, the translation is not identical, as the alternate implementation would not keep track of a global count of posts across all subclasses, only of `BlogPost` instances exactly.

Re:Why Ruby Is More Readable Than Python by gmalette on Tuesday July 19, 2022 @10:20PM (#32160586)
I saw you closed my ticket on sorbet for generics + sealed + exhaustiveness, that’ll allow some very welcome code cleanup!
Re:Why Ruby Is More Readable Than Python by gmalette on Tuesday July 19, 2022 @10:17PM (#32160574)
Depends who accessed it first ;)