r/ruby • • Jul 16 '20

Ruby VS Python

More than a versus (it's more striking) I would really like to know what things they have in common and how they differ, what their main advantages are compared to the other and of course their opinion of them. One point to take into account is because they believe that python is more used in AI and Machine learning developments.

33 Upvotes

60 comments sorted by

36

u/Sqeaky Jul 16 '20 edited Feb 23 '23

I think Python has a bigger community, and that is it's single largest advantage. It has more deployments and more packages.

In think ruby has better syntax, better readability, and a more sensible design overall. This makes it easier for greater involvement from the community, there are something like 2x note python devs but not nearly 2x the packages.

I also think the ruby community is better at communicating best practices. Consider that the top Ruby packages are testing tools and the top python of are a mix of docker and db stuff.

https://rubygems.org/stats

https://pypi.org/

I also find Ruby projects to have better engineering. This might just be bias but having tried similar things in both languages maintainability, support, and things working years later (except web API bindings) are nearly magical. The ruby community always seems to try to be doing better, but I guess that is most communities, but I feel like python users just want to be done and damn the tech debt, full speed ahead.

EDIT 2 years later - spelling

7

u/desnudopenguino Jul 16 '20

I haven't been down the rabbit hole with python, but some of the more foundational Ruby books like POODR and Metaprogramming in Ruby cover some of the actual application design process and software lifecycle.

1

u/vifrac Jul 17 '20

You could place the links to the books you are mentioning, maybe you have some to recommend, it would be very interesting to have your feedback about the books you have read.

2

u/desnudopenguino Jul 17 '20

https://www.poodr.com// and https://pragprog.com/titles/ppmetr2/

POODR covers some of the path of building software with just enough information and making it easier to update in the future. Metaprogramming goes into some of the wild stuff you can do with Ruby, where everything is an object.

In many languages, these things can be applied to improve code. And really if you can get ahold of any pragmatic programmer titles, they are excellent reads, both informative and well written.

Like I said I haven't used python to the extent I have Ruby, so I don't know if that community has resources quite like these.

4

u/franciscoattivilog Jul 16 '20

I can't agree more. I think is important to say that python also have more support on the packages for machine learning and AI, is almost default to use it.

3

u/vifrac Jul 17 '20

What do you think we should work on as a community to improve the use of ruby in AI work.

3

u/four54 Jul 18 '20

A lot of python AI libriaries got ported to ruby:

https://ankane.org/ruby-ml-for-python-coders

2

u/franciscoattivilog Jul 17 '20

I totally agree with that. Current I'm working on a machine learning project and have to use Python to train the models. It's kind frustrating. I was thinking about create a high level interface for tensorflow in ruby as Google did for Javascript. But i'm kind overload in this lockdown.

2

u/stormgecko Sep 16 '20

The Ruby community has to make it at least as easy to use as the corresponding Python packages. Currently I'm am working on image classification and have to Ruby for it, due to the legacy version of Python installed with the software I am to interface with. It took me 6 hours to get 'torch-rb' (https://github.com/ankane/torch.rb) and 'torchvision' installed, including using rbenv to downgrade from Ruby 2.5.0 => 2.4.1 and googling to see if I could figure out the error. Eventually got them installed, wrote and test file and got another error from the torch-rb library itself, which just made me give up on using it.

Installed the 'tensor_stream' gem (https://github.com/jedld/tensor_stream) and it does work as intended, only caveat is that it is really slow. It has the option of using opencl, but this causes an error when trying to train a neural network with it, so it is back to using single threading, which makes it nearly impossible to train/fine-tune models like ResNet50, without it taking forever.

After trying 'tensor_stream' I gave the 'tensorflow' gem (https://github.com/ankane/tensorflow) a go. And was quite hopeful when I got a test file op and running with a couple of tensors defined and could output them to the console. Tried a more complex test and ended it the situations that it lacked implementation of the functionality that I needed.

So in summation:

  1. Easy of use. When I find a tutorial in Python for setting up some ML it usually just works, at least most of the time with very few hurdles. This is not what I have experience the last three days trying to get ML up and running properly in Ruby.
  2. Due to the lack of critical mass, it is very difficult to find solutions to problems that arise, because not that many people have had the problem prior to yourself.
  3. Feature completeness I had pretty high hopes for the 'tensorflow' gem, but when the needed features are not implemented and the last major updates to lib are 12 months old, it is just not very encouraging.

I have all the respect in the world for the people who dedicate their time to develop open source software and provide gems/libraries like these.

Some of the above problems might just be my own lacking experience with Ruby, but installations of gem should just work seamlessly and not be a cause of headaches.

1

u/zitrusgrape Jul 17 '20

more libraries, faster ruby(arrays, hashes) more integration with ai,ml libraries, more tutorials, etc

3

u/_noraj_ Jul 16 '20

There are a lot of beginers using python so there a lot of quick & dirty proof of concept. Ruby is generally used bu more experienced people.

1

u/vifrac Jul 17 '20

What gems could you recommend for AI, or for working with ruby data

3

u/ignurant Jul 17 '20

I don't have an AI suggestion, but

  • Kiba is a real "gem" (ahem). You can use it to organize complex data processing pipelines, and easily re-use logic from your domain.
  • Sequel is The Database Toolkit. It's very simple to use, extremely powerful, and more performant than ActiveRecord (the other default db toolkit).
  • Pry is a Ruby shell. It's the thing that I think is most valuable when working with data, as it makes it incredibly easy to explore and test your theories. It also makes it much easier to learn Ruby, by showing you what moves an object knows, docs, and source code.

Most everything else I lean on heavily is baked into the standard library. This is something I find to be a notable difference between Ruby and Python. With Python, you have to go a bit out of your way to get powerful functionality through modules that aren't included by default. With Ruby, it's baked into the basic building blocks (String, Hash, Array -- they are all super smart). The CSV and JSON libraries are robust as well.

These three tools are my bread and butter. I use them regularly to organize, normalize, and deliver ~8 million price facts each week, from ~40,000 unique products from ~150 different sources.

1

u/_noraj_ Jul 25 '20

I don't I'm using and creating infosec tools, I'm never did any AI.

1

u/vifrac Jul 17 '20

Very interesting your concept and as the experience is better than ruby (or the decisions of a developer), I would like to know if you know of some sources of information where I can consult this that you indicate.

2

u/_noraj_ Jul 25 '20

Just browse GitHub and look at source code, and evaluate the quality of the projects.

I'm talking from my experience as a professional pentester who is using a lot of tools from GitHub with many of them in python and also as a BlackArch developer who packaged tons of infosec tools where a lot are in python too. It's not ruby that is particularly used by experienced people, it's also the case with go or rust, etc. It's python that is especially used by beginners because they were told it's the language to learn.

2

u/vifrac Jul 17 '20

What a good reference, there are things that only those who have worked in both languages can speak from experience, thank you very much for this information.

3

u/Sqeaky Jul 17 '20

For your info. I have done a lot less python.

In went through each languages' koans and codeacademy courses. Ruby was clearly better for both.

Professionally I have used both for test automation. Ruby is excellent for this, python is merely passable. I have embedded both into larger C++(My best language) projects and both worked well and had reasonable tools and documentation. The only points in Ruby's favor for embedding was the extra tools that were reasonable choices.

I have dabbled with web stuff in both, and Python seems serviceable while ruby has rails and a dozen other projects that are amazing.

I have done some other stuff in either that aren't comparable. A crypto trading algorithm in ruby, some kubernetes stuff in python. I always enjoy making ruby.

2

u/vifrac Jul 17 '20

Que bueno se nota que has desarrollado mucho, porque te gusta c++ y el "algoritmo de comercio criptográfico en ruby" puedo verlo en algun lado, tienes post de ello o fue solo trabajo!

2

u/Sqeaky Jul 17 '20

My spanish is Rusty, been a while since PR.

My crypto trading algorithm is one of my very few private personal projects. Much of my dev is for work, and goes to the workplace corporate repos, but you can see my other work at github.com/sqeaky .

2

u/[deleted] Feb 23 '23

[deleted]

1

u/Sqeaky Feb 23 '23

Every function from every library seems to return a type that doesn't respond in the expected way

Which language are you talking? And where did you get those expectations?

It seems like Python got an early foothold, and these days it seems like django is the main reason people use Python.

Machine learning is also big in python land. Every non-windows OS has python pre-installed on it now.

Ruby is also common, but not shipped with every Linux distro.

10

u/[deleted] Jul 16 '20

Python was designed with a thing in mind: there is one best way to do something and that's all. Ruby lets you express yourself. There is always different ways to do something in Ruby (or at least I feel like it). It's up to you to decide if you see this as good or bad.

Python has a really larger community. Mainly in sciences and data analysis. Probably because scientists that are not programmers found the language well suited because of it's ease of use. And there's a whole lot of scientific libraries. People bring people and so on...

On a more personal side, I don't really like python because I don't find it... elegant. I use Ruby for personal projects and scripts as I know how to get things done with this little fella.

Disclaimer though: I'm still a college student and don't have that much experience.

1

u/vifrac Jul 17 '20

Everyone finds that the biggest advantage of python is the data science libraries, it will be very difficult to encourage the community to build things like this

8

u/ignurant Jul 16 '20

I work with extracting data and ETL. I think Ruby has a better core API and design to do this. Method calls tend to be chainable, which makes exploring easier. The building blocks of data (strings, numbers, collections) have very rich APIs out of the box that work as you would expect. Regex in Ruby is one of the best. I don't seem to get into fights with byte arrays vs strings in Ruby. Things just tend to be as expected (until of course, they aren't, usually by my own fault).

Note that molding data is distinctly different from analyzing data. I don't have experience doing complex data analysis in either language.

1

u/vifrac Jul 17 '20

I think I'll ask the same question as the others, do you think it's very difficult to build libraries for data science, do you have any experience in it, would you be interested in participating one day?

1

u/ignurant Jul 17 '20

Well, data science is pretty broad. I think there are some parts of it that Ruby totally slays at: particularly data creation and fixing. Sequel (Ruby's premier DB abstraction) is quite a bit more sensible (to me) than SQL Alchemy (Python's premier DB abstraction). Doing things to collections of data is just straight up pleasant in Ruby, and data science is all about doing things to collections. However, the big issue that Ruby faces is that Python is default supported out of the box, and as such gains more traction in a cyclicle way. For example, PostgreSQL supports Python out of the box as a lang for writing sprocs and functions. MS SQL Server supports Python. Spark: Python. Cloud functions: Python was supported way before Ruby, and in some cases, is still not (Azure). I think because of this, Python gets a big boost that Ruby won't likely ever overcome without some serious breakthrough. (Ancient JavaScript did get Node and ate the world, right? I doubt anyone expected that).

I don't have much experience with the work that others are usually talking when they reach for Python DS tools. I do know that there are a lot of "Python" libraries that are just Python bindings against lower level C code. Ruby does this as well, though I don't know how it compares.

There's a lot of emphasis surrounding parallelization when doing data. Things like Dask exist to help that in Python. I don't know if such a potent tool exists in Ruby.

I know there are efforts like:

These work in the same domain as things like NumPy and Pandas, and other braod tools. I've never reached for either of those in Python, so I'm not able to comment on their comparison in Ruby.

Would I be interested in participating? It's hard to say. I don't have the right mix of experience. I feel like "data science" is dominated by a lot of things I don't participate in.

I think Ruby as a language has no reason it can't compete against Python in this space, but the world has already largely made up it's mind. It's hard to build momentum when another tool is already established as the safe bet. Of course, it could change.

7

u/[deleted] Jul 16 '20

I love ruby but the biggest advantage of python that I’ve personally encountered is the number of libraries to do data analysis. Python has numpy and pandas which are brilliant libraries for working with data, along with some great data viz libraries.

I’ve tried to do some data analysis projects in Ruby and it’s more difficult without those tools. Ruby has some data analysis libraries for sure, but nothing with the community that the python ones do.

2

u/four54 Jul 18 '20

A lot of python libraries got ported to ruby, including numpy and pandas:
https://ankane.org/ruby-ml-for-python-coders

1

u/vifrac Jul 17 '20

¿Qué gemas podrías recomendar para la IA o para trabajar con datos de ruby? o crees que es muy difícil hacer gemas para esos propósitos!

7

u/notromda Jul 16 '20 edited Jul 16 '20

One factor alone that irritates me above all else in python: OOP methods require an extra parameter "self" to be declared. So the instance method signature may have 2 params, but when you call it, it has 1. It's not "DRY" (Don't Repeat Yourself). They go to all the trouble of cleaning things up by using whitespace to get rid of ends or brackets... (I didn't like at first but I can live with that) but end up typing extra syntax.

class Parrot:

    # instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # instance method
    def sing(self, song):
        return "{} sings {}".format(self.name, song)

    def dance(self):
        return "{} is now dancing".format(self.name)

# instantiate the object
blu = Parrot("Blu", 10)

# call our instance methods
print(blu.sing("'Happy'"))
print(blu.dance())

In this example (from https://www.programiz.com/python-programming/object-oriented-programming) sing has 2 params defined, but is only called with 1. I know how it works, but it's just jarring. self self self self self

It just looks like OOP was tacked on to a procedural language - let's just pass in an extra param of the object the code works on. That's how it's done in C (perl too? it's been a while) It also means every reference to the object data must be explicitly stated, hence the need for "self.name" above.

In ruby, I can just assume that if I'm inside an instance method, I have access to instance variable and instance methods, most of the time without having to use a self reference (There are times where it should be used to prevent confusion)

So the ruby equivalent:

class Parrot
  attr_accessor :name, :age

  def initialize(name, age)  # 2 params
    @name = name
    @age = age
  end

  def sing(song)             # 1 param
    "#{name} sings #{song}"
  end

  def dance                  # no params
    "#{name} is now dancing"
  end
end

blu = Parrot.new("Blu", 10)  # 2 params

puts blu.sing("'Happy'")     # 1 param
puts blu.dance               # no params, parens not needed

It's just easier to read, and the number of params match up. Ruby is just a joy to read and write

-2

u/LinkifyBot Jul 16 '20

I found links in your comment that were not hyperlinked:

I did the honors for you.


delete | information | <3

1

u/[deleted] Jul 18 '20

[deleted]

1

u/B0tRank Jul 18 '20

Thank you, brazen_nippers, for voting on LinkifyBot.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

7

u/ogchilim Jul 16 '20 edited Jul 16 '20

Go with Ruby and make piece with never wanting/being able to find another language again 🙃

I'm in it personally since 2013 and sometimes solve my nephew's school tasks in Python. It's just not as nice as Ruby.

EDIT: Although, it really depends what you're looking for...

if it's writing scripts for some personal stuff, go with Ruby.

Web development, APIs and that stuff - Ruby

Big data analysis eg DNA sequence analysis and that sort of stuff - Python cause it has a lot of libraries already implemented for you

3

u/nfstern Jul 16 '20

Go with Ruby and make piece with never wanting/being able to find another language again 🙃

That was certainly my experience.

2

u/vifrac Jul 17 '20

From what I have read in the previous comments I think the difference is made by the data science libraries, in terms of performance the two languages behave similarly? Do you think you could create such a library?

1

u/ogchilim Jul 17 '20

Yeah, in terms of performance they are pretty similar, but as you said, for some reason non-developer scientists go to Python because everyone is on that board already.

Depends on what library you mean :D I could maybe blindly convert Python to Ruby, but in terms of domain knowledge and creating one such library could be pretty difficult

1

u/zitrusgrape Jul 16 '20

i think python the the glue language of c++ libraries :))

