this post was submitted on 24 Aug 2025
281 points (97.6% liked)

Programmer Humor

38003 readers
165 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] HiddenLayer555@lemmy.ml 2 points 4 days ago* (last edited 4 days ago) (3 children)

How is this implemented? Is it just functions and the language assumes the first parameter is autofilled with variable.function syntax?

[–] vga@sopuli.xyz 5 points 4 days ago* (last edited 4 days ago) (2 children)

Ruby is object-oriented, modelled after Smalltalk mostly. So

irb(main):001:0> 10.class
=> Integer

So you'll just have implement the method "years" on the Integer (or something more generic like Numeric) class and then "ago" on whatever class the years method returned.

You might imagine that you can do something like 10.years().ago() in python but the parser prevents you:

>>> 10.years
  File "<python-input-0>", line 1
    10.years
      ^
SyntaxError: invalid decimal literal

Doesn't seem like it would have to prevent it, back in ruby:

irb(main):001:0> 10.0.class
=> Float

Ruby is a pretty cute language in my opinion, and I find it sad that python kinda drove over it.

[–] barubary@infosec.exchange 1 points 4 days ago (1 children)

That's just syntax.

>>> 10 .yearsTraceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: 'int' object has no attribute 'years'
[–] vga@sopuli.xyz 1 points 4 days ago* (last edited 4 days ago)

Yeah, I figured there would be a workaround. Also

>>> (10).years()
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    (10).years()
    ^^^^^^^^^^
AttributeError: 'int' object has no attribute 'years'

But the other thing is also: can you add methods to the int class so that they're available everywhere? I suspect that you cannot in python, at least without significant hackery. And I also suspect that it's probably something they decided to prevent knowingly.