All posts by rogerdpack

How to serve a file in RoR

# this is how to send it out–through Ruby. Unfortunately it appears that webrick buffers all output, so…with a 1 gig file…that is not good–it runs out of memory.
# send_file(realFileCompleteName,
# :filename =>,
# :type => ‘image/gif’,
# :disposition => ‘attachment’,
# :streaming => ‘false’ # If false, the entire file is read into server memory and sent to the client. Otherwise, the file is read and written to the client in :buffer_size chunks. [from Agile Web Development p. 431]

# :buffer_size => ‘4096’
# }

Ruby starting a class as a thread

Here’s a function to start a class as if it were a thread (a la python)

def startGoFunctionAsThread()
return { ||
go() # this is defined somewhere else in the class

Remember if you run this as

a = objectName. startGoFunctionAsThread

to run a.join to wait for it to complete.

Ruby exception handling

how to do ruby try/catches

file = open(“/tmp/some_file”, “w”)
# … write to the file …

# ex: rescue Timeout::Error, Errno::ECONNREFUSED

# or  rescue Exception => exception
# … handle the exceptions …
file.close # … and this always happens.

How to pass in a ‘new’ DB setting through fields in RoR

So typically in RoR you pass one object from page to page (in my opinion you do). You are passing the parameters to create “one” new object, etc.
Now what happens when you want to pass in two objects?
You can re-create it by passing in parameters which will re-create and mimic the “one object” way…
It’s a little contorted…
Here’s an example:

<%= hidden_field 'filter', "id", :value => %>
This generates filter[id] = value yeesh!
To use it:

How to use an “interior” field to specify another DB entry

<%= text_field 'filter_assignment', 'parameter1_setting', "size" => 10, :value => 61 %>

now in the “receiving” model of the post…

newFilterLink =[:filter_assignment])

so we setup and used a new “db” entry by generating its parameters. This is just barely nuts, but works. A little bit contorted, as mentioned. Good luck!

How to use hidden values

So…say you’re in RoR and you want to pass to yourself some specific value to a hidden part of a form? Something that doesn’t totally match a DB entry, in the name?

Here’s how:

(This shows you how to specify the starting value, which also applies to other types besides hidden fields, BTW)


<%= hidden_field ‘filter’, “id”, :value => “newvalue” %>

This generates filter[id] = newvalue in the HTML.

What this actually does is allows you to setup an “arbitrary” value to pass into the controller.  Typically with RoR


<%= hidden_field ‘filter_to_use’, “id” %>


Generates <code>filter_to_use[id] = </code> in the HTML (I think). This is of course not useful if filter_to_use is a variable name that is just a variable you want to pass in to the next controller (i.e. it doesn’t exist).  So you need to use the <code> :value => whatever </code>above.

To use it in the receiving controller:

@filter = Filter.find(params[:filter_to_use][:id]) # retarded
What this does is looks up your parameter. So that’s a little contorted, but works, once you know how to set the value.

symbols disallowed in windows filenames

So…say you want to write a Ruby function to “sanitize” file names so that users can’t input file names and overwrite other files, or save theirs in odd places, etc?

Turns out that

Windows disallows:

? * / \ | < >

And the following are sensitive in Linux:
! & ; : ” ‘ ~ `
(at least for commands run in bash) so…

sanitizedName =[`\.?*\/\\|<>!&;:"'~@#\$%\^\(\)]/, '_')

Which replaces those bad boys with an underscore. So that’s my attempt at a file sanitize function so you can use it on the command line, etc.