Live data from Hacker News

Ruby: A great language for shell scripts

lucasoshiro.github.io

181–190 of 368 posts

Re: Ruby: A great language for shell scripts

#181
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 best piece of code that I worked on was an ETL in pure Ruby. Everything in modules, simply to read, no crazy abstractions, strange things like __main__, abstract clssses or whatever.

Maybe others can chime in, but the main difference that is found in ruby developers is that they really have fun with the language making everything with a higher lever of software craftsmanship that other folks in the data space, e.g. Python of Julia.

Re: Ruby: A great language for shell scripts

#182
I have what probably sounds like a niche use case, where most of the boxes I work on don't have access to the Internet.

So for me, "is it installed in the base distribution" is the difference between being able to start immediately no matter which box I'm concerned with, and spending months trying to upstream a new program to be installed with our OS image team.

I took a look around a vanilla Debian 12 box, and didn't see Ruby in there [1]. So, sadly, although I really like the way Ruby looks, I'm going to have to stick with Bash and Python 3 for the hard stuff.

[1]: https://hiandrewquinn.github.io/til-site/posts/what-programm...

Re: Ruby: A great language for shell scripts

#183
While I write most of my scripts in Ruby and enjoy doing so, there is one gripe I have with it: its slow start-up time. On my machine, running an empty Ruby script takes about 100ms, compared to One can mitigate the problem somewhat using the `--disable-gems` flag, but that's not a good general solution.

Re: Ruby: A great language for shell scripts

#184
post #119

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…

They have inherited the second biggest mistake of shellscripts; Requiring the user to manually check $? after each command. No thanks. Anything that doesn't have error handling enabled by default goes straight in the trash bin.

This is only true for backticks, which are somewhat intended for non-serious use. If you want exceptions for subprocess failure, `system` does the trick.

There’s really no need for this kind of over-the-top response.

Re: Ruby: A great language for shell scripts

#185
post #129

Ruby's a great language- I've always enjoyed its ergonomics and clarity. But its editor tooling hasn't kept up with its one-time competitor, Python. I've mostly been in the Python ecosystem for the past few years and the LSP investment from Microsoft has really shown. Rich Python support in VSCode is seamless and simple. Coming back to Ruby after that caught me off guard - it feels like I'm writing syntax-highlighted…

Funny, I have the complete opposite experience with python. Constantly turned off inline errors and warnings because they were almost always wrong - packages I installed “could not be found” by the LSP, it constantly worried about type issues that were no longer incorrect, it didn’t pick up function changes across files, etc etc.

Then you think “maybe I just have the wrong lsp” only to realize there are half a dozen that all behave differently and nobody can agree on.

I tried them all, they all turned even my simplest of scripts between 10-50% red. I think half of my ire towards python came from the fact that the LSP situation was so awful, I just had to get used to reading code with a bunch of “errors” in my face, or turn them off completely… I could never decide which was worse

Re: Ruby: A great language for shell scripts

#187

While I write most of my scripts in Ruby and enjoy doing so, there is one gripe I have with it: its slow start-up time. On my machine, running an empty Ruby script takes about 100ms, compared to One can mitigate the problem somewhat using the `--disable-gems` flag, but that's not a good general solution.

100ms !!!! wtf? :)

[borg@cube] time ruby -e 'nil'

real 0m0.007s

Re: Ruby: A great language for shell scripts

#188

Not denying Ruby is good, but other than process forking, why should I prefer it to Python?

Personally, I find Ruby's syntax more natural, (I'm going to be heavily biased though, having written Ruby for 10+ years). But for example, let's say I wanted to make a hash (dict) of files, keyed by their size (for some unknown reason), in Ruby it would look like:

  Pathname.glob('*').filter { |f| f.file? }.each_with_object({}) { |f, h| h[f.size] = f }
Whereas the equivalent Python would be:

  result = {}
  for file_path in glob.glob('*'):
    if os.path.isfile(file_path):
      result[os.path.getsize(file_path)] = file_path
Or capitalizing a string in Ruby:

  string.split(' ').map(&:capitalize).join(' ')
And in Python:

  words = string.split(' ')
  capitalized_words = [word.capitalize() for word in words]
  result = ' '.join(capitalized_words)
Python seems to be more convoluted and verbose to me, and requires more explicit variable declarations too. With the Ruby you can literally read left to right and know what it does, but with the Python, I find I have to jump about a bit to grok what's going on. But maybe that's just my lack of Python experience showing.

Re: Ruby: A great language for shell scripts

#189
post #187

While I write most of my scripts in Ruby and enjoy doing so, there is one gripe I have with it: its slow start-up time. On my machine, running an empty Ruby script takes about 100ms, compared to One can mitigate the problem somewhat using the `--disable-gems` flag, but that's not a good general solution.

100ms !!!! wtf? :) [borg@cube] time ruby -e 'nil' real 0m0.007s

Whoa, interesting... What version are you running? Here's my system:

  $ ruby -v
  ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux]

  $ time ruby -e ''

  real 0m0.122s
  user 0m0.102s
  sys 0m0.020s
I found an old Reddit thread also hinting at bad start-up times: https://old.reddit.com/r/ruby/comments/aqxepw/rubys_startup_....

Re: Ruby: A great language for shell scripts

#190
post #28

Earlier quoted context omitted.

This complaint comes up enough that I'm surprised nobody's created the Ruby equivalent of GraalVM, to compile a Ruby script, all its deps, and a WPOed subset of the Ruby runtime, into a native executable.

It's not quite what you're describing, but TruffleRuby is Ruby on GraalVM: https://github.com/oracle/truffleruby Unlike GraalVM Java, as far as I can tell TruffleRuby doesn't provide a bundler that can create a single executable out of everything, but in principle I don't see why it couldn't.

Graal can create an executable from a ruby program as well with TruffleRuby and native image (both part of the general graal project).
Post reply on HN