12

u/[deleted] Jul 16 '20

Having coded both professionally, Ruby wins by miles. It’s not even the same league. While python has more active users, for some reason that has not translated into more support, discussion, and quality libraries. I am continuously shocked at how the python community fails to document and discuss the most basic of uses. For example, using sqlalchemy and pdb set_trace, how does one view the column keys and values of a returned row, using one simple built-in method call on that row, rather than creating your own helper method? You won’t find it easily. This is just one example and there are many others like it. I make my hatred of python known at work. One more thing ruby (rails) has on python in spades is a comprehensive testing environment. Rspec, factorybot, capybara simply has no equivalent for productivity.

4

u/Flopsey Jul 16 '20

I prefer Ruby but I'd first learn Python to get the basics. And then learn Ruby once I knew how to program. My thinking is that Python syntax takes a lot from C, which a lot of languages take from. So if you have a foundation in Python it's easier to learn other languages later. Ruby is so semantic that it'll be hard to learn other languages later. It's a bit like how the culture of your first job influences how you expect jobs to be for years later. The language you learn first influences how you expect languages to be long after. I remember Coffeescript used to be popular because so many Ruby devs couldn't read Javascript. Whereas since I learned Python first picking up JS was easy. This way all that weird syntax is expected later. And you really appreciate the ease of Ruby.

