Running a file

ok, i’m writing a ruby program that interprets some text and does stuff
with it, but thats not what i’m here to ask about.
What I want to know is how would I make it so the user can right-click
and open a file with my program. Just like how if you right click a .rb
file you can run it with the ruby MRI. All I need is to get the text in
the file into a variable in the program when the user opens it. Any
suggestions?

Thanks
John

Is it quine? You can get info at http://www.nyx.net/~gthompso/quine.htm

no, i just want to be able to right click and run a file and run it with
my program.

On Fri, Feb 26, 2010 at 7:30 PM, John P. [email protected]
wrote:

ok, i’m writing a ruby program that interprets some text and does stuff
with it, but thats not what i’m here to ask about.
What I want to know is how would I make it so the user can right-click
and open a file with my program. Just like how if you right click a .rb
file you can run it with the ruby MRI. All I need is to get the text in
the file into a variable in the program when the user opens it. Any
suggestions?

Assuming you’re using Windows, you will most likely have to edit the
registry entry for the file type you want to open with your script.

Eric C. wrote:

On Fri, Feb 26, 2010 at 7:30 PM, John P. [email protected]
wrote:

ok, i’m writing a ruby program that interprets some text and does stuff
with it, but thats not what i’m here to ask about.
What I want to know is how would I make it so the user can right-click
and open a file with my program. Just like how if you right click a .rb
file you can run it with the ruby MRI. All I need is to get the text in
the file into a variable in the program when the user opens it. Any
suggestions?

Assuming you’re using Windows, you will most likely have to edit the
registry entry for the file type you want to open with your script.

Open explorer, menu Extra --> Folder options --> tab File types, select
the file type, for instance .txt --> advanced --> new

Under “action” type a name
The other textfield should propably something like
path\to\ruby.exe path\to\your_progam.rb %1

hth,

Siep

On Sat, Feb 27, 2010 at 11:20 AM, Siep K. [email protected]
wrote:

Assuming you’re using Windows, you will most likely have to edit the
registry entry for the file type you want to open with your script.

Open explorer, menu Extra --> Folder options --> tab File types, select
the file type, for instance .txt --> advanced --> new

Under “action” type a name
The other textfield should propably something like
path\to\ruby.exe path\to\your_progam.rb %1

Siep, I was about to suggest the same thing, except when I tried it,
it said it couldn’t find the Ruby executable. It turned out that I
needed to put quotes around the Ruby pathname (since it has spaces on
my machine).

On Sun, Feb 28, 2010 at 01:35:24AM +0900, John P. wrote:

i am using windows, but opening a file with my program doesn’t do
anything. I need to know what code will allow the program to read the
file when the file is opened with my program.

It sounds like what you need is not a way to get the operating system to
call your program when “doing something” with a file, but a way to get
your program to operate on a file. The answer to that question
depends
on what exactly you want to do with the file within your program.

If that is what you’re asking, I recommend you look at the File module
for hints:

http://www.ruby-doc.org/core/classes/File.html

i am using windows, but opening a file with my program doesn’t do
anything. I need to know what code will allow the program to read the
file when the file is opened with my program.

On Sun, Feb 28, 2010 at 11:42:11AM +0900, John P. wrote:

yes thats what im trying to do, but i want to do it by having the user
right-click> open with> my program that would hopefully give my program
the directory of the file, which i could then use. as opposed to the way
i have it now where they need to type in the filename. i read through
the file section on ruby-doc, no help there.

How do you have the program written to take the path/filename now? Have
you tried feeding it a filename as a command argument? I’m wildly
guessing here, because I’ve never done what you’re trying to do, but it
seems (intuitively, to me at least) like that should work.

For example, if you write your program such that this works:

> progname filename.ext

. . . then it seems like double-clicking the file, when the filename
extension in question is associated with your program via MS Windows’
file type association mechanism, should automatically feed it to the
program just as it would be fed to the program as a command argument.
I’d think it would be worth a try.

If I was using an MS Windows system right this instant, I’d give it a
try
just to make sure before offering the suggestion, but I figure it’s
worth
telling you my idea in case you get a chance to test it before I find
myself sitting in front of my XP test system.

Chad P. wrote:

How do you have the program written to take the path/filename now? Have
you tried feeding it a filename as a command argument? I’m wildly
guessing here, because I’ve never done what you’re trying to do, but it
seems (intuitively, to me at least) like that should work.

For example, if you write your program such that this works:

> progname filename.ext

. . . then it seems like double-clicking the file, when the filename
extension in question is associated with your program via MS Windows’
file type association mechanism, should automatically feed it to the
program just as it would be fed to the program as a command argument.
I’d think it would be worth a try.

If I was using an MS Windows system right this instant, I’d give it a
try
just to make sure before offering the suggestion, but I figure it’s
worth
telling you my idea in case you get a chance to test it before I find
myself sitting in front of my XP test system.

thats not how I have it now, but I think it would work beautifully. What
I need is the code that would let my program take a command argument,
any clues as to where i could find that?

On Mon, Mar 01, 2010 at 12:57:50AM +0900, John P. wrote:

thats not how I have it now, but I think it would work beautifully. What
I need is the code that would let my program take a command argument,
any clues as to where i could find that?

Look into Ruby’s handling of the ARGV array, which is automaticaly
populated with command line argument strings. For instance, if a single
argument of ‘filename.txt’ is supplied, the following is true:

ARGV[0] == 'filename.txt'

Thus, you could use ARGV[0] as an argument for File.open() or
File.read()
to open a file. There are other means of getting such input in Ruby as
well, such as using this:

while gets
  # do stuff with $_
end

That can be implicitly wrapped around the rest of your code by way of
the
-n command line option for Ruby, and is equivalent to the following Perl
idiom:

while (<>) {
  # do stuff with $_
}

I recommend doing some searches in your favorite search engine for file
input and handling of STDIN with Ruby. I’ve probably done enough of
your
work for you already.

yes thats what im trying to do, but i want to do it by having the user
right-click> open with> my program that would hopefully give my program
the directory of the file, which i could then use. as opposed to the way
i have it now where they need to type in the filename. i read through
the file section on ruby-doc, no help there.

thanks so much, thats exactly what I was looking for, thanks again for
everyones time.

On Mon, Mar 01, 2010 at 09:48:08AM +0900, John P. wrote:

thanks so much, thats exactly what I was looking for, thanks again for
everyones time.

I’m glad it was helpful. Good luck.

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