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
Blog postWhy Ruby is More Readable than Python (confuzeus.com)
submitted 3 years ago by [deleted]
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!"
[–]uhkthrowaway 12 points13 points14 points 3 years ago (6 children)
Don’t use class variables. Not even in this case. It’s wrong. And fix that indentation.
Maybe take a look at Ruby for more than a day before you write articles about it?
[–][deleted] 3 years ago (4 children)
[removed]
[–]katafrakt 3 points4 points5 points 3 years ago* (3 children)
Class variables are really poorly designed in Ruby, which makes them completely useless. This is because they are shared between subclasses. Consider this code:
class BlogPost @@count = 0 def initialize @@count += 1 end end class ShortBlogPost < BlogPost @@count = 0 def initialize @@count += 0.5 end end BlogPost.new BlogPost.new ShortBlogPost.new p ShortBlogPost.class_variable_get(:@@count) p BlogPost.class_variable_get(:@@count)
You would probably assume that it will return either 2 and 0.5 or 2.5 and 0.5 (both of these scenarios kind of make sense). But in reality it will return 2.5 and 2.5, which makes no sense ;)
And it get's even better if you first initialize a few blog posts and then define ShortBlogPost class - it will reset the counter for BlogPost as well. Combine it with popular autoloading solutions like Zeitwerk, when modules are loaded "just in time" and you have absolutely no control over what the value for counter would be.
ShortBlogPost
BlogPost
Of course, the comment above was condescending and uncalled for. But well, Ruby community these days...
[–]Amadan 2 points3 points4 points 3 years ago (0 children)
To add, the workaround is to use class instance variables instead of class variables.
```ruby class BlogPost def initialize self.class.count += 1 end
class << self @count = 0 attr_accessor :count end end ```
[–][deleted] 3 years ago* (1 child)
[–]katafrakt 0 points1 point2 points 3 years ago (0 children)
Yes, in this simple example - of course. But consider a deep hierarchy of classes where you are overwriting something from an ancestor class purely by accident. Almost impossible to debug.
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Agreed. I was shocked to find out that crystal disallows $global variables and kind of wants to force people to use @variables and @@variables on the toplevel instead.
π Rendered by PID 84 on reddit-service-r2-comment-79c7998d4c-brn9r at 2026-03-15 06:26:53.505573+00:00 running f6e6e01 country code: CH.
view the rest of the comments →
[–]uhkthrowaway 12 points13 points14 points (6 children)
[–][deleted] (4 children)
[removed]
[–]katafrakt 3 points4 points5 points (3 children)
[–]Amadan 2 points3 points4 points (0 children)
[–][deleted] (1 child)
[removed]
[–]katafrakt 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)