* . *
  • About
  • Advertise
  • Privacy & Policy
  • Contact
Wednesday, October 15, 2025
Earth-News
  • Home
  • Business
  • Entertainment
    Bluesman James Montgomery Will Perform In Falmouth – CapeNews.net

    Blues Legend James Montgomery Ready to Ignite the Stage in Falmouth

    Mexican singer Pedro Fernández to make Ave Fénix tour stop in Stockton. Tickets, schedule – Yahoo

    Mexican Singer Pedro Fernández Brings the Ave Fénix Tour to Stockton – Don’t Miss It!

    Flutter Entertainment’s SWOT Analysis: Uncovering the Growth Potential Amid Challenges

    Dylan Efron Shares Sweet ‘DWTS’ Rehearsal Photos Featuring His Little Sister Olivia – yahoo.com

    Dylan Efron’s Heartwarming ‘DWTS’ Rehearsal Moments with Little Sister Olivia

    Diane Keaton, Oscar-Winning Star of ‘Annie Hall’ and ‘The Godfather,’ Dies at 79 – Yahoo

    Diane Keaton, Oscar-Winning Star of ‘Annie Hall’ and ‘The Godfather,’ Dies at 79 – Yahoo

    THE VAMPIRE LESTAT Shares First Look Teaser Trailer (And It’s Fangtastic) – Yahoo

    THE VAMPIRE LESTAT Shares First Look Teaser Trailer (And It’s Fangtastic) – Yahoo

  • General
  • Health
  • News

    Cracking the Code: Why China’s Economic Challenges Aren’t Shaking Markets, Unlike America’s” – Bloomberg

    Trump’s Narrow Window to Spread the Truth About Harris

    Trump’s Narrow Window to Spread the Truth About Harris

    Israel-Gaza war live updates: Hamas leader Ismail Haniyeh assassinated in Iran, group says

    Israel-Gaza war live updates: Hamas leader Ismail Haniyeh assassinated in Iran, group says

    PAP Boss to Niger Delta Youths, Stay Away from the Protest

    PAP Boss to Niger Delta Youths, Stay Away from the Protest

    Court Restricts Protests In Lagos To Freedom, Peace Park

    Court Restricts Protests In Lagos To Freedom, Peace Park

    Fans React to Jazz Jennings’ Inspiring Weight Loss Journey

    Fans React to Jazz Jennings’ Inspiring Weight Loss Journey

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Science
  • Sports
  • Technology
    Day 5 of Gains Streak for Solidion Technology Stock with 475% Return (vs. -20% YTD) [10/14/2025] – Trefis

    Solidion Technology Stock Rockets for 5th Consecutive Day, Soaring an Astonishing 475% Year-to-Date

    Tracking DNA and RNA Together To Unlock Disease Insights – Technology Networks

    Unlocking Disease Insights by Tracking DNA and RNA Together

    The future of battery technology – Engineer Live

    Revolutionizing Energy: Exploring the Future of Battery Technology

    How Can Boosting Your Travel Experience with Less Technology Lead to a More Relaxing Vacation? All You Need to Know About This Latest Trend – Travel And Tour World

    How Can Boosting Your Travel Experience with Less Technology Lead to a More Relaxing Vacation? All You Need to Know About This Latest Trend – Travel And Tour World

    Davenport CornCon Cybersecurity Conference helps students explore technology, AI use – KWQC

    Davenport CornCon Cybersecurity Conference Ignites Student Passion for Technology and AI Innovations

    Inside Europe’s military technology resurgence – NBC News

    Europe’s Bold Comeback: Unveiling the Rise of Cutting-Edge Military Technology

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
No Result
View All Result
  • Home
  • Business
  • Entertainment
    Bluesman James Montgomery Will Perform In Falmouth – CapeNews.net

    Blues Legend James Montgomery Ready to Ignite the Stage in Falmouth

    Mexican singer Pedro Fernández to make Ave Fénix tour stop in Stockton. Tickets, schedule – Yahoo

    Mexican Singer Pedro Fernández Brings the Ave Fénix Tour to Stockton – Don’t Miss It!

    Flutter Entertainment’s SWOT Analysis: Uncovering the Growth Potential Amid Challenges

    Dylan Efron Shares Sweet ‘DWTS’ Rehearsal Photos Featuring His Little Sister Olivia – yahoo.com

    Dylan Efron’s Heartwarming ‘DWTS’ Rehearsal Moments with Little Sister Olivia

    Diane Keaton, Oscar-Winning Star of ‘Annie Hall’ and ‘The Godfather,’ Dies at 79 – Yahoo

    Diane Keaton, Oscar-Winning Star of ‘Annie Hall’ and ‘The Godfather,’ Dies at 79 – Yahoo

    THE VAMPIRE LESTAT Shares First Look Teaser Trailer (And It’s Fangtastic) – Yahoo

    THE VAMPIRE LESTAT Shares First Look Teaser Trailer (And It’s Fangtastic) – Yahoo

  • General
  • Health
  • News

    Cracking the Code: Why China’s Economic Challenges Aren’t Shaking Markets, Unlike America’s” – Bloomberg

    Trump’s Narrow Window to Spread the Truth About Harris

    Trump’s Narrow Window to Spread the Truth About Harris

    Israel-Gaza war live updates: Hamas leader Ismail Haniyeh assassinated in Iran, group says

    Israel-Gaza war live updates: Hamas leader Ismail Haniyeh assassinated in Iran, group says

    PAP Boss to Niger Delta Youths, Stay Away from the Protest

    PAP Boss to Niger Delta Youths, Stay Away from the Protest

    Court Restricts Protests In Lagos To Freedom, Peace Park

    Court Restricts Protests In Lagos To Freedom, Peace Park

    Fans React to Jazz Jennings’ Inspiring Weight Loss Journey

    Fans React to Jazz Jennings’ Inspiring Weight Loss Journey

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Science
  • Sports
  • Technology
    Day 5 of Gains Streak for Solidion Technology Stock with 475% Return (vs. -20% YTD) [10/14/2025] – Trefis

    Solidion Technology Stock Rockets for 5th Consecutive Day, Soaring an Astonishing 475% Year-to-Date

    Tracking DNA and RNA Together To Unlock Disease Insights – Technology Networks

    Unlocking Disease Insights by Tracking DNA and RNA Together

    The future of battery technology – Engineer Live

    Revolutionizing Energy: Exploring the Future of Battery Technology

    How Can Boosting Your Travel Experience with Less Technology Lead to a More Relaxing Vacation? All You Need to Know About This Latest Trend – Travel And Tour World

    How Can Boosting Your Travel Experience with Less Technology Lead to a More Relaxing Vacation? All You Need to Know About This Latest Trend – Travel And Tour World

    Davenport CornCon Cybersecurity Conference helps students explore technology, AI use – KWQC

    Davenport CornCon Cybersecurity Conference Ignites Student Passion for Technology and AI Innovations

    Inside Europe’s military technology resurgence – NBC News

    Europe’s Bold Comeback: Unveiling the Rise of Cutting-Edge Military Technology

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
No Result
View All Result
Earth-News
No Result
View All Result
Home Technology

