Never worked on Ruby, so I definitely cannot judge it, but that syntax looks so uncomfortable…
And the best part is the Ruby way accounts for leap years.
Well,
365 * 10
certainly doesn’t ;-)I prefer the one on the left because it’s evident it doesn’t account for leap days, while I’d be questioning whether the one on the right does.
I’ll give it a shot. Looks a bit kludgy and I’ve been typing this on my phone while sitting on the toilet. What am I doing with my life?
from datetime import datetime now = datetime.now() year = now.strftime('%Y') month = now.strftime('%m') day = now.strftime('%d') tenyearsago = datetime(year-10, month, day) print(tenyearsago.strftime('%d.%m.%Y')
or just this
from datetime import datetime today = datetime.today() ten_years_ago = today.replace(year=today.year - 10) print("Date 10 years ago:", ten_years_ago.date())
Looks like one is defined as years and one as days. 10 years does not necessarily equal 365 times 10.
In fact, it would never equal 365 * 10 days.
from datetime import datetime from dateutil.relativedelta import relativedelta print(datetime.now() + relativedelta(years=10)) # 2035-08-24 12:02:49.795177
Didn’t know about
relativedelta
. Thanks!
Ruby is awesome. Finding out that everything is an object, and because of that you can do things like in your example (10.whatever), is hilarious coming from other languages.
By the way, it isn’t actually necessary for everything to be an object to make this work. The language just has to define
10.whatever()
to be syntactic sugar forwhatever(10)
. Some languages, like Python and Rust, have an explicitself
parameter on their methods to help illustrate this.But yeah, a bunch of languages decided to special-case their objects instead, for whatever reason.
That’s not quite right, the language has defined Int#days and 10 is actually Int(10). 10.days calls the instance method days on an instance of an Int (it has been years since I’ve used ruby so not sure if the stdlib class is actually Int)
Ah, I’m not talking about Ruby, I’m talking about language design in general.
I’m currently mostly doing Rust, so I can only really name that as an example (even though there’s very likely other languages that allow this, too), but yeah, here’s for example the 64-bit signed integer in Rust: https://doc.rust-lang.org/stable/std/primitive.i64.html
That is a primitive type, not an object, so it is exactly 64-bits in size and stored on the stack, not the heap. But as you can see in the documentation, Rust allows for associated functions anyways, like for example:
let my_number: i64 = -3; my_number.abs() //returns the absolute value, so 3
That’s because that last call is just syntactic sugar for calling the same function statically on the type:
i64::abs(my_number)
Does that work? Might be enough to convert me
I was on a project a while back that used Ruby, and what I concluded was that cute things like that look good at first glance if you’re skim-reading some already-correct code, but are pretty much a net wash in terms of writing or debugging code.
It’s not unusual for people to think that code would be better if it scanned like regular English, but the problem is that English is woefully imprecise and doesn’t always correlate to what kind of operations get run by the code, and so you still end up having to learn all that syntax and mentally process it like any other programming language anyway, but now you’ve also got a bunch of false friends tricking you into thinking they do one thing but actually they do another.
(also, the bulk of the text in that python example is the import statement, which is like… ok so what, it’s not like Ruby doesn’t have its own dependency hell problems)
I had to modify some ruby a few years ago, I don’t remember liking it! Once I understood the syntax it wasn’t terrible to work with but I still wasn’t a fan of the syntax
it works in Ruby on Rails but not in bare-naked Ruby, if that gives you a hint of how the language’s architecture makes things easy for you and also might stab you in the back one day.
How is this implemented? Is it just functions and the language assumes the first parameter is autofilled with variable.function syntax?
“365*10”???
This is like a 10yo meme template, fellow kids pls update your meme stashes!
Meh. If it works it works. Keep the golden oldies in circulation I say!
🟥🟥🔵👷I guess were doing memes now.