Live data from Hacker News

Ruby: A great language for shell scripts

lucasoshiro.github.io

211–220 of 368 posts

Re: Ruby: A great language for shell scripts

#211
post #172

Earlier quoted context omitted.

Ruby is surprisingly picky with white space

Can you give an example? I can't think of a single situation where whitespace matters in Ruby (unless of course you forget to put a space between two commands or something silly).

if foo is a method then

`foo + bar` and `foo+bar` are `foo()+bar`, but `foo +bar` is `foo(+bar)`

ternary ? : also has some interesting whitespace dependent mixups with symbols, but I cannot remember what. I think that parser has many gotchas like that, but they are really really rare to bite you, because ruby's magic follows human intuition as much as possible.

Re: Ruby: A great language for shell scripts

#212
post #141

Earlier quoted context omitted.

This is indeed why I use Perl over Ruby. As long as it's not for a Window machine, a Perl script is deployed by copying it over and that's it.

That's true of Python and Perl as long as you keep using only the features built in in the core language (standard lib or whatever they call it.) The same applies to Ruby. My scripting language is bash in at least 99% of cases. I used to program in Perl when I need some complex logic. I stopped using it some 10 or 15 years ago when I switched to Ruby for two reasons: I became more familiar with it than with Perl and…

In fact, that's true for Python if you use a zipapp and no c extension: https://docs.python.org/3/library/zipapp.html

You can happily copy the zip of your scripts and all deps in the server.

You still do have to mind your versions, as always with python.

Re: Ruby: A great language for shell scripts

#213
post #133
post #108

Earlier quoted context omitted.

> I sometimes wonder why we don't see ruby used for shell stuff more often. The reason we don't see Ruby used more for shell stuff is because Python won this particular war. It's already installed on basically every Linux distribution out there, and this simple fact outweighs all other language considerations for probably >95% of people who are writing shell scripts in something that isn't Bash. Personally, I don't m…

> It's already installed on basically every Linux distribution out there, PEP 668 pretty much negates this though. To do anything you need a python environment set up per script/project w/e

Even if you don't want to limit yourself to the stdlib, you can still use a zipapp : https://docs.python.org/3/library/zipapp.html

Re: Ruby: A great language for shell scripts

#214

Earlier quoted context omitted.

Golang's not so secret weapon

[flagged]

Every compiled language can do it until you run into issues with glibc vs musl or openssl version or network stack defaults and remember you weren't as static as you thought.

Re: Ruby: A great language for shell scripts

#215

It seems like a waste of precious syntax to dedicate backticks to running shell commands. What's between the backticks is not even portable; the commands rely on an operating-system-specific command interpreter. > puts `ls`.lines.map { |name| name.strip.length } # prints the lengths of the filenames Fantastic example, except for the commandment violation: "thou shalt not parse the output of 'ls'"! You really want to…

> You really want to read the directory and map over the stat function (or equivalent) to get the length.

Indeed. Equivalent in Ruby:

    Dir["*"].map { |x| File.size(x) }

Re: Ruby: A great language for shell scripts

#216

It seems like a waste of precious syntax to dedicate backticks to running shell commands. What's between the backticks is not even portable; the commands rely on an operating-system-specific command interpreter. > puts `ls`.lines.map { |name| name.strip.length } # prints the lengths of the filenames Fantastic example, except for the commandment violation: "thou shalt not parse the output of 'ls'"! You really want to…

> You really want to read the directory and map over the stat function (or equivalent) to get the length. Indeed. Equivalent in Ruby: Dir["*"].map { |x| File.size(x) }

I avoided

  (flow (glob "*") (mapcar [chain stat .size]))
because * skips entries starting with dot, which sends us down a certain distracting rabbit hole.

Re: Ruby: A great language for shell scripts

#217
post #9

I sometimes wonder why we don't see ruby used for shell stuff more often. It inherited most of the good stuff for shell scripting from Perl, and Perl took a lot of it's syntax from sh and sed and awk, so almost anything you can do in shell script you can do in ruby, but with an option of making it gradually less terse and more readable, while having sane variables and data handling from the start. Also ruby is great…

because there's Python

Re: Ruby: A great language for shell scripts

#218
post #108
post #9

I sometimes wonder why we don't see ruby used for shell stuff more often. It inherited most of the good stuff for shell scripting from Perl, and Perl took a lot of it's syntax from sh and sed and awk, so almost anything you can do in shell script you can do in ruby, but with an option of making it gradually less terse and more readable, while having sane variables and data handling from the start. Also ruby is great…

> I sometimes wonder why we don't see ruby used for shell stuff more often. The reason we don't see Ruby used more for shell stuff is because Python won this particular war. It's already installed on basically every Linux distribution out there, and this simple fact outweighs all other language considerations for probably >95% of people who are writing shell scripts in something that isn't Bash. Personally, I don't m…

Yes, Python won the war, which is a pity. Linux distributions started getting bloated at the same time they switched to Python for everything. Yum hanging inexplicably and such things never occurred before.

The BSDs do not have this problem (yet!). I hope they stay sane and keep using Perl/sh.

Re: Ruby: A great language for shell scripts

#219
post #141

Earlier quoted context omitted.

I think the quality of a language for shell scripting is often secondary. What’s of greater significance is where it is at. I.e., does it have it already installed? The answer with Linux and Bash is almost always “yes”. Not so with ruby. The moment you start asking the user to install things, you’ve opened up the possibility for writing a program rather than a shell script. The lifecycle of a piece of software is alm…

This is indeed why I use Perl over Ruby. As long as it's not for a Window machine, a Perl script is deployed by copying it over and that's it.

Even on Windows there's a good chance. The Git for Windows project bundles Perl, but not Ruby.

Re: Ruby: A great language for shell scripts

#220

Earlier quoted context omitted.

I think golang is used because you can easily create a single static binary, which is incredibly easy to distribute. I often find non-trivial CLI tools written in Python cumbersome because of the dependency wrangling necessary.

> I often find non-trivial CLI tools written in Python cumbersome because of the dependency wrangling necessary. I'm thinking of trying out Mojo in large part because they say they're aiming for Python compatibility, and they produce single-file executables. Previous to that I was using PyInstaller but it was always a little fragile (I had to run the build script a couple of times before it would successfully complet…

Scriptisto is an underrated tool: https://github.com/igor-petruk/scriptisto

It can do the Python venv stuff behind the scenes for you and it just looks like a single Python file.

Post reply on HN