Ruby: A great language for shell scripts

June 23, 2024
in Technology
Ruby: A great language for shell scripts
Share on FacebookShare on Twitter

4 minute read

Intro

Ruby is so associated with its most famous framework, Rails, that many people
forget how amazing this language is. I mean, I know a lot of people who says “I
don’t like Ruby” and when I ask why, they say something about Rails. Personally,
I consider Ruby one of my favorite programming languages, and the last time I
touched any Rails code was 7 years ago…

So, if I don’t use Rails anymore, what I do with Ruby? Well, Ruby is a very rich
and complete language, perhaps even more than its more famous relative, Python
(sadly, I can’t say the same about its ecosystem…). And one of the things that
I think that Ruby is better than Python is using it for writing shell
scripts.

That is, most of the cases Bash for me is enough, but if the script starts to
become complex, I switch to Ruby. Here I show the main features that might be
interesting for this case of use.

Goals

Show features of Ruby that are useful for writing shell scripts;

Compare Ruby to Bash and Python;

Non-goals

Replace entirely Bash scripts by Ruby scripts.

Feature 1: calling external commands

The first thing that you expect of language for writing shell scripts is to
call external commands. In Ruby, you do that using backticks (`):

That’s it! You don’t need system, popen or something like that, or import
a library. And if you set that to a variable, you’ll have the output of the
command:

Note: if you want to use system (e.g. if you want the output to be
redirected to stdout instead of a string) or popen (if you want to read or
write data from or to a subprocess), those are also available in Ruby!

Feature 2: status code

This is real quick: in Ruby, the variable $? contains the status code of the
last executed command. So, it’s really close to Bash:

`true`
puts $? # 0

`false`
puts $? # 1

Feature 3: it’s a typed language

Ruby is not a statically typed language, but it has types. In fact, it is a
object-oriented language, and it follow strictly the OOP paradigm (more than
Python, in some aspects even more than Java!). Bash, on the other hand,
everything is a string, and that leads to several safety issues…

total_lines=`wc -l my_file`.to_i # an int containing the number of lines of a file
half=total_lines.div 2 # integer division
puts `head -n #{half} my_file` # print half of the file

Feature 4: functional constructions

Ruby implements map, select (filter), reduce, flat_map and other
functional operations as methods. So, you can, for example, apply a map over a
command output:

puts `ls`.lines.map { |name| name.strip.length } # prints the lengths of the filenames

Note for Git lovers: I know that I could do that only using git branch
–show-current, but that was the first example that came in my mind to
demonstrate the use of regex…

Feature 5: regex matching

Regex is a type in Ruby, and operations using regex are built-in in the
language. Look at this example, where we get the current git branch name calling
git branch:

current_branch_regex=/^* (S+)/
output_lines=`git branch`.lines
output_lines.each do |line|
if line=~ current_branch_regex # match the string with the regex
puts $1 # prints the match of the first group
end
end

Feature 6: easy threads

If want to work with multiple threads, Ruby is perhaps the one of the easiest
language to do it. Look:

thread=Thread.new do
puts “I’m in a thread!”
end

puts “I’m outside a thread!”

thread.join

So, it can be useful for, for example, downloading several files at the same time:

(1..10).map do |i| # iterates from i=1 to i=10, inclusive
Thread.new do
`wget http://my_site.com/file_#{i}` # you can use variables inside commands!
end
end.each { |thread| thread.join } # do/end and curly braces have the same purpose!

Feature 7: builtin file and dir operations

In Ruby, all the file operations are methods of the File class and all the
directory operations are methods of the Dir class, as it should be. In Python,
for example, if you want to read a file you use open, but if you want to
delete it you need to use os.remove, and os does a lot of other things that are
not related to files.

So, in Ruby:

exists=File.exists? ‘My File’ # methods that return booleans end in ?
file_content=File.open(‘My File’).read
File.delete ‘My File’ # parentheses are optional if it’s not ambiguous

Conclusion

I hope that after reading this short text you consider using Ruby as a
replacement for complex shell scripts. I mean, I don’t expect that you drop
Bash entirely, but consider using Ruby when things get complex. Of course, you
can do that in Python, Perl, even JS, but, as my personal choice I think that
Ruby is the most complete and easier Bash replacement for that!

If you find something wrong, or if you have any suggestion, please let me know
here.

>>> Read full article>>>
Copyright for syndicated content belongs to the linked Source : Hacker News – https://lucasoshiro.github.io/posts-en/2024-06-17-ruby-shellscript/

Tags: languageShelltechnology
Previous Post

OpenEMR: Open-source medical record software

Next Post

The manager’s unbearable lack of endorphins

Back to school shopping? – Washington State Department of Ecology (.gov)

Back to school shopping? – Washington State Department of Ecology (.gov)

October 15, 2025
Alltech breaks ground on new crop science facility in Nicholasville – LEX18

Alltech Unveils Cutting-Edge Crop Science Facility in Nicholasville

October 15, 2025
SVC obtains $150,000 National Science Foundation grant – Saint Vincent College

SVC Lands $150,000 Grant to Fuel Innovative Research

October 15, 2025
Zhaobangji Lifestyle Holdings Approves Name Change at EGM – TipRanks

Zhaobangji Lifestyle Holdings Reveals Exciting New Name After EGM Approval

October 15, 2025
Day 5 of Gains Streak for Solidion Technology Stock with 475% Return (vs. -20% YTD) [10/14/2025] – Trefis

Solidion Technology Stock Rockets for 5th Consecutive Day, Soaring an Astonishing 475% Year-to-Date

October 15, 2025
2026 NFL mock draft: Steelers’ strategy post-Aaron Rodgers, Cowboys eye Micah Parsons replacement – CBS Sports

2026 NFL Mock Draft: Steelers’ Bold Moves Following Aaron Rodgers, Cowboys Eye Micah Parsons’ Successor

October 15, 2025
Preview: could Premier League fitness make the difference in recurve men? – World Archery

Could Premier League Fitness Transform the Future of Recurve Archery?

October 15, 2025
The $2.5 trillion ocean economy is at a crossroads. Capital must act now – Fortune

The $2.5 Trillion Ocean Economy at a Crossroads: Why Urgent Capital Investment Is Essential

October 15, 2025
Bluesman James Montgomery Will Perform In Falmouth – CapeNews.net

Blues Legend James Montgomery Ready to Ignite the Stage in Falmouth

October 15, 2025
30-year decline in Kansas health can be reversed with leadership, report finds – Kansas Reflector

30-year decline in Kansas health can be reversed with leadership, report finds – Kansas Reflector

October 15, 2025

Categories

Archives

October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Sep    
Earth-News.info

The Earth News is an independent English-language daily published Website from all around the World News

Browse by Category

  • Business (20,132)
  • Ecology (868)
  • Economy (889)
  • Entertainment (21,761)
  • General (17,604)
  • Health (9,931)
  • Lifestyle (902)
  • News (22,149)
  • People (890)
  • Politics (899)
  • Science (16,100)
  • Sports (21,389)
  • Technology (15,869)
  • World (872)

Recent News

Back to school shopping? – Washington State Department of Ecology (.gov)

Back to school shopping? – Washington State Department of Ecology (.gov)

October 15, 2025
Alltech breaks ground on new crop science facility in Nicholasville – LEX18

Alltech Unveils Cutting-Edge Crop Science Facility in Nicholasville

October 15, 2025
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2023 earth-news.info

No Result
View All Result

© 2023 earth-news.info

No Result
View All Result

© 2023 earth-news.info

Go to mobile version