Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Ruby One-Liners Cookbook (learnbyexample.github.io)
191 points by asicsp on Sept 30, 2020 | hide | past | favorite | 36 comments



Ruby is based on programming languages like Perl, Smalltalk, Eiffel, Ada, and Lisp. Many cli options are same as Perl and behave similarly too. While Ruby would be slower compared to sed/awk/perl, I find it a pleasure to use Ruby's built-in array/enumerable methods, blocks, etc.

---

The post links to the web version of the ebook. You can download pdf/epub versions using the links below (free until this Sunday)

* Ruby one-liners cookbook: https://gumroad.com/l/ruby-oneliners or https://leanpub.com/ruby-oneliners

* Bundle of one-liners and Regexp: https://gumroad.com/l/ruby-textprocessing or https://leanpub.com/b/ruby-textprocessing

Also, my 6 book bundle (regular expressions and grep/ripgrep/sed/awk) is priced $5 until Sunday:

* https://gumroad.com/l/regex/off10

* https://leanpub.com/b/regex/c/off10

---

Writing a book: is it worth it? (https://news.ycombinator.com/item?id=24628549)

For my unique circumstances, emphatic yes. I left my job more than 6 years back for various reasons. I tried a few things that didn't work out.

Before I left the job, I had been conducting yearly workshops at my college for electronics students to get started with command line, vim, perl/python, etc. For reference material, I used provide a few slides. As something to keep me mentally occupied, I started with word document, then moved to GitHub/Gitbook. My awk tutorial (https://news.ycombinator.com/item?id=15549318) hit front page and combined with sharing on other social sites, I started getting decent traffic to my GitHub repos. My primary aim with these resource materials was to share with workshop students. Since I had failed to earn outside of workshops and my savings were dwindling, I tried putting donation button on GitHub (sponspor wasn't introduced at that time). Didn't work out.

Finally, about two years back, kinda like a last try before I was forced to look for a job again, I self-published Ruby Regexp book. Didn't earn much, but couple of months later, I used this material to publish Python regex book. This was received much better, got me about $250 in initial sales.

I live alone in outskirts of a city in India, and my modest lifestyle needs about $150 per month to cover my living expenses. Last year, ebook sales and workshops nearly covered my expenses. This year, pandemic put a stop to my workshops. But, ebook sales has more than covered for it so far.

I have a sustainable income again. About 20k free copies given away and all my books are now free to read online. It is worth it.


Thanks for a detailed, honest, and inspiring share. That’s a pretty amazing story.


I recently picked Ruby up again after about a year of JS and Python. I instantly fell in live again due to how simple it is to do anything with Ruby.

Rails is a breath of fresh air after trying to maintain hand-rolled solutions. The productivity gains are incredible. With Ruby 3 coming soon it seems like the performance of the language has drastically improved. I’m excited to see ruby continue to flourish.


It's hard not to fall in love with Ruby. Since 2010 I've also dabbed into Javascript, Go, Elixir, Rust --work in progress, damned lifetimes!-- but as the song goes, Ruby was my first programming love and will be my last.


And mind you, i've seen and written my good share of crap Ruby code! It's not about the quality of the code or the fit-for-purposeness of the language or platform, it's about the joy of writing code in it. Totally subjective.


Ruby is the only language that says, "nevermind all that other stuff - Developer Happiness." (And, "Matz Is Nice So We Are Nice")

It comes through in so many tiny ways!


the next release of Crystal will also be 1.0 too.

https://crystal-lang.org/


Crystal is a great language that has learned a lot from Ruby. I'm excited to see where it goes. I think one area where it doesn't measure up to Ruby, however, is Windows support, which they've decided to forgo for the 1.0 milestone [0].

That said, I'm excited to pick up Crystal someday. What are some good niches you've found for it?

[0]: https://crystal-lang.org/2020/03/03/towards-crystal-1.0.html...


I remember they said that already 2 years ago. :(


Crystal has no metaprogramming. Lang is far less useful than Ruby imo, doesn’t deserve equal comparison.



True, but one of the selling points of Ruby to begin with is runtime metaprogramming. Sure, it should generally be discouraged, but when it's heavily utilized by ecosystems like Rails, it becomes a primary feature that macros do not adequately address.


Are you saying it's a primary feature for Rails or for Ruby? I can't think of an example where it's a primary feature for a user of Rails but I usually avoid it in favour of better tools so I might not know of them. Perhaps you know of some?


Rails leans on meta programming for a lot of its magic (though less so than it used to in early versions). You don’t generally use it as a user of Rails, though.


Then I don't see how they're any better than macros in that situation.


It does have metaprogramming, only at compile time. Crystal metaprogramming is very advanced as well, you could even create methods dynamically from a JSON file if you wanted to.


Very similar to this idea, simply using ‘irb’ for one-off data munging and simple automations is very handy. That’s one of my go to tools when I am not sure exactly what I want to do with the data, and being able to poke at the intermediate steps as I transform it is handy. If I then come up with a pipeline that is useful, it only takes a minute to copy paste what I figured out in IRB to a file and save it as an executable script for future use.


I do this all the time. People are always amazed at what can be accomplished in 5 minutes in an irb session. It can be your ‘secret weapon’.


One feature I REALLY miss in Node is a great REPL like irb/rails c


It’s fascinating to me that the mindshare that was once taken up by Perl seems to slowly but surely be replaced by Ruby. This is a positive, IMO. Compared to Perl, Ruby is easier to write and — more importantly — read.


I get a lot of mileage out of

    command | ruby -e 'ARGF.each { |line| #do stuff with line }'


Same, but I was doing it so often I made an alias to a script in my dotfiles folder.

   ruby_code = ARGV[0]

    loop do
      line = $stdin.gets()
      l = line
      break unless line
      line.chomp!
      final_ruby_code = 'puts "' + ruby_code + '"'
      eval(final_ruby_code, binding())
    end
Invoked like this:

    echo foo | rg "The double do: #{l * 2}"
To print:

   The double do: foofoo
Extremely useful when data munging around.


mine is called ruby-each-line:

    #!/usr/bin/env ruby

    if ARGV.size != 1
      puts "USAGE: ruby-each-line CODE"
      exit
    end

    STDIN.each_line do |line|
      line = line.strip
      l = line
      eval(ARGV.first)
    end


You can cut that back a bit with the '-n' switch. Using an example from this helpful article[1] on some of the switches Ruby inherited from Perl:

    ps ax | ruby -e 'ARGF.each { |line| puts line.split.first if line =~ /top/ }'
becomes:

    ps ax | ruby -ne 'puts $_.split.first if $_ =~ /top/'
It even squashes the extra newline the ARGF version prints out first.

[1] https://robm.me.uk/ruby/2013/11/20/ruby-enp.html


Well, my book linked in this post is all about using Ruby from cli, with Perl like switches.

    ps ax | ruby -ane 'puts $F[0] if /top/'


Nice, I didn't know about autosplit.


Ruby, like Perl, is really built to be a part of a *nix system and has some bash elements (like special variables).

It's a natural fit for one-liners.


Ruby brings me joy of programming. Like this morning, doing a report for year, month and getting the last day of the month is absolute genius: Date.new(year, month, -1) # wow


Just FYI, the code element color is impossible to see when the page loads its dark theme. Otherwise, looks great.


I'm using mdBook [0] and haven't changed the default theme settings. Could you clarify that by 'code element' you mean the code blocks or inline code? I think you mean inline code for 'Coal' and 'Navy' themes, because for other combinations I'm able to see them properly.

[0] https://github.com/rust-lang/mdBook


Both inline and code blocks appear with black text and a black background.


I took screenshots: https://drive.google.com/drive/folders/1bxJdI6z3XPNtRDCt1Vva...

If these are readable, then may be your browser+os combo or something else might be the issue? Could you try on a different browser if possible?


Firefox on MacOS causes the issue, Chrome works fine.



Some of these one-liners are clocking in at around 120 characters.


Towards the end, multiline commands are more common. I'm using "one-liners" to invoke a mental image like grep/sed/awk/perl one-liners.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: