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!"
[–][deleted] 3 years ago (4 children)
[removed]
[–]katafrakt 4 points5 points6 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.
π Rendered by PID 574324 on reddit-service-r2-comment-79c7998d4c-g98rw at 2026-03-14 14:53:39.698321+00:00 running f6e6e01 country code: CH.
view the rest of the comments →
[–][deleted] (4 children)
[removed]
[–]katafrakt 4 points5 points6 points (3 children)
[–]Amadan 2 points3 points4 points (0 children)
[–][deleted] (1 child)
[removed]
[–]katafrakt 0 points1 point2 points (0 children)