Skip to content

Instantly share code, notes, and snippets.

@morganp
morganp / ruby_file_operators_i_foget.rb
Created November 22, 2010 09:30
File Operators I forget
File.expand_path( relative_file ) => absolute_file
File.dirname( path/file ) => path
File.basename( path/file ) => file
#Better way to delete if it exists
if UnWatched::Node.exists?( :name=>file_name )
node = UnWatched::Node.find_by_name( file_name )
UnWatched::Node.destroy(node)
end
#!/usr/bin/env ruby
#List all file below this location
path = File.expand_path( $0 )
puts path
def list_files( files, spacer='', path='.')
path = File.expand_path( path )
files.each do |f|
#Ruby mixin Enumerable implement each:
# hey presto you have .map (but not .map!)
class Core
include Enumerable
attr_reader :name, :data
def initialize( name, data )
#Classy Sinatra
#Launch with ruby app.rb
require 'rubygems'
require 'sinatra/base'
#module MyModule
class MyApp < Sinatra::Base
#set :raise_errors, Proc.new { false }
#set :sessions, false
#set :static, true
@morganp
morganp / flatten.rb
Created September 2, 2010 02:14 — forked from Whoops/flatten.rb
require 'pp'
a=[1,[2,[3]],4,5,[6,7,[8,9]]]
pp a.inject([]){|b,x| if Array===x then x.each {|y| b << y } else b << x end;b;}
#Sort by field / case insensitive
@tags = @tags.sort_by { |tag| tag.tag.downcase }
$DATE_FORMAT = "%I:%M%p %b %d %Y"
x = Time.now
puts x.strftime($date_format)
def contents(path)
n_path = File.expand_path( path )
Dir.chdir(n_path)
files = Dir.glob('*')
return files
end
center = [500,500]
radius = 500
height = 100
width = 100
num_edge = 50
num_hour = 10
num_min = 10
length_hour = 300