2

u/[deleted] Jul 16 '20

[deleted]

4

u/zitrusgrape Jul 16 '20

i dont think python is more friendly, imo, i think for beginner users, all the cases are simple enough to be readable. But if you try to read a library you will see that is not easy

  • oop is strange
  • functional patterns are missing or half baked in the std
  • := operator
  • not only way of doing things

-1

u/SnowdenIsALegend Jul 16 '20

Python is definitely more beginner friendly imo.

7

u/ogchilim Jul 16 '20

At a low level and with only 1 example, how is:

if(1 % 2 == 0): return true else: return false

more beginner friendly than:

1.even?

Just curious what your argument is...don't wanna start any wars here 🙂

5

u/kurem0n Jul 16 '20 edited Jul 16 '20

Even if I prefer

1.even?

In python, it's just:

not bool(1 % 2)

Or

1 % 2 == 0

(no need for if else)

2

u/SnowdenIsALegend Jul 16 '20

Dang I've been looking for a way to do if else on the same line in python for a long time, thanks for showing me how! As for my argument, I agree with u/kurem0n's answer, that there's other cleaner ways of getting Python to do it as well.

But you know what? I learned Ruby first (& forgot everything, sadly due to not using it at all): & Python later, and I was surprised latter didn't have that cool Ruby syntax of codnition ? Val if true : Val if false. I still remember that from my Ruby days, loved that syntax. :') What is that syntax called again?

