Archive for January, 2009

For my own notes:
To rebuild the Postfix virtual.db file including any new additions/edit to the /etc/postfix/virtual config file. Use this command line a root:
1postmap virtual

This might help someone:
Today I was trying to build a form that was going to be used as an include/partial on multiple pages of a website. So I needed each included version to submit to its parent page (itself). I was going to handle each submission with a before filter in the application controller. I [...]

Useful to find out if one number is divisible by another, in this case 2:
PHP:
123if($c % 2 == 0){
    # It is.
}
Ruby
123if c % 2 == 0
  # It is
end

This works as a rtrim equivalent. I’m trimming a comma (,) in this example.
PHP:
1$variable = rtrim($variable, ‘,’);
Ruby:
Could be a one liner but it’s easy to see how the Regex is working with this example.
12rtrim_chars = ","
variable = variable.gsub(/[#{rtrim_chars}]+$/, ”)

It’s probably a better idea to use the following to check types in Ruby.
1array.is_a?(Array)
I got caught out earlier trying this:
1array.class # Returns: Array
The returned ‘Array’ value is NOT a string but the actual class. So the following will not equate to true:
123if array.class == ‘Array’
#Do stuff
end
But this will:
123if array.class.to_s == ‘Array’
#Do stuff
end
Must admit, I prefer [...]

You can’t unset an array in Ruby! You set it to nil and let the garbage collector take care of it!
1array = nil

To convert wav to mp3 using FFMPEG on OSX you’ll need to install it with this port command as root (or sudo):
1port install ffmpeg +gpl +lame +x264 +xvid
This will enable Lame and some other useful codecs, allowing you to encode mp3 (192kbs bitrate in this example) with this command line:
1ffmpeg -i source.wav -acodec libmp3lame -ab [...]

For my own records:
1UPDATE tablename SET tablefield = REPLACE(tablefield,"findstring","replacestring");

If you want to use the /lib/ directory in a Ruby on Rails application to store .rb files that contain constants. For some (name spacing / scoping?) reason they must be written in a class declaration:
Contents of /lib/constants.rb
123def ConstantClass
  CONSTANT_NAME = ’some value’
end
Then access these constants using the scope resolution operator in your views/controllers:
1<p><%= ConstantClass::CONSTANT_NAME [...]

Might be useful for someone, I wrote it to handle dynamic select menus. Particularity when listing URLs.
1234567891011121314151617181920212223242526272829<?php

/**
* Smarty truncate_left modifier plugin
*
* Type:     modifier<br />
* Name:     truncate_left<br />
* Purpose:  truncates left hand side of a string
* @link: none
* @param string
* @param integer
* @param string
* @return string
*/

function smarty_modifier_truncate_left($string, $threshold=80, $replacement=’&amp;hellip;’) {

if(strlen($string) >= $threshold){

$amount_over  = count($string) – $threshold;
$output [...]


top