11 ms·
Ruby vs. Python comes down to the for loop (2021)
- Rapzid 3y agoI've done a lot of SRE work and two stints at companies with RoR stacks. Really have kinda soured on Ruby and Python at this point and it has little to do with loops. If I had to choose though it would be Python for asyncio.
- sho 3y agoThe omitted context in this comment is that the RoR stack propelled those 2 companies to the level of usage and viability where they needed, and could afford to hire, SRE engineers. I'm not saying RoR doesn't have its faults, I've left it behind too. But I can't deny its productivity, and even in 2024 I would be hard pressed to name something better for a SAAS company starting out.
- SkyPuncher 3y agoI’ve tried so many stacks on side projects and always come back to RoR (with a bit of a grudge). I dislike many things about Ruby, but RoR is extremely hard to beat. Basically, everything you need in a stack is provided or readily available in the community.
- sho 3y agoYeah. My usual advice to anyone who asks is "Use Rails unless you have a very good reason not to". Those reasons do exist, but they're pretty specific and don't apply 90% of the time. Of course if we're being really real, the first piece of advice should actually be "Can this be Wordpress?"
- kayodelycaon 3y ago> Can this be Wordpress? Yup. I’m a huge fan of rails, but I’m an even larger fan of not writing code in the first place. If a spreadsheet can solve your problem, I’m going to suggest a spreadsheet. If a vendor can solve your problem well enough, I’ll suggest a vendor. My time, energy, and attention is valuable. I’m here for the problems you can’t or shouldn’t outsource.
- tasuki 3y agoWhy WordPress? I'll take a simple static website over wordpress anytime. With wordpress: - you are now suddenly also a database administrator - the different people touching the admin interface will break things in subtle ways - welcome an incomprehensible security nightmare into your life Yes, wordpress is extremely easy to set up. Maintain? Nope!
- sho 3y agoIn my experience there is absolutely nothing "simple" about a static website, especially one that is going to be used by other people. If it was my personal blog or something, sure. A website for a non-technical friend with a business who wants to be able to post updates, etc? Omg no. No no no no no. Good managed wordpress starts at like $10-20/month. All those problems you mention go away, you can even do things like rollbacks if someone breaks something. If what you want to do fits into what WP can do pretty well, it's a no-brainer.
- tasuki 3y ago> A website for a non-technical friend with a business who wants to be able to post updates, etc? Omg no. No no no no no. I co-organise an event with a non-technical friend. Previously it was using some sort of a custom cms which was hell. I set it up with Jekyll, created a GitHub account for my friend and taught him markdown (five minutes perhaps). Yes he breaks things occasionally. That's fine! Everything is versioned, so I can easily fix things.
- sho 3y agoI see your definition of non-technical is a little different from mine...
- kqr 3y agoThis is so often the case. "Ugh, with technology X you get such intense growing pains!" ...well, congratulations!
- Rapzid 3y agoOr it was along for the ride. One of those companies was propelled by product market fit, the other by 0% interest and the VC firehose of 2021. When the successful company was founded back in the noughties RoR was arguably miles ahead of everything else. An argument could certainly be made about time-to-market and iteration speed. For the later there were a lot of other viable options available.. > even in 2024 I would be hard pressed to name something better for a SAAS company starting My sense, at least looking at the stacks for tons of recent YC companies, is that most startups are all in on JavaScript these days. Might not actually be better(though I prefer it), but that seems to be the trend.
- VS1999 3y agoWhat issues did you have with python?
- Rapzid 3y agoI just prefer the experience, tooling, and performance of other languages more. Honestly two of my biggest "pains" with it were resolved the last time I was using it professionally via pipenv and asyncio. For use cases outside AI, and perhaps some niche stuff as a bash replacement since its pre-installed on most distros, it's in a sorta no man's land where I believe there are better options and the industry activity has moved on.
- dragonwriter 3y ago> If I had to choose though it would be Python for asyncio. I can think of lots of reasons to prefer Python over Ruby, but asyncio in Python compared to Ruby’s async is not at all one of them.
- wiseowise 3y agoWhat about it?
- dragonwriter 3y ago“Not an advantage” is sort of the default case; Ruby supports async, too (and, unlike Python, doesn't have function coloring.) So, I don't see why asyncio is an advantage. I like Python -- I use it more than Ruby -- and asyncio is fine, just, IMO, not an advantage of Python over Ruby.
- pjmlp 3y agoPython has two killer use cases, being a better Perl that one can read one month later, and a better BASIC for introduction to programming and IoT stuff. Trying to use it for application code, eventually ends up in pain, rewriting code into C and calling it "Python".
- resource_waste 3y agoWas this written in 2008?
- pjmlp 3y agoWritten in 2024, with 38 years of experience reaching out to dynamic languages, some of which without any kind of JIT/AOT in the reference implementation, which Python happens to be one of them. Eventually if the JIT POC from 3.13 evolves to something that can compete with a Common Lisp, or Smalltalk JIT, then I can change of opinion. Note that I only mention those two languages on purpose.
- danielheath 3y ago> Ruby keeps going with its methods-first approach, except instead of each we have a new set of methods commonly implemented on collections, as below Once you implement `each`, `include Enumerable` is all it takes to get the full set of collection methods (including `max`/`min` etc, if the entries define `<=>`).
- vidarh 3y agoIndeed. The entire article largely read like "Python developer learns Ruby", and on top of that now fairly dated Ruby, though I'll make some concessions for him wanting to show a close parallel to the Python. I wish he'd signposted more clearly that these are examples, though, because as it stands it implies this is how to do things, while it really is not. E.g. his "Stuff" example can be reduced to: class Stuff def initialize @a_list = [1, 2, 3, 4] end # The ellipses here are part of the Ruby code for 'forward # all the arguments, including the a block if passed' def each(...) = @a_list.each(...) include Enumerable end Stuff.new.each {|item| puts item } puts Stuff.new.map {|item| item} puts Stuff.new.select{|item| item.even?} One could argue about my use of "..." and endless def, but one certainly would not typically implement each when forwarding to an Array by using a for loop to iterate over it other than to make it relatable to Python developers... And, more controversially perhaps, the last three lines can be reduced to: Stuff.new.each { puts _1 } puts Stuff.new.map.to_a puts Stuff.new.select(&:even?) The first one is one that will cause arguments. The second just showcases that map is pointless here other than as an example - map here effectively just a slow way of duplicating the array, but notably first when forcible evaluated as map without a block will return an Enumerator, and "&:even?" is fairly idiomatic for "call to_proc on this symbol and apply it to the argument", but some might still not be familiar with it.
- re 3y ago> his "Stuff" example can be reduced to [...] The Python example is similarly unidiomatic, reduceable, and flawed for the sake of simplicity (the example doesn't allow multiple independent Stuff iterators), so it's not like Ruby is really at a disadvantage in the article. The point of the example is to illustrate the mechanics of custom iteration (which forwarding to the encapsulated implementation wouldn't accomplish, for either language) while keeping the rest of the Stuff definition as simple as possible. But yes, a brief mention of including Enumerable wouldn't hurt.
- foobarbaz321 3y agoThis is because Ruby inherits it's approach to flow control from Smalltalk, while Python comes from a C/Algol-like heritage. In fact, Smalltalk takes this much further, such that basically all flow control (including if-then-else) is handled as message sends (e.g. if-then is just a message sent to the Boolean object taking a block as it's argument). The downside is the syntax can feel a tad clunky. The upside is incredibly simple and consistent language grammar, while making it trivial to create new flow control mechanisms since the language has all the tools baked in (primarily first class blocks).
- anal_reactor 3y ago[flagged]
- jojobas 3y agoC syntax for say list iteration would be much more cumbersome than "for a in mylist", so there's benefit in diverging from C sometimes. Funnily, regardless of deeply different implementations, for loops look almost the same in Python and Ruby.
- stouset 3y agoWhat exactly do you think the purpose of having different programming languages is? The popularity and usefulness of Ruby’s block-based control flow, which you seem to take issue with, is almost certainly largely responsible for the adoption of lambdas in… basically every modern language, not to mention being backported to existing languages like C# and Java. Frankly your hot take is terrible. C was an amazing language, but there are an infinite number of practical and effective ways to improve upon it. Particularly having just admitted that you’ve never actually used the syntax in question it’s honestly astonishing that your first instinct is to jump straight to posting about how much better the C approach is. Hell, you’d be hard-pressed these days to find a modern language that uses C-style `for` loops. They might say `for` on the tin, but they’re much closer to Ruby-style enumerators than they are to C-style setup-condition-increment control flow. With, of course, the caveat that that’s all they ever can be since they’re keyword syntax rather than just a method. What a sad world this would be if everything interesting in programming was discovered by K&R in the 1970s.
- irjustin 3y ago> So much of how Ruby and Python differ comes down to the for loop. I wish we wouldn't try to simplify the wildly different philosophies of language into a single "thing". From imports, loops, calls, sub-classing, chaining, multi-lines, blocks - there are just so many differences that materially matter. That said, regarding the loop itself, I agree with the article. I come from not-C background and looping in Ruby has always been a pleasure because it feels more natural especially once you include chaining.
- neya 3y agoI think the article leaves out the main difference in Ruby vs Python. Ruby is built for humans, Python is built for machines. This example used to be illustrated on Ruby on Rail's website. https://rubyonrails.org/doctrine https://rubyonrails.org/doctrine They are philosophical vastly different languages. This isn't to say one is better or worse over the other. Just use the right tool for the job. If I was going to do anything with machine learning or data analytics or LLM's, Python is hand's down the right tool for the job. However, If I were to setup a full fledged web application that required a really short time to market with some complex features, I would probably choose Ruby because of Rails.
- adw 3y agoGiven Python has a long history as a teaching language, that strikes me as the kind of grandiose statement without evidence which Perl fans used to be fond of. Even more so given there are approximately no “non-programmers” writing Ruby and enormous numbers writing Python in finance, data science, typography, EDA, and a billion other places; civilians seem to manage with Python just fine. For what it’s worth, language _really_ built for humans probably looks more like Excel or Max/MSP (or both) than like any traditional language - at least those use our embedded spatial reasoning rather than laundering everything through a text serialization.
- dexwiz 3y agoFunny, because I find Python much more readable than Ruby in most cases. The metaprogramming in Ruby has always struck me of bikeshedding novel programming idioms instead of focusing on simplicity. But that’s just my own opinion.
- danmur 3y ago"Ruby is built for humans, Python is built for machines." I'm sorry, that's just ridiculous. Perfectly in line with that load of blather from DHH though.
- wiseowise 3y ago> I think the article leaves out the main difference in Ruby vs Python. Ruby is built for humans, Python is built for machines. This example used to be illustrated on Ruby on Rail's website. > Just use the right tool for the job. Oh, cut the crap. Both statements have nothing to do with reality. If we go by your definition, there’s no reason for Ruby, because Python will always be righter tools for the job.
- dang 3y agoDiscussed at the time: Ruby vs. Python comes down to the for loop - https://news.ycombinator.com/item?id=29199810 https://news.ycombinator.com/item?id=29199810 - Nov 2021 (302 comments)
- nirse 3y agoFunny, I started typing a comment here, about the author not getting the Ruby side of things right, that actually the difference would rather be that Ruby uses a special language construct and Python just normal methods and conventions. But just skimming through the previous comments it seems that Python also has these a `yield` keyword that would be more idiomatic to use. I think we're dealing with someone who had limited experience with both Python and Ruby. This article is somehow getting more attention then it merits.
- taeric 3y agoI still prefer the LOOP macro. Shame it gets such a bad rep.
- graboid 3y agoRuby always seems so appealing. Can anybody tell me how the Ruby ecosystem looks like w.r.t. - (Desktop) GUI - Natural language processing I would really like to learn Ruby, but I can only justify the effort if I can use its ecosystems for some private projects, and I often have been burned by languages not offering too much in those two areas. And speed-wise, as I understand, Ruby is the same ball-park as Python?
- pprotas 3y agoRuby is only relevant because of Rails, and if those two things are your use-cases then you’ll be better off with Python
- stouset 3y agoThere is an enormous Ruby ecosystem completely outside of Rails.
- norman784 3y agoI would argue that the main selling point of using Ruby is Rails, for sure there are a lot of things you do in Ruby, but for sure in 2024 there are more performant alternatives.
- stouset 3y agoPerformance is worth infinitely less than the sheer developer productivity of Ruby, at least when used by experienced engineers capable of restraint.
- submeta 3y agoWhat I like in Ruby: Every expression returns a value. In Python my_list.sort() will return `none`. So if I do `sorted_list = my_list.sort()`, my `sorted_list` will be `none`. And I have been shooting a lot in my foot in the beginning. - I love Python now, but not because I find it aesthetically appealing (I prefer Lisps or functional languages) but because it is ubiquitously available, the ecosystem is phantastic, and it gets the job done. One rant though: Am I the only one being overwhelmed by so many "end" delimiters in Ruby? Feels like visual noise. end end end end...
- oaiey 3y agoend, curly braces, same same. The deviation is IMHO Python with its intention oriented block creation. Which is a debate on its own ;).
- submeta 3y agoI love the indents now with modern editors being able to deal with them ;) Zen mode. Super clean. No end delimiters.
- SwiftyBug 3y agoI personally am more confused without the block delimiters. Having a 4 spaces indent size helps. I wonder if this is purely a matter of getting used to it.
- self 3y agoit's a meme (image), so i can't copy it here: https://twitter.com/stylewarning/status/1772795474589987226 https://twitter.com/stylewarning/status/1772795474589987226
- submeta 3y agoThat's a good one :) So I'm a non-typical hacker, because I use paredit in Emacs so I don't have any issues with parentheses :D
- 3y ago
- IshKebab 3y agoHmm I'm no Python fan but I'd take it over Ruby any day, because it's so much easier to read a codebase. This is based on me trying to follow Gitlab's code. The problems with Ruby seem to be: * No static typing (I think there is Sorbet but apparently it's not very good and Gitlab doesn't use it). * The lack of "syntax" makes it hard to grep for things. For example you can't find where `foo` is called by searching for `foo(` or `.foo` like you can in Python. * It seems to encourage highly dynamic code where even identifiers are dynamically created, so often you'll find an identifier, and try to search for its definition but get zero results. Maybe it's elegant and nice to write, but it's definitely awful to read.
- xtracto 3y ago>* It seems to encourage highly dynamic code where even identifiers are dynamically created, so often you'll find an identifier, and try to search for its definition but get zero results. I've had to take care of 3 large ruby codebases at different companies and this is what kills ruby for me. A lot of ruby programmers think they are being clever when writing crazy dynamic ruby code, but they are only creating technical debt. Years later when they have left and the "context knowledge " is gone from the team, the Ruby code is a huge mess of "magical" code. And rails with its "implicit" functionality depending on method name. A lot of Ruby feels like "magical" to me (in that, things work because of some hidden implicit readmson). I prefer code that is explicit, in your face. You can quickly see what it does and how it does it. Principle of least surprise and "don't make me think". As a language it's pretty, I like to say that Ruby is really object oriented while python is a mix of stuff (why len(x) instead of x.len??)
- kayodelycaon 3y agoAgreed. As a long time Ruby developer, I smack knuckles every time someone dynamically defines methods. If I can’t find def method or def self.method, your code is not passing my review.
- lamontcg 3y agoI'll take dynamically defining methods over method_missing
- nomilk 3y agoFirst impressions apply to languages too. A lot of mathematicians went into AI but didn't start out with any programming experience. They dabbled in some languages and gravitated toward the one(s) they found most useful. Paraphrasing Guy Steele [1]: > It's important that when you design a language that does a familiar thing, that you do the familiar thing exactly... it's criminal that you can write 1/2 and get values nowhere near one-half. Python >>> 1/2 0.5 Ruby irb(main):001> 1/2 0 To a novice programmer, ruby's result makes no sense, and introduces an incredible degree of doubt into the mind of the user. Sure, they could explore why it gives that result, but if that's not helping them get toward solving the problem they're using the programming language for in the first place, then it could seem like an indulgent tangent. [1] https://www.youtube.com/watch?v=agw-wlHGi0E&t=19m27s https://www.youtube.com/watch?v=agw-wlHGi0E&t=19m27s
- wodenokoto 3y agoBack when Python was winning over the AI crowd, they did the same thing as Ruby, if you didn't remember to call some voodoo chants "from future import ..."
- polemic 3y agoNotable that this was python behaviour until Py3.
- lou1306 3y agoThe shift towards // vs / also shows great acumen by the Python designers: they tried (and arguably, largely succeeded) in catering to the greater data science/statistics/numerical analysis communities, where 1/2=0.5 is obvious, rather than sticking to the "compsci-obvious" 1/2=0.
- crabbone 3y agoNeither is obvious in either community... And this isn't the reason Python succeeded. Nor is this the reason for the particular change. The reason Python succeeded with data-science is NumPy and related group of libraries. They happened to be the first to offer easy access to R-like features of other statistically-flavored languages in an all-purpose language. I.e. it makes it easy to combine general-purpose code with statistics-specific code, and once it accumulated critical mass, the process became self-sustaining and alternatives died off quickly. The reason for most of the changes that happened to Python in the last fifteen or so years is design driven by fashion. Which means the majority decides what to do with the language. Which also means that Python is made to look more and more like other mainstream languages (eg. Java, JavaScript, C++...) So, a lot of changes, this one included were made out of subconscious fear of non-conformity.
- cb321 3y agoOr you could just use Nim (https://nim-lang.org/ https://nim-lang.org/) and pass code blocks with templates and/or define iterators for your types - whichever best fits your use-case (with ahead-of-time compilation to native binary executables coming along for the ride).
- dosinga 3y agoFWIW the Python implementation of Stuff seems not all that Pythonic. Returning self from __iter__ and keeping state where the iteration is works, but breaks if you over the same object from an iter. To me that really distracts from the argument since the idiomatic implementation in Python and Ruby look almost exactly the same
- alexcosan 3y agoOn the same note, the implementation of Stuff in Ruby is not very ruby-like. It could have just used `.each` on the `@a_list` (in that case, you just delegate the implementation to `@a_list`, no reason to use a for-loop.
- ajmurmann 3y agoThe whole premise here IMO is quite flawed. In real Ruby code I almost never use a for-loop. I can't think of a single use base for it. You always use .each or one of the other methods like .map or .select. while loops might get used, but I'm not sure I've ever seen a for loop in Ruby on any of the projects I've worked on with the exception of PRs from people who are brand new to Ruby Edit: much more interesting would be the contrast to list comprehensions in Python and the somewhat associated crippled lambdas. Another interesting one is Ruby's private methods being not accessible by other instances of the same class whereas Python's are (to be bizarrely) accessible by instances of the same class (like I can use your heart because we are both human?). I always justified this difference in my mind with Ruby object despite being of the same class potentially having different methods due to metaprogramming at runtime. But it's probably just due to the languages' respective ancestors.
- deleted 3y ago[deleted]
- zbentley 3y ago> Ruby's private methods being not accessible by other instances of the same class whereas Python's are (to be bizarrely) accessible by instances of the same class Not sure what you mean here; Python doesn't really have private methods.
- thangngoc89 3y agoIsn’t the double underscores __name would makes the method private?
- zbentley 3y agoNo, that trick merely renames ("mangles") the method name: https://docs.python.org/3/reference/expressions.html#atom-identifiers https://docs.python.org/3/reference/expressions.html#atom-id... This is very rarely used to try to achieve privacy in practice, not least because it's extremely easy to get around.
- bdcravens 3y agoAs a long-time Rubyist dipping my toes in the Python world, I find decorators to be my favorite feature so far, and a much nicer way of wrapping functionality than blocks.