* . *
  • About
  • Advertise
  • Privacy & Policy
  • Contact
Monday, December 22, 2025
Earth-News
  • Home
  • Business
  • Entertainment
    $150 million, 12,500-seat entertainment venue coming to Houston in 2027 – CultureMap Houston

    Houston Set to Unveil a Spectacular $150 Million, 12,500-Seat Entertainment Venue in 2027

    WildBrain Sells Stake in Peanuts Holdings to Sony Pictures Entertainment – Licensing International

    WildBrain Sells Stake in Peanuts Holdings to Sony Pictures Entertainment – Licensing International

    Country music star, wife are getting divorced: ‘We are no longer suited to be married’ – PennLive.com

    Country Music Star and Spouse Reveal They Are No Longer Suited for Marriage

    Nate Bargatze is leaving his podcast — and Utah recently saw why – Deseret News

    Nate Bargatze Is Leaving His Podcast – What Utah Fans Recently Went Through

    State Farm Arena Ranks In The Top 5 Live Entertainment Venues In The U.S. & Top 7 In The World, According To Billboard – Secret Atlanta

    State Farm Arena Ranks In The Top 5 Live Entertainment Venues In The U.S. & Top 7 In The World, According To Billboard – Secret Atlanta

    Walk on White features Conchettes and Santa – keysnews.com

    Uncover the Enchantment of Conchettes and Santa in Walk on White

  • 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
    Technology Stocks Week Ahead: AI Spending Scrutiny, Fed Rate Path, and Holiday-Thin Trading to Drive Tech Stocks (Dec. 22–26, 2025) – ts2.tech

    Tech Stocks Outlook for Dec. 22-26, 2025: AI Investments, Fed Rate Moves, and Holiday-Thin Trading to Drive Market Action

    Technology is powerful but unforgiving when misused – Supreme Court judge warns – GhanaWeb

    Supreme Court Judge Issues Stark Warning: Technology’s Power Can Be Dangerous When Misused

    The 8 worst technology flops of 2025 – MIT Technology Review

    The 8 worst technology flops of 2025 – MIT Technology Review

    Bangor School District receives new CNC router technology from First National Bank – news8000.com

    Bangor School District Unveils Cutting-Edge CNC Router Technology Thanks to Local Support

    6G discussions: How things have changed – 5gtechnologyworld.com

    The Evolution of 6G: How the Conversation Has Transformed

    Retail supply chains brace for a redefined 2026 as tariffs, technology gaps, and nearshoring upend old models – Raleigh News & Observer

    Retail Supply Chains Revolutionize in 2026: How Tariffs, Technology Gaps, and Nearshoring Are Shaping the Future

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
No Result
View All Result
  • Home
  • Business
  • Entertainment
    $150 million, 12,500-seat entertainment venue coming to Houston in 2027 – CultureMap Houston

    Houston Set to Unveil a Spectacular $150 Million, 12,500-Seat Entertainment Venue in 2027

    WildBrain Sells Stake in Peanuts Holdings to Sony Pictures Entertainment – Licensing International

    WildBrain Sells Stake in Peanuts Holdings to Sony Pictures Entertainment – Licensing International

    Country music star, wife are getting divorced: ‘We are no longer suited to be married’ – PennLive.com

    Country Music Star and Spouse Reveal They Are No Longer Suited for Marriage

    Nate Bargatze is leaving his podcast — and Utah recently saw why – Deseret News

    Nate Bargatze Is Leaving His Podcast – What Utah Fans Recently Went Through

    State Farm Arena Ranks In The Top 5 Live Entertainment Venues In The U.S. & Top 7 In The World, According To Billboard – Secret Atlanta

    State Farm Arena Ranks In The Top 5 Live Entertainment Venues In The U.S. & Top 7 In The World, According To Billboard – Secret Atlanta

    Walk on White features Conchettes and Santa – keysnews.com

    Uncover the Enchantment of Conchettes and Santa in Walk on White

  • 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
    Technology Stocks Week Ahead: AI Spending Scrutiny, Fed Rate Path, and Holiday-Thin Trading to Drive Tech Stocks (Dec. 22–26, 2025) – ts2.tech

    Tech Stocks Outlook for Dec. 22-26, 2025: AI Investments, Fed Rate Moves, and Holiday-Thin Trading to Drive Market Action

    Technology is powerful but unforgiving when misused – Supreme Court judge warns – GhanaWeb

    Supreme Court Judge Issues Stark Warning: Technology’s Power Can Be Dangerous When Misused

    The 8 worst technology flops of 2025 – MIT Technology Review

    The 8 worst technology flops of 2025 – MIT Technology Review

    Bangor School District receives new CNC router technology from First National Bank – news8000.com

    Bangor School District Unveils Cutting-Edge CNC Router Technology Thanks to Local Support

    6G discussions: How things have changed – 5gtechnologyworld.com

    The Evolution of 6G: How the Conversation Has Transformed

    Retail supply chains brace for a redefined 2026 as tariffs, technology gaps, and nearshoring upend old models – Raleigh News & Observer

    Retail Supply Chains Revolutionize in 2026: How Tariffs, Technology Gaps, and Nearshoring Are Shaping the Future

    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

