"System" command

Hello!

I was messing around with the “system” command in Ruby!
That’s really cool, but a question immediately sprang out while using it
: I noticed that its output is a TrueClass, but how could I use the
output made of directories and files as string?
For example : if I cast a

system(‘ls’)

it will return me a variety of directories and files. So, let’s put that
I want to scan this result to find out which directories are named how
(with a Regexp), is there a way to read this output or the is solely a
TrueClass?

On 12/20/2011 08:03 AM, Leo M. wrote:

it will return me a variety of directories and files. So, let’s put that
I want to scan this result to find out which directories are named how
(with a Regexp), is there a way to read this output or the is solely a
TrueClass?

Related to the system method is backticks (`):

output = ls
puts “These are the directories and files:\n#{output}”

The backticks run a command similarly to the system method, but the
output of the command you give them is captured into a string and
returned. You can slice and dice the string as you please at that
point.

Keep in mind though that there are more efficient ways to handle this
particular example than using an external command to acquire a
file/directory listing.

-Jeremy

Like the shell?

On 12/20/2011 10:12 AM, Leo M. wrote:

Like the shell?

If you’re asking about my efficiency comment, check out the glob and
similar methods in the Dir class:

http://rubydoc.info/stdlib/core/1.9.2/Dir.glob

It’s possible to skip Ruby entirely and use just the shell for many
things, and you may find that to be more efficient by some measures
depending on your needs. My comment, however, was more about avoiding
calling out to external programs when Ruby itself offers similar
functionality built-in. In this case, calling ls is needlessly
wasteful.

Whether or not you should call out to external programs for processing
of data will vary on a number of factors; including performance,
availability, cross platform compatibility, and ease of output
consumption to name a few. Be mindful of that when you start coding
calls to such programs. :slight_smile:

-Jeremy

Khat H. wrote in post #1037569:

Leo M. wrote in post #1037552:

Like the shell?

I laughed. :slight_smile:

:slight_smile:

I never studied informatics, many assumptions that you usually do could
not be clear enough to me :slight_smile:

Leo M. wrote in post #1037552:

Like the shell?

I laughed. :slight_smile:

Jeremy B. wrote in post #1037553:

On 12/20/2011 10:12 AM, Leo M. wrote:

Like the shell?

If you’re asking about my efficiency comment, check out the glob and
similar methods in the Dir class:

http://rubydoc.info/stdlib/core/1.9.2/Dir.glob

It’s possible to skip Ruby entirely and use just the shell for many
things, and you may find that to be more efficient by some measures
depending on your needs. My comment, however, was more about avoiding
calling out to external programs when Ruby itself offers similar
functionality built-in. In this case, calling ls is needlessly
wasteful.

Whether or not you should call out to external programs for processing
of data will vary on a number of factors; including performance,
availability, cross platform compatibility, and ease of output
consumption to name a few. Be mindful of that when you start coding
calls to such programs. :slight_smile:

-Jeremy

Thanks, yes of course. LS was just a trial to understand the mechanism
behind the courtain :slight_smile:

Besides of this, try out the system option or `` to launch rails
commands while in rails console! I found it very useful!

Hi Leo,

You can use the %x[] construction to get the output of a system command

For example:
output = %x[ls] #no quotes needed

-Jake

Hi Leo,

If you’re playing around with “system”, checking out “IO.popen” and
“open3” could also be of interest.

Garth

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs