Setting a custom Content type

schmidt Nov 3, 2009

The given example seems to be broken. The +:mime_type+ option as well as the [] access on the Mime::Type class are both not working.

The following code allows the custom setting of content types as intended by the original example:

class  PostsController < ActionController::Base
d...

Don't forget to require 'tmpdir'

mcmire Oct 31, 2009 2 thanks

If you simply say Dir.tmpdir you might get a nice surprise:

irb> Dir.tmpdir NoMethodError: undefined method `tmpdir' for Dir:Class from (irb):1

Strangely, this method seems to be stored in a file that Ruby doesn't require by default. Just require 'tmpdir' and all should be wel...

Map-like Manipulation of Hash Values

svoop Oct 30, 2009 3 thanks

Let's say you want to multiply all values of the following hash by 2:

hash = { :a => 1, :b => 2, :c => 3 }

You can't use map to do so:

hash.map {|k, v| v*2 }   # => [6, 2, 4]

However, with merge you can:

hash.merge(hash) {|k,v| v*2 }   => {:c=>6, :a=>2, :b=>4}

(The above is Ruby 1....

Getting relative path from absolute globbing

Mange Oct 19, 2009 2 thanks

Say you want to scan for files in directory +base_dir+ and you want to use the relative path from this base dir, you could do it like this:

base_dir = '/path/to/dir'
files = Dir[File.join(base_dir, '**', '*.yml')]

# files now contain absolute paths:
files.first # => "/path/to/dir/f...

Get all inner texts

emime Oct 18, 2009 1 thank

Extend REXML::Element so that it can get the first text and following inner texts (child texts included) of the current element as array and as string:

class REXML::Element

def inner_texts

REXML::XPath.match(self,'.//text()') end

def inner_text

REXML::XPath.match(self,'.//t...

Receiving data over UDP

mutru Oct 18, 2009 1 thank

It's perfectly normal to receive 'X' strings with Ruby's UDP sockets before the actual content.

Consider the following example:

require 'socket'

PORT = 5500

socket = UDPSocket.new
socket.bind('', PORT)

for i in 1..10
IO.select([socket])
p socket.recvfrom_nonblock(409...