Saturday, September 12, 2009

Use File.join to state file pathname

# Straight naming of pathname for "file.rb"
require 'path/name/for/file'

# Using File.join. Advantage --> auto forward slash on windows
require File.join('path', 'name', 'for', file')

# Even better way which takes care of relative paths
require File.join(File.dirname(__FILE__), 'path', 'name', 'for', 'file')

Linux: './path/name/for/file'
Windows: '.\path\name\for\file'

# Full path instead of relative path
require File.expand_path(File.join(File.dirname(__FILE__), 'path', 'name', 'for', 'file'))

Linux: '/home/jasonong/path/name/for/file'
Windows: 'c:\users\jasonong\path\name\for\file'

No comments:

Blog Archive