4 ms·
I used Python for a really long time and switched to Ruby several months ago. Benefits of Ruby are: -no distinction between expression and statement (everythin
by thehotelambush 15y ago
I used Python for a really long time and switched to Ruby several months ago. Benefits of Ruby are:
-no distinction between expression and statement (everything has a value)
-lambdas can thus contain any expression
-much bigger range of built-in methods and classes (all permutations of a list, removing duplicates from a list, Rational class, Regexp class, Range class, etc.)
-more flexible syntax and "control operators" like each_with_index
-almost zero distinction between built-in classes and methods and user-written ones: you can open up Hash (dict) or Array (list) and extend it straightforwardly.
Downsides are:
-generally uglier and more ambiguous syntax
-distinction between functions and methods (b/c "all you can do is send a message") leads to a lot of unnecessary duplication of concepts
-so-called "blocks" which are really just a hack to avoid writing the word "lambda" a bunch of times (maybe if the more concise Ruby 1.9 syntax for lambdas had existed earlier blocks wouldn't have been implemented). I think they are based on the idea that most HOFs only require one functional argument, and they encourage functional programming, but they (along with the last bullet point) discourage the general case. They provide some of the functionality of macros.
-you can't define a class inside a method (still haven't quite figured out the reasoning behind this one).
In general Ruby is definitely more powerful than Python, but in its higher ambitions suffers more obviously from clashes between functional and OO programming. Python's dogmatic insistence on "readability" makes it feel like an old grandma in comparison to Ruby.
- codesuela 15y ago> -lambdas can thus contain any expression why would you want to use lambdas if you have generator expressions? eg: map(lambda c: c.strip(), countries.split(',')) vs [c.strip() for c in countries.split(',')] IMHO the latter one is way more readable and generators cover almost all the use cases for lambdas. If you could not do it with a generator expression and choose to use a anonymous function it should have been a method anyway. > -much bigger range of built-in methods and classes (all permutations of a list, removing duplicates from a list, Rational class, Regexp class, Range class, etc.) you mean sets, dictionaries(hashtables) etc.? those are built in as well in Python. Remove duplicates: mylist = list(set(mylist)) Range class: [i for i in range(10)] you can import the rational class via a simple from fractions import Fraction same for regex > -more flexible syntax and "control operators" like each_with_index you can iterate over dictionaries with for k,v in dict.items() if you need the index of a list you can use for i,v in enumerate(list) personally I don't think there is much of a difference between Python and Ruby. One is more mature the other is way hipper and it comes down personal taste which one you choose.
- haberman 15y ago> why would you want to use lambdas if you have generator expressions? Lambdas (blocks) in Ruby are used for way, way more than just building or manipulating lists. For example, there is no need for the "with" keyword in Ruby, because you can just use blocks for that. As another example, you can specify a block to the Hash constructor to specify code that runs when a key is accessed that is not present. > Range class: [i for i in range(10)] That is not a range class, that is an expression for constructing a contiguous list of numbers. The Range class in Ruby is an actual class that you can instantiate and call methods on. ("a".."z").cover?("c") #=> true ("a".."z").cover?("5") #=> false > personally I don't think there is much of a difference between Python and Ruby. One is more mature the other is way hipper and it comes down personal taste which one you choose. They have similar capabilities but make very different choices in a lot of ways.
- thehotelambush 15y agoThanks for mentioning sets - this is another thing that I would like to be built-in in Ruby. But "list(set(mylist))" seems like kind of a hack. > you can import the rational class via a simple from fractions import Fraction Sure, you can write a library for anything and by now Python has libraries for most everything. But it still makes a difference to have things in the core language, plus a nice syntax for them. > why would you want to use lambdas if you have generator expressions? This is a case of making up for odd semantic choices with special syntax, which then becomes an unnecessarily fixed-in-stone part of the language. Range objects in Ruby are actually like slice objects in Python. But Ruby separates the syntax for ranges from the get-item method, since there are plenty of other places where you would want to use ranges (then you can iterate and map over them, etc.). Ruby uses blocks to implement things like an each method that fulfills the same function as Python's for statement. (Ruby has a for statement but it is hardly ever used.) In Ruby your example would be countries.split(',').map &:strip which is obviously more concise and no less readable. Compare with countries.split(',').each &:foo where foo is some side-effectful method. IMO such a small change in behavior should result in a similarly small change in how the program is written.
- nathan_f77 15y ago
- candl 15y agoI did a switch in a reverse direction. From Ruby to Python. People actually shouldn't compare both languages in terms of language constructs and what or what not they got right. They are similar enough that it doesn't matter. What matters is library support and the community. I got into Ruby by pure accident (No Rails-bait). I needed a scripting language to automate some tasks and one of my friends was playing with it back then and suggested it. Ruby's weakness (and it's strength at the same time) is that it got popularized by Rails. As a result a huge community has grown around it - but it brought a tunnel vision effect along with it. People don't recognize that there are things besides Rails and it's reflected in weak (non web-dev) library support. I didn't like the fact that there's a big push in Ruby-land for metaprogramming / DSLs and monkey patching. Ruby's not inherently that great at it. It just happens that you can do it, but it doesn't mean you should. Which has always been in my eyes mostly a big abuse of *_eval, method_missing etc. that leads to performance issues, maintainability issues, and so on. All that just to save a few keystrokes. Couple that with the very implicit nature of Ruby then you get a disaster. How often did I want to use libraries that turned out to be monkey-patching the same built-in classes and clashing with each other, How often did I want to delve to some code base to customize something, make a fix, etc. just to find such coding practices and being unable to reason about it in a regular object oriented fashion which Ruby's foundations are built on. The more I wanted to build serious things in Ruby the more often I would hit such problems. This all brings a question: What would happen if Rails had disappeared the next day. What would all these people do? What could they use instead? It was an obvious choice not to stick with Ruby because it has nothing to offer in the long term over Python. Learning python is a very good investment - I can do web programming, gui programming, scientific computing. I can use it to script blender, gimp, you name it. And unlike Ruby - I have excellent alternatives for each task.