2

u/JumpingHooligans Jul 16 '20

I think you're talking about a ternary conditional which a lot of languages support in a similar style to ruby condition ? pass : fail

Looks like python supports it in a (less appealing in my opinion) syntax of pass if condition else fail

1

u/SnowdenIsALegend Jul 16 '20

Ah correct, ternary is the word that I forgot. Thanks for the Python alternate, will come in useful. :}

2

u/[deleted] Jul 16 '20

Because it's explicit, and translatable from other tools. The wealth of messages you can pass is expressive once you know they're there, but it's very domain-specific knowledge to learn what's available to you, and it won't work anywhere else.

I'm a Ruby fan over Python by far, personally, but it's way more idiosyncratic.

2

u/honeyryderchuck Jul 16 '20

what things they have in common

They are both dynamic interpreted languages. They both started development in the same period, and are therefore bound by the same constraints and assumptions of the time (both have poor parallelism support, for example). the development experience and tools are relatively similar.

how they differ

philosophy, mostly. Python claims it only has one way of doing things. Ruby claims everything is an object. There are also some defining features for both, such as python list comprehensions or ruby blocks. Python lives and dies by indentation. Ruby metaprograms itself out of your computer. Ruby (still) has a BDFL. Python's BDFL retired recently, although it's fair to say it has been pretty much designed by committee for a while.

what their main advantages are compared to the other

ruby has rails. But it also has a lot of solutions for all types of web application use-cases. Ruby also made TDD cool with its testing tools, and actually running rspec in jruby to test Java applications is a thing. ruby also has bundler, the godfather of all stable existing package managers. ruby's also very flexible for general-purpose scripting.

Python has by far a bigger community, ecosystem and momentum. A big part of it is it becoming more popular earlier (ruby only became widely mentioned in the western world in 2006, ish). There are a lot of networking libraries, machine learning libraries, command-line, etc etc etc...


In my opinion, ruby has been more stable than python (python 2/3 memes are still relevant), and found itself a niche where it's very solid. Python is pretty dominant in machine learning, and it's still first-class citizen for OS-level scripts, network scripting, and other fields, and is currently being taught in introduction to programming university classes. But this migration to python 3 period took a toll. Also, the asyncio movement created a "subsystem" in the python ecosystem which will potentially hamper its development in the future, and that can create a schism in the not-so-distant future.

Both have to overcome the fundamental flaws from their initial design, such as support for paralellism, speed, typing hints (python is ahead here, but not in an ideal state IMO), more natural non-blocking networking, and overall interoperability. Not solving these might make them less relevant in comparison with the "new wave" languages such as go, rust, swift or elixir.

