Ruby exception handling

how to do ruby try/catches

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

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

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

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 => filter.id %>
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 = FilterAssignment.new(params[: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)

<code>

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

</code>
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

<code>

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

</code>

Generates <code>filter_to_use[id] = 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 = @user_blended_cast.name.gsub(/[`\.?*\/\\|<>!&;:"'~@#\$%\^\(\)]/, '_')

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.

Ruby require in local directory

So it turns out that if you require a ruby file that is in your local directory, it doesn’t work. It loads the file and the class names, but leaves out all of the functions, etc.

To get it to work

instead of

require ‘localfile’

require ‘./localfile’ works.

Go figure.

Also note there is a discreapncy between require and load, which is interesting. load _requires_  the “.rb” (at least for me) while require does not. A minor inconsistency.

Roger's meanderings, notes to himself, bug reports, and other things