back

by thunderbong·6y ago·view on hn ↗
Honest question -

I'm very familiar with Ruby and for my automation tasks I tend to use it almost exclusively. Of these, is there anything that -

1. Ruby can't do

2. Or is more complex / complicated to do in Ruby

8 comments
I'd say the languages are largely equivalent. Roughly the same level of abstraction, roughly the same technical trade-offs. Highly productive, lots of people enjoy them, not very strict.

What Python does have specifically for automation in my eyes is that it is often the default language for most Raspberry Pi-related hardware libraries. So if you want to build home automation on accessible and easy hardware, the Pi is very welcoming to Python just in terms of making things easy, providing libraries.

It is also heavily supported for machine learning and computer vision which is related. Again, this is generally just a matter of library support.

I don't know the Ruby ecosystem well enough to say if it has equivalents to all the Python scraping, fuzzy matching and such libraries out there. But I expect it does. So that bit is probably a wash.

Hope that gives a perspective.

Yes, I know about Python's extensive ML libraries.

Didn't know about it being the default language for Raspberry Pi libraries.

Thanks. I've been planning to look at both learning some ML and doing some small Raspberry Pi projects. That's good motivation for diving into Python!

1. no 2. no, not really - however there may be libraries that python has that ruby doesn't have

Side note: Ruby has more syntax to grok, which makes it harder to read (for me). On the other hand Python does have its own idiosyncrasies and subtle behaviors. However, Ruby also has some features (like multiline inline functions) that one may easily miss in Python, but they haven't been worth switching over for me.

Python has multiline inline functions--perhaps you mean lambda?
my bad: meant multiline lambdas.
Honestly, I've done a lot of professional work in both languages, and there is pretty much nothing you can't do with ruby that you can in python. There is no intrinsic difference in power of expressiveness between the languages. Although ruby is lagging a little behind with the asyncio syntactic sugar stuff. You may reach a point where there simply isn't any client written for the service you are trying to automate in either language. And python got more love in that aspect what with all the being the number one preferred scripting language on planet earth, but in case your target of automation provides a simple rest or websocket protocol you could easily do without a dedicated client library for it.
Thanks. I really don't have any idea about the asyncio syntatic sugar that you mention. Can you point me to some books / articles?
Oh async/await is just python's and javascript's way of executing functions concurrently within the same process. asyncio emerges as a rewrite of a standard library to support this idiom. Browsers that execute javascript ES6 and node v7 onwards have this feature. Instead of passing callbacks to functions and have them executed concurrently, you create a task or future (in python 3.6+) or a Promise (in javascript) and await that. That is very useful because it untangles the callback-inside-callback structure.

There's a comparison between node and python's async/await syntaxes here https://medium.com/@interfacer/intro-to-async-concurrency-in...

For ruby I found this article which shows the gist of what we are trying to accomplish with async/await (we want to gather a bunch of promises and await all their results and have them run concurrently in an event loop so that when one is waiting for IO to complete, the other one takes over and runs) https://www.codeotaku.com/journal/2018-06/asynchronous-ruby/...

I understand async/await as I'm conversant with NodeJs. When you mentioned asyncio, I assumed it be something specific in the Python library.

In case of Ruby, I've found these libraries (ruby gems) to be very useful.

1. Sidekiq - https://github.com/mperham/sidekiq

2. RocketJob - https://github.com/rocketjob/rocketjob

What I'm guessing you're saying is that this kind of behavior is available natively in Python, whereas in Ruby, it is achieved by some roundabout fashion.

These gems are not really related to the asyncio stuff in python. (They're async job runners though... overloaded names can be annoying)

async/await works at a function level in the same process. Asyncio in python is pretty much the async/await from node/js.

Sidekiq alternative in python would be closer to http://www.celeryproject.org/

Yes, async functions are natively available in python3.6+ and node7+, and requires a gem in ruby. Ruby can also use fibers and eventloop to do concurrency using just the standard library, but it is missing the async/await syntactic sugar.
> [Python] being the number one preferred scripting language on planet earth

Is it?

I'd imagine JavaScript usage/execution is probably way way higher.

Yes python is more preferred when choosing a scripting language and javascript is more executed in browsers. And the two do not contradict.
I think you're both correct. For sysadmin definition of scripting, python would be most popular. For the 'fast to write' definition of scripting then sure, JavaScript.
I don't think I've ever heard that as a definition of "scripting".
Which one?

For the second, the definition of 'scripting language' used to be 'doesn't use a compiler' but JS and everything else has a JIT now, so 'make something quick' is a modern equivalent.

But people don't use javascript to write scripts. Javascript is mostly used in writing web services.
I mostly write javascript now for my day job... But python is nicer for scripts... The asynchronous nature of node makes it a pain for me
Async/await largely solves that though.
To an extent... If you're using something that supports promises and not callbacks, which not all of the things you want to use to write an actual script do.
You can easily transform a callback into a promise via simple glue-functions:

    let result = await CallbackPromise.from(some.callbackConventionFunction, param);
Then you get a unified form for the code-base.
Python is significantly more popular than Ruby. It doesn't mean that it's a better programming language, but a library for X or an example of how to do X are significantly more likely to exist for Python than for Ruby, and there are other popularity advantages like more implementations.
There's micropython for the esp8266... not sure if ruby has been ported
If you like ruby, checkout crystal.
Honestly, variations of this question has been asked too many times to be taken as an “honest question”. See also: Rails developer hijacking Django thread.

It’s weird lots of Ruby developers feel the need to shout “don’t forget ruby!” when nice things about Python are posted. Just take your pick, who cares.

Btw, I know, assume good faith, yada yada.

I researched Ruby, but the syntax idiosyncrasies of the language made it appear to be created by an amateur. Like weird language constructs to do something, that makes you go, "huh! What kind of random junk is this?"

Some people obviously swear by it, and thus created Ruby on Rails.

Python does have some weird features as well, but I try to steer away from it, and keep my code simple and boring. My primary concern is how efficient the code is executed as it gets translated into machine code.

The cost/benefit ratio of switching to Ruby never quite seemed worth it. So I continue to stick with Python, for applications that are not time execution sensitive. For all others, there's Java, then C++, then C itself.

you say “created by an amateur” like it’s an insult.

some of the best things we have are created by total amateurs. thats how this works.

i’m not 100% on the life histories of Matz and Guido but wasnt Guido kind of also an amateur too?

They were both involved academically with language design.
"...Python, for applications that are not time execution sensitive. For all others, there's Java, then C++, then C itself."

That is assuming you know how to work with data structures and algorithms the right way. A O(n^n) implementation runs slow in C as well.