8 ms·
There are thousands of little things that make Ruby nice. They all add up to a more enjoyable experience. I'll give you one tiny example: Python: from dat
by shaicoleman 4y ago
There are thousands of little things that make Ruby nice. They all add up to a more enjoyable experience. I'll give you one tiny example:
Python:
from datetime import date
if date.today().weekday() == 5:
print("It's Saturday")
Ruby:
require 'date'
puts "It's Saturday" if Date.today.saturday?
- ouija 4y agoI prefer the Python code here. The import is explicit, so if you want to know where "date" comes from, that's easy. In a modern IDE I don't write imports manually anyway. You could write it as a one-liner in Python as well, but I would prefer not to. print("It's Saturday") if date.today().weekday() == 5 else None That the Python library doesn't define constants for days is unfortunate, but not a limitation of the language.
- petepete 4y agoWhy do I need to remember that Saturday is 5?
- greenpeas 4y agoS looks like 5 if you squint. not hard to remember
- d0mine 4y ago>>> import calendar >>> calendar.SATURDAY 5
- petepete 4y agoYeah, I'll take `Date#saturday?` thanks.
- quickthrower2 4y agoYou are allowed to define your own constants.
- squeaky-clean 4y ago¿Por qué necesito recordar que el sábado es "Saturday"?
- throwawaymaths 4y agoTampoco "if", "for", "while"...
- unixhero 4y agoPorque en ese caso no usas un IDE moderno.
- wiseowise 4y agoRest of the language doesn't bother you?
- shaky-carrousel 4y agoThe ruby oneliner looks way cleaner and more natural than the python one.
- rcfox 4y agoWould you ever use either in a real codebase though?
- xipho 4y agoAs opposed to a fake codebase? This sounds a bit like https://en.wikipedia.org/wiki/No_true_Scotsman https://en.wikipedia.org/wiki/No_true_Scotsman.
- orf 4y agoAs opposed to either a snippet in a REPL or a comment on hacker news.
- xipho 4y agoThe code is part of a standard, used-almost-everywhere library, `date`. Code in these libraries, from what I undersand, have a ton of thought behind them. It might follow that there were good real-life use-cases ready to use the code that influenced its presence in that library? Also: https://github.com/search?l=Ruby&q=Date.today.saturday%3F&type=Code https://github.com/search?l=Ruby&q=Date.today.saturday%3F&ty...
- orf 4y agoSo the answer to the original question was simply “yes”.
- mod 4y agoNo, not as opposed to a fake codebase. I think it's clear what they meant. One incredibly common alternative to a codebase is a script.
- by_the_bay 4y agoAgreed, in complicated Ruby packages with layers and layers of dependencies it quickly becomes a nightmare to identify which class comes from which import, where it’s defined, etc. Never an issue in python
- cutler 4y agoThe else None is redundant, though. If you want a one-liner in Python just ignore the indentation: if date.today().weekday() == 5: print("It's Saturday")
- dragonwriter 4y agoPreferring the Python import syntax doesn't preclude preferring the Ruby library design.
- BiteCode_dev 4y agoI can't fathom optional parenthesis. In this code it makes it very hard to know if today is an attribute or a method without knowing the context, meaning I can't scan the code, I have to read it carefully.
- nickjj 4y ago> I can't fathom optional parenthesis. I think this kind of falls in line with the idea of Ruby giving you sharp knives[0]. Like you I used to have a love / hate relationship with this because it feels inconsistent to sometimes use parenthesis and sometimes not. In a world where you just want to run Python's Black code formatter and always use parenthesis, having to think about when to use parenthesis feels really tedious. But I think nowadays I'm happy that they are optional because when you want them to be optional it creates a really nice aesthetic to your code. For example with Rails you can do: `2.days.ago` and then there's also things that feel like a mini DSL such as `validates :title, :body` or `belongs_to :users`. These just look nicer without parenthesis. With that said, I do use parenthesis more often than not but it's nice to avoid using them when it makes something look nicer visually. [0]: https://rubyonrails.org/doctrine#provide-sharp-knives https://rubyonrails.org/doctrine#provide-sharp-knives
- jenrzzz 4y agoIt's always a method. Ruby doesn't expose instance variables unless you define accessor methods for them.
- fny 4y agoI'm going to emphasize this as well. Everything is an object, and everything called on an object is a method. Full stop. Unlike with other languages, there is no ambiguity here. In python, you don't know when you dir() an object whether your dealing with a property or method. In Ruby, you are guaranteed it's always a method. A perk is that you don't need parens.
- ek750 4y agoExactly. Once you get some basics of Ruby, there is no ambiguity, and I find it quite elegant. And I think I’m in the minority here, but I prefer the cleanliness of optional punctuation. That feature also allows for fluent DSLs (which seem to be falling out of favor for various reasons), but is one of the reasons I still love Ruby. To look at the other side, I hate having to constantly add semicolons and parens in other languages. Makes my poor fingers hurt even more than they usually do.
- politelemon 4y agoIf I only used Ruby I'd probably prefer the Ruby example. As someone who jumps across many languages, I prefer the first. I found Ruby's approach to things was at many times too clever, to the point of becoming unreadable and cryptic. Python prefers clearness over cleverness.
- heartbreak 4y agoHow is computing a weekday where the caller (and reader) must know that Monday=0 not cryptic?
- throwawaymaths 4y agoI think the if being an infix operator is a bit "cryptic". I mean IMO it is easier to read - and in reality it's not a problem, because it's pretty unambiguous what's going on, but some people get annoyed by "oh you can do that in ruby?" Trigger that happens the first few times you see it. Probably same with fields/methods with a postfix ?
- dragonwriter 4y ago> Probably same with fields/methods with a postfix ? It's not a “postfix ?”, it's literally just “?” being part of the identifier name. (It's convention for methods that are boolean predicates.) Also, Ruby doesn't have fields. (It has instance variables, but they don't have dotted access, so they aren't in the same namespace as methods of the same object the way fields are.)
- throwawaymaths 4y agoIt's a postfix sigil, even if it's part of the identifier name. You're not allowed to put ? in the middle of an identifier. (I use ruby and elixir, so this is second nature to me. It might not be for others). I'm aware ruby doesn't have fields, I called it fields/methods because it's a method in ruby but people in other languages will read it as a field, which it is, a good percentage of the time.
- AnonymousPlanet 4y agoHow do you know which 'Date' your are using. How do you know it's the one from 'date'? That's like using * imports in Python which is a huge pain once you done even minor refactoring.
- gedy 4y agoYeah in the Rails shops I've worked with, this "developer productivity" turns into a nightmare with large codebases.
- shaicoleman 4y ago> How do you know which 'Date' your are using. How do you know it's the one from 'date'? Ruby allows you to check where a method is defined, e.g.: puts Date.singleton_method(:today).source_location Although in this case it returns nil because it's implemented in C. But it would return the location if you overrode it. There are various tools to see what the code is doing: invoking a debugger and stepping into the code, tracing the code execution via TracePoint, etc. The Zeitwerk autoloader which comes with Rails enforces that the class name matches the filename. Naming conventions, specs, a quick grep and avoiding generic names make it almost a non-issue in my experience - only happens every few years or so, and usually quite easy to fix. Ruby gives you a lot of power, but it doesn't stop you from shooting yourself in the foot. https://m.signalvnoise.com/provide-sharp-knives/ https://m.signalvnoise.com/provide-sharp-knives/
- tzs 4y agoI think every language I've used has annoyed me in how it handles dates and times. I've been tempted several times to sit down and repeatedly read Dershowitz and Reingold's book "Calendrical Calculations" [1] cover to cover until I thoroughly understand it, and then write my own date and time library and port it to every language I use. That would mean that when I learn a new language I'd have to take some time to port my date and time library, but looking at the languages I already know that would have been less than the time it took me to find and learn to deal with the quirks and annoyances in their libraries. I see no reason to believe that this will be different for any future languages I decide to learn. Yes, I know this would be annoying to whoever has to take over my code when I'm gone, but I tend to stick around on projects until they reach end of life and so there almost never is anyone that comes after me. [1] https://www.amazon.com/Calendrical-Calculations-Ultimate-Edward-Reingold-dp-1107683165/dp/1107683165/ https://www.amazon.com/Calendrical-Calculations-Ultimate-Edw...
- scubbo 4y agoI cordially invite you to look into the clusterfuck that is Go's treatment of date formatting. Everything else will seem clean and logical by comparison. Per https://golang.cafe/blog/golang-time-format-example.html https://golang.cafe/blog/golang-time-format-example.html: > Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time, defined to be Mon Jan 2 15:04:05 -0700 MST 2006 would be displayed if it were the value; it serves as an example of the desired output. The same display rules will then be applied to the time value. So, for instance, if you want to format a date in ISO format (YYYT-MM-DDTHH:mm:ss), you have to figure out how the reference date would be displayed in that format ("2006-01-02T15:04:05"), and pass that as the format string.
- DangitBobby 4y agoI agree, of all the date formatting I've encountered, Go's is the worst.
- dragonwriter 4y agoIf I didn't know that was actually an accurate description of Go date formatting, I’d think it was satirical.