Some basic Ruby questions

Hi!

I am writing a gallery maker (HTML) in ruby and could need some help…

I already know how to write to files.
What I don’t know is following:

How can i execute some native commands in Ruby? (For example ls | grep “jpg” >
test.txt)

What I am planning to do is executing this linux command, so that all
image files names get stored in a local file on the HD.
And after that i want to read the filenames in my ruby application one by
one, cause each file has an own line in this output file.

So, it should look like this:

def pictureMethod
executeNativeCommand(“ls | grep “jpg” > pictures.txt”)
pictureFile = File.open(“pictures.txt”,“r”)
while(pictureFile.hasMoreLines)
array.attach(pictureFile.actualLine)
pictureFile.goOneLineDown
end
end

I don’t know the Ruby syntax that’s why I wrote it in kind of “spoken code”:slight_smile:

Hope someone can help me out,

Thanks in advance, Benjamin

···

Do You Yahoo!?
Yahoo! Health - Feel better, live better

Why would you want to call external programs when Ruby does all of
that?

array = Dir.new(’.’).entries.grep(/.jpg$/)

···

On Friday 26 July 2002 11:19 am, Benjamin Sommerfeld wrote:

def pictureMethod
executeNativeCommand(“ls | grep “jpg” > pictures.txt”)
pictureFile = File.open(“pictures.txt”,“r”)
while(pictureFile.hasMoreLines)
array.attach(pictureFile.actualLine)
pictureFile.goOneLineDown
end
end


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

So, it should look like this:

def pictureMethod
executeNativeCommand(“ls | grep “jpg” > pictures.txt”)
pictureFile = File.open(“pictures.txt”,“r”)
while(pictureFile.hasMoreLines)
array.attach(pictureFile.actualLine)
pictureFile.goOneLineDown
end
end

···

=========================================

Try this:

Dir[’*.txt’]

sample resulting array:

[“codes.txt”, “desc.txt”, “map.txt”, “temp.txt”]

easy, eh?

Chris
http://clabs.org

Hi,

def pictureMethod
executeNativeCommand(“ls | grep “jpg” > pictures.txt”)
pictureFile = File.open(“pictures.txt”,“r”)
while(pictureFile.hasMoreLines)
array.attach(pictureFile.actualLine)
pictureFile.goOneLineDown
end
end

Like this?

def pictureMethod
array = []
system(“ls | grep “jpg” > pictures.txt”)
pictureFile = open(“pictures.txt”,“r”)
while (line = pictureFile.gets)
array.push(line)
end
array
end

Or even

def pictureMethod
system(“ls | grep “jpg” > pictures.txt”)
File.readlines(“pictures.txt”)
end

If you don’t need to leave the file

def pictureMethod
open("|ls | grep “jpg”").readlines
end

I don’t know the Ruby syntax that’s why I wrote it in kind of “spoken code”:slight_smile:

It was close.
matz.

···

In message “Some basic Ruby questions” on 02/07/27, Benjamin Sommerfeld benlebt@yahoo.de writes:

Why would you want to call external programs when Ruby does all of
that?

Probably, like most people asking basic questions, he didn’t know it
did.

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Health - Feel better, live better

Which, as Chris pointed out, is more efficiently written as:

array = Dir[ ‘*.jpg’ ]

You might also look at the find module if you need to search a
hierarchy. To find all the .jpg files in or under the current
directory:

require ‘find’
array = []
Find.find(’.’) { |f| array << f if /.jpg$/ =~ f }

···

On Friday 26 July 2002 11:31 am, I wrote:

On Friday 26 July 2002 11:19 am, Benjamin Sommerfeld wrote:

def pictureMethod
executeNativeCommand(“ls | grep “jpg” > pictures.txt”)
pictureFile = File.open(“pictures.txt”,“r”)
while(pictureFile.hasMoreLines)
array.attach(pictureFile.actualLine)
pictureFile.goOneLineDown
end
end

Why would you want to call external programs when Ruby does all of
that?

array = Dir.new(’.’).entries.grep(/.jpg$/)


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

or even something like

def pictureMethod
ls -1 --color=never *jpg*.split("\n")
end

···

On Sat, Jul 27, 2002 at 03:47:15AM +0900, Yukihiro Matsumoto wrote:

def pictureMethod
open("|ls | grep “jpg”").readlines
end


http://mas.cs.umass.edu/~rawlins

I used to know the answer to that one, before I ate so many preservatives…
(Zippy)

Thanks for all of your help!

Another question:

Does anyone know where to find a really good Ruby/GTK Howto?

···

Do You Yahoo!?
Yahoo! Health - Feel better, live better

Of course. So how can Ruby answer these questions more quickly?

I rather like Perl’s built-in FAQ answerer (though there’s not nearly
enough of a search built in):

$ perldoc -q day

answers:
How do I find the week-of-the-year/day-of-the-year?
How can I find the Julian Day?
How do I find yesterday’s date?

though I couldn’t think of anything that would make it spit out the
answer to this particular question.

···

On Friday 26 July 2002 11:33 am, Michael Campbell wrote:

Why would you want to call external programs when Ruby does all
of that?

Probably, like most people asking basic questions, he didn’t know
it did.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

I just wanted to point this out as a caveat in particular as someone
mentioned that filenames and directories can contain newlines.

db

···

On Sat, Jul 27, 2002 at 03:55:11AM +0900, Kyle Rawlins wrote:

or even something like

def pictureMethod
ls -1 --color=never *jpg*.split("\n")
end


http://mas.cs.umass.edu/~rawlins

I used to know the answer to that one, before I ate so many preservatives…
(Zippy)


“Then we typed the G, and the system crashed”…

Hi,

···

At Sat, 27 Jul 2002 03:39:08 +0900, Ned Konz wrote:

require ‘find’
array = []
Find.find(’.’) { |f| array << f if /.jpg$/ =~ f }

array = Dir["**/*.jpg"]


Nobu Nakada

I just wanted to point this out as a caveat in particular as someone
mentioned that filenames and directories can contain newlines.

db

Didn’t think of that at all - I think any of the other solutions posted that
called ‘ls’ have this problem too. The only way to do it correctly by making
calls to ls seems to be to parse the output of the dired mode (which isn’t
hard, but hard to do in one line).

You can also have ls output strings meant for C which are properly escaped and
quoted, but I don’t know of any way to unescape them without eval (also
possibly causing issues if for some strange reason a #{} sequence is in a
filename):

ls -Q1 --color=never *.jpg.split("\n").collect do |item| eval("#{item}") end

So I guess the pure ruby methods are better.

btw, to make one of these crazy files in bash (this took me a bit of work) type
the following line:
touch ‘test^v^jing’

where the ^v is typed as control-v

-kyle

···

On Sat, Jul 27, 2002 at 07:00:01AM +0900, Daniel Bretoi wrote:

On Sat, Jul 27, 2002 at 03:55:11AM +0900, Kyle Rawlins wrote:

or even something like

def pictureMethod
ls -1 --color=never *jpg*.split("\n")
end


http://mas.cs.umass.edu/~rawlins

I used to know the answer to that one, before I ate so many preservatives…
(Zippy)