Question from someone who invested much time in Python. What benefits Ruby has to convince to move? Instead continue with Python?
When it comes to scripting languages, the objective differences are very nuanced. Largely, it depends on what you want to do. Python, for example, is extremely popular in the data sciences. A complementary example for Ruby would be rapid application development with Ruby on Rails, a web framework that allows you to build web applications very quickly. Another example would be building an API using Ruby's Sinatra library. Python also has web frameworks, so it's not as if Ruby has an exclusive claim to this benefit, but many developers find tools like Ruby on Rails and Sinatra very satisfying and beneficial to work with.
My recommendation would be to give Ruby an honest shot. Don't make the mistake of simply trying to write Python code using Ruby. Really dig in to what makes Ruby, Ruby. If you enjoy it, then you've added another language to your tool belt. If you don't, you might walk away with some ideas about development that you can apply to Python.
Ruby delivers on the promise of being "optimized for programmer happiness." But I think that in order to experience that you have to become fairly immersed. In fact, some of the best parts seem outright offensive at first (question marks in method names?!). No language is perfect. But once you get past the idiosyncrasies, I honestly do think Ruby feels better. That's pretty esoteric, so I'll try to call out some specifics as well.
I agree with most of what's already been said, but I'll try to add a few things. In order of most to least significance (for me):
The standard library, especially with regard to collection methods. Want to slice/filter/sort/chunk an array/hash in some weird way? Ruby's standard library almost certainly supports it. So many amazing things are built-in across the board.
Not relying on indentation for scoping. It's one of my biggest beefs with Python. Yes, of course, code should be indented properly. But goodness....let my linter enforce that, not the interpreter. I don't love ruby's do/end keywords (I prefer curly-braces), but at least having a visual cue for end-block is a vast improvement over python.
A more consistent interface. Everything is an object, and you invoke methods on those objects. I think [].size just makes more intuitive sense than len([]).
Great readability boosts from things like question-marks or exclamation-points in method names (admittedly that felt gross and wrong at first), trailing if-statements, unless-conditionals, invoking methods without parens (though I only sanction this if not passing args).
A more helpful, less snobby community. 100% just my personal experience, maybe I've just had bad luck with pythonistas.
No __init__.py nonsense. Maybe that's fixed/improved in python3? But I hate it. In fact, I hate any use of dunders...littering the code with unreadable symbols.
Ruby vs Python: Which is better suited for scripting utilities on Windows? And why? - Stack Overflow
What does Ruby have that Python doesn't, and vice versa? - Stack Overflow
Ruby vs Python
Python is more popular in when it comes to scientific computing and big data analysis. Ruby (I believe) is more popular when it comes to web development.
The major difference come down to programming philosophy imho. Ruby is more about expressiveness and flexibility while Python emphasizes correctness and consistency.
More on reddit.comAsk HN: Why Python over Ruby?
Videos
I started with python and ruby both few months ago. Ruby has loads and loads of issues and I find python really cool. Dint try with scripting. I am working on web apps. Used python script to to interact with database and inserting raw fields. Works really fine.
Ruby's type system is much simpler and cleaner and allows a smaller and simpler language core. And, you write scripts that don't look like OOP in Ruby just as easily because the default "global" namespace is the body of a default "main" object, which just implements all the usual imperative functions like print, gets, eval, etc. Ruby also allows you to write scripts that don't really look like you're using objects. Ruby people complain about Python's lack of blocks, the difficulty of doing dynamic code generation, the awkwardness of trying to modify existing classes. They complain about these things
You Can Also Check This Link
Ruby has the concepts of blocks, which are essentially syntactic sugar around a section of code; they are a way to create closures and pass them to another method which may or may not use the block. A block can be invoked later on through a yield statement.
For example, a simple definition of an each method on Array might be something like:
class Array
def each
for i in self
yield(i) # If a block has been passed, control will be passed here.
end
end
end
Then you can invoke this like so:
# Add five to each element.
[1, 2, 3, 4].each{ |e| puts e + 5 }
> [6, 7, 8, 9]
Python has anonymous functions/closures/lambdas, but it doesn't quite have blocks since it's missing some of the useful syntactic sugar. However, there's at least one way to get it in an ad-hoc fashion. See, for example, here.
Python Example
Functions are first-class variables in Python. You can declare a function, pass it around as an object, and overwrite it:
def func(): print "hello"
def another_func(f): f()
another_func(func)
def func2(): print "goodbye"
func = func2
This is a fundamental feature of modern scripting languages. JavaScript and Lua do this, too. Ruby doesn't treat functions this way; naming a function calls it.
Of course, there are ways to do these things in Ruby, but they're not first-class operations. For example, you can wrap a function with Proc.new to treat it as a variable--but then it's no longer a function; it's an object with a "call" method.
Ruby's functions aren't first-class objects
Ruby functions aren't first-class objects. Functions must be wrapped in an object to pass them around; the resulting object can't be treated like a function. Functions can't be assigned in a first-class manner; instead, a function in its container object must be called to modify them.
def func; p "Hello" end
def another_func(f); method(f)[] end
another_func(:func) # => "Hello"
def func2; print "Goodbye!"
self.class.send(:define_method, :func, method(:func2))
func # => "Goodbye!"
method(:func).owner # => Object
func # => "Goodbye!"
self.func # => "Goodbye!"
I am a CS student and we have just begun studying OOP in Ruby this semester. I have had quite minimal usage of Python over the past year.
I love Ruby. I find it very refreshing to write and read compared to C, C++, Java etc. (although I do still thoroughly enjoy these languages). With my lack of experience in both Ruby and Python, I see them as two very similar languages - a program/script written in one could be translated into the other very easily. However, for some reason I find myself preferring Ruby over Python. It just comes across as the most user-friendly language I have written in so far.
Why does it seem that so many more people write in Python compared to Ruby? Does Python offer better performance? I'd imagine that Python has a more vast collection of libraries to offer, but this, again, would mainly be down to the fact that it is more commonly used.
Is there any reason why I couldn't just forget about Python and focus on Ruby instead of it? Are there any concrete examples where a program would be better suited to Python than Ruby, or vice versa? I know that languages are just tools to solve problems with, but it is easy to choose when to write a program in Python over Java, for example. Yet, I can't say the same for Ruby vs Python.
Python is more popular in when it comes to scientific computing and big data analysis. Ruby (I believe) is more popular when it comes to web development.
The major difference come down to programming philosophy imho. Ruby is more about expressiveness and flexibility while Python emphasizes correctness and consistency.
Python and Ruby are both very mature languages with good standard libraries and a huge collection of supporting libraries ("gems" in Ruby parlance). I wouldn't say one has "more" developers than the other, but due to the libraries and communities that have developed around them, you will find their popularity varies in different circles.
In the web development world, Ruby reigns supreme. Projects like Ruby on Rails, Sinatra, and Jekyll are still going strong. Chef, which is used for configuring and managing servers, is Ruby (although you can deploy any kind of server with it, not just Ruby/Rails ones).
I also use Ruby for day-to-day shell scripting as a much nicer alternative to bash.
Python is (was?) huge at Google, and is also very popular within scientific communities due to the good support of mathematical libraries like NumPy, matplotlib, and others (I know much less in this area). Python also has a web framework, Django, which is moderately popular and follows in the footsteps of Rails.
In the end, since you're just learning OOP concepts, either will work totally fine. If you like Ruby, then go for it. You'll be able to transfer the knowledge to other OOP languages quite easily. Ruby also leans heavily on functional concepts, which will aid you when you inevitably pick up functional languages.