5 ms·
> 1.day.ago This type of shenanigans always rubbed me the wrong way. Like, are we saying that "1" has a property, "day"? Conceptually that makes no sense, rig
by montroser 5y ago
> 1.day.ago
This type of shenanigans always rubbed me the wrong way. Like, are we saying that "1" has a property, "day"? Conceptually that makes no sense, right? And "day" having a property "ago" is bogus as well.
So then, is it just a cute "let's make it read like a sentence" trick?
One time a colleague was trying to tell me about this trick, and I opened up a ruby interpreter to see what he was talking about. I typed in "1.day.ago" and pressed enter and it gave some error message. A day later he came back and told me how he was surprised to learn that the trick only worked if you had ActiveRecord in the mix.
To me, that's all downside. Sneaky and confusing that loading an ORM adds magic properties to your _numbers_. And, the properties don't even make sense.
The worst part of it all is: how in God's name are you supposed to find out where these magic properties and methods sprinkled all up into everywhere -- where did they come from, so you might read their docs, or read their source? It's madness!
- Spivak 5y agoI think the interpretation makes a lot of sense actually. number.day -> timedelta timedelta.ago -> current time minus delta 2.months.from_now 3.months.after 2.days.ago 1.day + 2.weeks Given the choice between Time.now.advance(years: 2) and 2.years.from_now in a language that uses idioms like 1.step(to: 10) {|x| print x } I think it's totally natural.
- montroser 5y agoSure, the terseness is nice, and it is possible to learn this interface and make sense of it, but to me it still seems like abusing OOP paradigm in a way that conflicts with reality. Abstract of any programming language, the concept of "2" has the properties that it is an integer, that it is a natural number, that it follows "1", that it is prime, etc. But it doesn't have a property "years". From another perspective, back to programming, if "years" and "step" make sense as properties of "2" then does _any noun_ make sense? Is it cool for every library to monkey patch its own domain's worth of nouns to be properties of every number? 2.centimeters, 2.users, 2.widgets? I can get with natural expressions for date math, but how about something more like Time("2 years from now") Then I can read that to mean we are constructing a time, described by the parameter.
- gls2ro 5y ago1.day.ago is idiomatic Ruby code because 1.class.methods will give a long list of methods supported by `1` This is because in Ruby everything* is an object. I agree that from outside Ruby this might look weird, but knowing Ruby this is very simple to understand. Knowing this you can also expect the following things to work in Rails I think: 1.day.ago + 2.hours This is because: + is an instance method defined on the Integer class. See Integer.instance_methods. So it is more like `1.day.ago.+(2.hours)` and I expect then that in Rails (or some gem) they added `hours` as a method to the Integer or a compatible object so in the end this operation is just normal: object.instance_method.instance_method(object.instance_method) nothing magic here happening because this is all valid OOP code where an instance method could return a self object let's say. Knowing this then before writing in a Ruby Interpreter one can find out if that code is in the standard library (see https://ruby-doc.org/core-3.1.0/Integer.html https://ruby-doc.org/core-3.1.0/Integer.html) or in a Gem
- montroser 5y agoI understand how it all works mechanically, I just don't think it models the real world conceptually. OOP works well when you make classes to represent fundamental nouns, those nouns with properties describing their existential makeup. But day.ago is not that -- it's just abusing the syntax to fake a natural looking expression. On finding out where a method came from, I appreciate that I can look to see if it's core or else must be from a gem, but I want to know _which_ gem did it come from.
- gls2ro 5y agoI don't try to convince you to like Ruby, but I don't really understand how you can know in other languages if something comes from a plugin or not by simply looking at it and not knowing the std library or using an IDE. Maybe in the case of a language with a very small standard library, this could be easy: as the best assumption could be that it is in a plugin/module. But I think looking at a piece of code and direct knowing from _which_ plugin some methods comes from has more to do with naming conventions than with the language itself, especially in languages where either overriding or overloading or a form of them is possible. In the case of Ruby, I recommend RubyMine and just to go to definition. 99% of cases it will correctly open the definition from the gem of std library. I agree that in Ruby - maybe more than in other languages - one can really make some behaviors hard to discover. But most of the libraries are straight forward and Rails is very well documented in code. I usually prefer to just go to the definition and read the method than browse the API docs.