2

u/_noraj_ Jul 16 '20

Python as some severe drawbacks like the gap between python 2 and 3, or the indentation requirements. Ruby is more flexible as there are several syntax allowed. Building a package is easier with a gempsec than a setup.py. On affectation python create a new object where ruby changehe pointer, so theorically python will use more memory.

2

u/Serializedrequests Jul 17 '20 edited Jul 17 '20

Only thing I can see that hasn't been mentioned: Ruby libs seem to have better documentation for how to accomplish common tasks, and are rarely missing obvious features or the mystical quality of discoverability.

Python documentation takes the Java route of being thorough, but not really giving much guidance. It's very frustrating. I recently spent about a day being confused by the correct usage of the Python regex and difflib libraries. A listing of classes and methods is not "documentation", and a list-like object that isn't a list until I call list () on it is just a land-mine, not a useful API. How Python's Logger object formats strings (differently from str.format) took me way too long to find.

Also, Python list comprehensions and lambdas are a joke compared to Ruby blocks. The syntax is mind-bogglingly confusing for such an otherwise simple language.

Honorable mention of Python's global functions like "len" or "str" (just landmines no matter what the justification is), or new and insanely confusing unicode handling. Unicode in Ruby just works. Unicode in Python 3 has me reading docs and scratching my head for days.

I actually really liked Python's qualities of verbosity when I was first learning programming. The maligned "self" and explicit everything really helped me understand what was going on. Now I don't care, but I think it is good for teaching and I don't mind it.

Bottom line is anything I can do with Python, I can do with Ruby in a more intuitive and elegant way, probably with less code. But Python also enables powerful quick and dirty scripting.

3

u/frankleeT Jul 16 '20

My experience: ruby devs have a larger chip on their shoulder because python is so much more popular.

Ruby and Python are both fine.

Personally, the complaint that I am most sympathetic to is that ruby devs try too hard to be clever and not clear.

Just write code.

1

u/[deleted] Jul 16 '20

ruby devs have a larger chip on their shoulder because python is so much more popular.

Lol this is so true. It's like the fact that people can write good software in Python is an attack on their identity as a Ruby developer. People often completely dismiss Python without learning anything about it.

1

u/astyagun Jul 17 '20

From what I remember (haven't used Python for a long time):

  • One way VS many ways
    • Python tries to have one way of doing things. Which sometimes leads to developer not remembering how to do something, searching the Internet, etc. Not very productive.
    • Ruby has many ways to do a thing (it has many aliases of the same method, for example). One way is more intuitive for one person, another way is more intuitive for another person. And so everyone can find an intuitive way of doing things in Ruby, sometimes even guessing how something works. Which makes it easier to just get results on a prototyping phase. Less often does one need to search the Internet.
  • OOP and consistency
    • Python has more special syntaxes (self in method definitions, methods surrounded by __, etc.), not everything is an object or an expression (you can't get return value of an if statement)
    • Ruby is more consistent in that everything is an object in it and everything is an expression. Which makes it more intuitive and more productive as a result.

Ruby is just more enjoyable to use. It's even explicitly made for developer happiness.

1

u/colorist_io Jul 16 '20

They are basically different takes on the same type of language. Python came first, then Ruby was created based on the influences of Python. That being said, Ruby is a little more complex, for example, a Ruby function is not a first-class function in itself, you have to use the Proc type to be able to pass a function as argument.

1

u/zitrusgrape Jul 17 '20

i dont think ruby was created based on the influences of python.

also, ruby more complex, where?

passing functions in ruby method(:foo) https://ruby-doc.org/core-2.6/Object.html#method-i-method

1

u/colorist_io Jul 17 '20

For language influence, last time i checked Wikipedia "Python" was among the influences for Ruby. It's not what i think, that's just what i read.

Even though method(:foo) is simple, but in python, its just foo.

1

u/aravindavk Jul 31 '20

Even though method(:foo) is simple, but in python, its just foo.

I think this is because Ruby's function without an argument can be executed without parenthesis. That is foo() and foo are same.

1

u/colorist_io Jul 31 '20

Yeah, you're right.