6 ms·
Show HN: Ruby One-Liners Cookbook
- asicsp 6y agoRuby 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 https://gumroad.com/l/ruby-oneliners or https://leanpub.com/ruby-oneliners https://leanpub.com/ruby-oneliners * Bundle of one-liners and Regexp: https://gumroad.com/l/ruby-textprocessing https://gumroad.com/l/ruby-textprocessing or https://leanpub.com/b/ruby-textprocessing 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://gumroad.com/l/regex/off10 * https://leanpub.com/b/regex/c/off10 https://leanpub.com/b/regex/c/off10 --- Writing a book: is it worth it? (https://news.ycombinator.com/item?id=24628549 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 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.
- tomcam 6y agoThanks for a detailed, honest, and inspiring share. That’s a pretty amazing story.
- jmarchello 6y agoI 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.
- sdwisely 6y agothe next release of Crystal will also be 1.0 too. https://crystal-lang.org/ https://crystal-lang.org/
- radiospiel 6y agoI remember they said that already 2 years ago. :(
- block_dagger 6y agoCrystal has no metaprogramming. Lang is far less useful than Ruby imo, doesn’t deserve equal comparison.
- 1337_d00dZ 6y agoIt has metaprogramming via macros: https://crystal-lang.org/reference/syntax_and_semantics/macros.html https://crystal-lang.org/reference/syntax_and_semantics/macr...
- block_dagger 6y agoTrue, 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.
- 6y ago
- save_ferris 6y agoJust FYI, the code element color is impossible to see when the page loads its dark theme. Otherwise, looks great.
- asicsp 6y agoI'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 https://github.com/rust-lang/mdBook
- save_ferris 6y agoBoth inline and code blocks appear with black text and a black background.
- asicsp 6y agoI took screenshots: https://drive.google.com/drive/folders/1bxJdI6z3XPNtRDCt1VvakLMGb6FvYatZ 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?
- save_ferris 6y agoFirefox on MacOS causes the issue, Chrome works fine.
- asicsp 6y agoThanks, probably this issue? https://github.com/rust-lang/mdBook/issues/1304 https://github.com/rust-lang/mdBook/issues/1304
- revscat 6y agoIt’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.
- burlesona 6y agoVery 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.
- mberning 6y agoI 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’.
- aledalgrande 6y agoOne feature I REALLY miss in Node is a great REPL like irb/rails c
- bdcravens 6y agoSome of these one-liners are clocking in at around 120 characters.
- asicsp 6y agoTowards the end, multiline commands are more common. I'm using "one-liners" to invoke a mental image like grep/sed/awk/perl one-liners.
- jrumbut 6y agoRuby, 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.
- jfhufl 6y agoI get a lot of mileage out of command | ruby -e 'ARGF.each { |line| #do stuff with line }'
- 3pt14159 6y agoSame, 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.
- dorianmariefr 6y agomine 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
- brigandish 6y agoYou 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 https://robm.me.uk/ruby/2013/11/20/ruby-enp.html
- asicsp 6y agoWell, 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/'
- brigandish 6y ago
- oogway8020 6y agoRuby 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