A Lifestyle Rx For Keeping Your Brain Young – Indiana Gazette Online

Unlock the Secret to a Youthful, Sharp Brain with This Lifestyle Rx

December 21, 2025
Technology Stocks Week Ahead: AI Spending Scrutiny, Fed Rate Path, and Holiday-Thin Trading to Drive Tech Stocks (Dec. 22–26, 2025) – ts2.tech

Tech Stocks Outlook for Dec. 22-26, 2025: AI Investments, Fed Rate Moves, and Holiday-Thin Trading to Drive Market Action

December 21, 2025
Chargers lead Cowboys 21-17 at halftime – Yahoo Sports

Chargers Surge Ahead with a 21-17 Lead Over Cowboys at Halftime

December 21, 2025
World’s Calmest Stock Market Challenges Options Traders in India – Bloomberg.com

India’s Unstoppable Stock Market Leaves Options Traders Scratching Their Heads

December 21, 2025
The cash bazooka: Why Trump wants to send you money – Axios

The Cash Bazooka: How Trump Plans to Put Money Straight into Your Hands

December 21, 2025
$150 million, 12,500-seat entertainment venue coming to Houston in 2027 – CultureMap Houston

Houston Set to Unveil a Spectacular $150 Million, 12,500-Seat Entertainment Venue in 2027

December 21, 2025
Editorial: America’s looming health care crisis – Times Union

America’s Urgent Health Care Crisis: What Everyone Must Understand Today

December 21, 2025
Dismissing politics as ‘dirty’ is wrong and self-defeating – The Republic News

Why Labeling Politics as ‘Dirty’ Is a Dangerous Misstep That Harms Us All

December 21, 2025
Opinion — Eric Sorenson, Brett Engstrom, and Liz Thompson: We need more wild forests and ecological forestry. – VTDigger

Why We Must Protect and Expand Wild Forests Through Ecological Forestry

December 21, 2025
Scientists at the American Museum of Natural History discovered more than 70 new species in 2025 – Phys.org

Discover Over 70 Thrilling New Species Uncovered in 2025 by Top Scientists

December 21, 2025

Categories

Archives

December 2025
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Nov    
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 (980)
  • Economy (999)
  • Entertainment (21,876)
  • General (18,873)
  • Health (10,039)
  • Lifestyle (1,012)
  • News (22,149)
  • People (1,005)
  • Politics (1,013)
  • Science (16,214)
  • Sports (21,500)
  • Technology (15,982)
  • World (988)

Recent News

A Lifestyle Rx For Keeping Your Brain Young – Indiana Gazette Online

Unlock the Secret to a Youthful, Sharp Brain with This Lifestyle Rx

December 21, 2025
Technology Stocks Week Ahead: AI Spending Scrutiny, Fed Rate Path, and Holiday-Thin Trading to Drive Tech Stocks (Dec. 22–26, 2025) – ts2.tech

Tech Stocks Outlook for Dec. 22-26, 2025: AI Investments, Fed Rate Moves, and Holiday-Thin Trading to Drive Market Action

December 21, 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