Enable Dropping a File Onto a Ruby Script

Enable dropping a file onto a Ruby script

The behavior of drag & drop is dependent on the OS (and in case of Linux of the Window Manager), so no.

In Windows, you get the behavior you want for free. Just put a .rb file on the Desktop, and the files dragged onto it will be arguments to your script.

Another easy way for integrating with Windows is to write to registry entry HKLM\Software\Classes*.jpg\myhandler\command with the command you want to appear in the context menu of Windows Explorer (right click on a jpg file will popup a menu which will have your script in the menu).

I don't use drag & drop at all in Linux, so I wouldn't know how to do that there. I would expect it to have more security issues (permissions must be right, ...) but you could get there by creating a .desktop file, see http://standards.freedesktop.org/desktop-entry-spec/latest/ for the complete standard, or read some examples from ~/Desktop/*.desktop .

Ruby accept argument via drag and drop onto rb script

One alternative is to create a batch file that handles the drag and drop part. As seen here, when you drag and drop files onto a batch file, the list of dropped files will be stored in %* as a space-separated list. A batch file that simply said ruby yourscript.rb %* should take this list of files and pass it to your script (where you can access the arguments using the ARGS array).

How do I drop to the IRB prompt from a running script?

you can use ruby-debug to get access to irb

require 'rubygems'
require 'ruby-debug'
x = 23
puts "welcome"
debugger
puts "end"

when program reaches debugger you will get access to irb.

Running a selfwritten ruby program outside of an IDE

The simple answer that should work for all versions of Windows is to just create a simple batch launcher.

Create a .bat file. I usually just create a new .txt file via "right click > new > text document". Then rename it, highlight everything, including the extension, and rename it to something like run.bat. The .bat part is important. Once you rename it, the icon should change to gears. If you can't overwrite the extension, or Windows is still treating it as a text document, you'll need to either manually save it as a bat, or disable "hide file extensions" in the explorer settings so the extension can be changed.


Edit the bat file, and put into it something like:

@echo off

YOUR RUN COMMAND HERE THAT YOU WOULD NORMALLY TYPE MANUALLY

pause

Paste the command that you would normally run manually where the capital text is. The first line is so it doesn't repeat the commands back, and the pause is so if an error happens, the command prompt doesn't immediately close. This gives you a chance to read the error.


Save it and close it. Now, if you double click on the bat file, your program should run.

What does $: . do to Ruby's require path?

  1. $: is the variable that holds an array of paths that make up your Ruby's load path
  2. << appends an item to the end of the array
  3. . refers to the current directory

    1   2  3
    | | |
    V V V
    $: << "."

So you are adding the current directory to Ruby's load path

References:

  1. Can be found in the Execution Environment Variables section of of this page from The Pragmatic Programmers Guide

    An array of strings, where each string specifies a directory to be searched for Ruby scripts and binary extensions used by the load and require methods. The initial value is the value of the arguments passed via the -I command-line option, followed by an installation-defined standard library location, followed by the current directory (“.”)[Obviously this link is for an older version of Ruby as this is still in there]. This variable may be set from within a program to alter the default search path; typically, programs use $: << dir to append dir to the path.

  2. Can be found in the docs for array at ruby-doc.org.

    Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.

how do I prevent a script loaded into irb from keeping previous values?

You're not restarting irb, so it depends on what AmazonProduct[] does. From its source:

def self.[](locale)
@requests[locale] ||= Request.new(locale)
end

It's caching, creating a new request iff one doesn't exist yet.

From one standpoint, it's "because it doesn't have a new". new is only called when the locale hasn't been loaded yet. From another, it's less new/not-new, but caching, without documenting it may do so.

Given the behavior, it's a reasonable assumption--and why my first thought was []'s implementation.


Regarding not restarting irb: if there isn't a mechanism to reload the cache (I didn't check), quickest thing to do would be to monkey-patch [] and always retrieve a new Request for a given locale.

How to debug Ruby scripts

Use Pry (GitHub).

Install via:

$ gem install pry
$ pry

Then add:

require 'pry'; binding.pry

into your program.

As of pry 0.12.2 however, there are no navigation commands such as next, break, etc. Some other gems additionally provide this, see for example pry-byebug.



Related Topics



Leave a reply



Submit