Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

exists

Creating a directory with Pathname
Wednesday, 16 June 2010 08:53
To create a directory I would typically use File.mkdir however class Pathname can be a convenient alternative e.g.

require 'pathname'

pathname = Pathname.new '/home/james/learning/ruby/fun/today.txt'
#=> #

pathname.exist?
#=> false

pathname.basename
#=> #

#=> #

pathname.dirname.mkdir
#=> 0

# observed directory '/home/james/learning/ruby/fun' was just created

Notes:

1) There is no Pathname#mkdir_p meaning parent directories will not be created automatically.

2) If the directory to be created already exists it will create an error
e.g. Errno::EEXIST: File exists - /home/james/learning/ruby/fun

Resources:
- pathname: Ruby Standard Library Documentation [ensta.fr]
- Module: FileUtils [ruby-doc.org]

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/22zLf6bnb_M/11643

 
Check if a Ruby method exists
Sunday, 14 March 2010 15:01
Source: ruby’s respond_to? for checking if a method exists [prateekdayal.net]


Ruby has a function called respond_to? that can be used to seeing if a particular class or object has a method with a certain name. The syntax is something like

User.respond_to?('name') # returns true is method name exists otherwise false

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/1BMijCTcXqk/10519

 


Taxonomy by Zaragoza Online