Question: ruby vs. ksh


(David Douthitt) #1

I don’t feel I’ve been giving Ruby its fair share :slight_smile:

Since I’ve been writing in ksh for a lot more years (even before I used it as my interactive shell!), I tend to do things in ksh. I’d like to start using Ruby more.

So, along these lines, I have a question:

Why should I use (or when should I use) ruby over ksh?

(Yes, I mean it :slight_smile:

PS: How do I do mkdir -p in ruby?


(Philip Mak) #2

Why should I use (or when should I use) ruby over ksh?

If you’re doing something that involves a lot of shell commands and
doesn’t need complicated parsing, ksh might be better. Take this webpage,
for example:

http://lina.aaanime.net/

It is a page that shows the current status (uptime, free, df -k) of my
machine. It’s written in #!/bin/sh since I can directly call the
status commands from that. It used to be in Perl, until I realized I
was just making a bunch of system() calls anyway.

PS: How do I do mkdir -p in ruby?

Maybe this:

begin
Dir.mkdir path
rescue Errno::EEXIST
# ignore error if directory exists
end

That simulates the “do nothing if directory already exists” part of
"mkdir -p". It doesn’t simulate the “make all parent directories if
necessary” part, though. If you need that too, you might use
path.split(’/’).each somehow to iterate through all the parent
directories. I don’t know if there’s an easy one-liner that does
"mkdir -p"; I’ll let someone else answer that.

You could system(“mkdir -p #{path}”) as a quick and easy hack, but
make sure ‘path’ is secured. If a hacker can feed “; rm -rf /” to that
variable, it would screw you up.

···

On Tue, Jul 02, 2002 at 08:20:11AM +0900, David Douthitt wrote:


(Tim Hammerquist) #3

David Douthitt graced us by uttering:

[ HTML-bloat snipped ]

[ snip ]

Why should I use (or when should I use) ruby over ksh?
(Yes, I mean it :slight_smile:

ruby is not a shell, though there is undoubtedly at least 1 or 2 ruby
shell projects in progress right now.

As far as using ruby for admin tasks, I’ve been using Ruby for 99% of my
admin tasks since approx. 1 month after learning Ruby, and it’s several
times more graceful than ksh or any shell language for most tasks, IMHO.

PS: How do I do mkdir -p in ruby?

Hmm. TMTOWTDI:

1)

system(‘mkdir -p foo/bar’)

2)

‘foo/bar’.split(’/’).each do |dir|
Dir::mkdir(dir)
end

3)

require “ftools” # standard library
File::mkpath(‘foo/bar’)

HTH
Tim Hammerquist

···


Note that by displacing from “I got confused” to “It got confused”, the
programmer is not avoiding responsibility, but rather getting some analytical
distance in order to be able to consider the bug dispassionately.
– Jargon File 4.3.1


(Massimiliano Mirra) #4

require 'ftools’
File.mkpath(‘foo/bar’)

Massimiliano

···

On Tue, Jul 02, 2002 at 08:20:11AM +0900, David Douthitt wrote:

PS: How do I do mkdir -p in ruby?


(Philip Mak) #5

2)

‘foo/bar’.split(’/’).each do |dir|
Dir::mkdir(dir)
end

Isn’t that going to do this:

mkdir foo
mkdir bar

instead of this:

mkdir foo
mkdir foo/bar

3)

require “ftools” # standard library
File::mkpath(‘foo/bar’)

This seems like the cleanest way to do it.

···

On Tue, Jul 02, 2002 at 09:10:35AM +0900, Tim Hammerquist wrote:


(Joe "piman" Wreschnig) #6

Try

d = Dir::getwd
’foo/bar’.split(’/’).each do |dir|
Dir.mkdir(dir)
Dir.chdir(dir)
end
Dir.chdir(d)

Your version will create each directory in the current working
directory. Alternately, you could keep a string and use s += “/#{dir}”.

···

On Mon, 2002-07-01 at 19:10, Tim Hammerquist wrote:

David Douthitt graced us by uttering:

PS: How do I do mkdir -p in ruby?

2)

‘foo/bar’.split(’/’).each do |dir|
Dir::mkdir(dir)
end


Joe Wreschnig piman@sacredchao.net


(Josh Huber) #7

Philip Mak pmak@animeglobe.com writes:

2)

‘foo/bar’.split(’/’).each do |dir|
Dir::mkdir(dir)
end

Isn’t that going to do this:

mkdir foo
mkdir bar

instead of this:

mkdir foo
mkdir foo/bar

Yep, how about:

tmp = “”
‘foo/bar/baz’.split(’/’).each do |subdir|
tmp << subdir + "/"
Dir::mkdir(tmp)
end

But, yeah…using mkpath is probably the way to go, since it probably
handles much more error cases.

···

On Tue, Jul 02, 2002 at 09:10:35AM +0900, Tim Hammerquist wrote:


Josh Huber


(Tim Hammerquist) #8

Philip Mak graced us by uttering:

2)

‘foo/bar’.split(’/’).each do |dir|
Dir::mkdir(dir)
end

Isn’t that going to do this:

mkdir foo
mkdir bar

instead of this:

mkdir foo
mkdir foo/bar

smack! Of course! I apologize. I didn’t think it through.

3)

require “ftools” # standard library
File::mkpath(‘foo/bar’)

This seems like the cleanest way to do it.

This was, of course, my personal favorite. I tend to show people the
hard way before showing them the simpler way… just like my Calc prof
used to.

Again, my apologies. Use File::mkpath()

Tim Hammerquist

···

On Tue, Jul 02, 2002 at 09:10:35AM +0900, Tim Hammerquist wrote:

Randal can write one-liners again. Everyone is happy, and peace spreads
over the whole Earth.
– Larry Wall in 199705101952.MAA00756@wall.org