Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

c00lryguy@gmail.com

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Hash.keys_to_meths by c00lryguy WIP

Example:
   1  
   2    hsh = {:one=>1, "two"=>2, "tres"=>3}
   3    hsh.keys_to_meth
   4    hsh.two # => 2
   5    hsh.two = "t-dubble-izzo" # => "t-dubble-izzo"
   6    p hsh # => {:one=>1, "two"=>"t-dubble-izzo", "three"=>3}

Fckup: String and Symbols mess up keys
   1  
   2    hsh = {:one=>1, "one"=>"uno"}
   3    hsh.keys_to_meth
   4    hsh.one # => 1
   5    hsh.one = "GARBLE" # => "t-dubble-izzo"
   6    p hsh # => {:one=>"GARBLE", "one"=>"uno"}

   1  
   2  module Keys_to_Meths
   3    def method_missing(meth, *args)
   4      if meth.to_s[-1] == 61 #if last char is an equal sign
   5        key = self.keys.select{|key| key.to_s == meth.to_s[0...-1]}[0]
   6        key ? self[key] = args[0] : raise("undefined key '#{meth}'")
   7      else
   8        key = self.keys.select{|key| key.to_s == meth.to_s}[0]
   9        key ? self[key] : raise("undefined key '#{meth}'")
  10      end
  11    end
  12  end
  13  
  14  class Hash
  15    def keys_to_meths
  16      self.each_key {|key| if self.methods.include?(key.to_s) then raise("key '#{key.to_s}' included in Hash method list") end}
  17      extend Keys_to_Meths
  18    end
  19  end

Hash_to_Struct

Make a hash work like a struct without having to use the struct class
Throw this into a new .rb file and use the require method for convenience

   1  
   2  =begin Hash_to_Struct by c00lryguy
   3    Description: 
   4      Make a hash work like a struct without having to use the struct class
   5    Example:
   6      imma_hash = {:one=>1, "two"=>2, "three"=>3}
   7      imma_hash.two # => 2
   8      imma_hash.two = "t-dubble-izzo" # => "t-dubble-izzo"
   9      p imma_hash # => {:one=>1, "two"=>"t-dubble-izzo", "three"=>3}
  10    Cons:
  11      Only works with strings and symbols
  12  =end
  13  
  14  module Hash_to_Struct
  15    def method_missing(meth, *args, &block)
  16      if self.keys.include?(meth.to_s.downcase) || self.keys.include?(meth.to_sym)
  17        key = self.keys.select{|key| key.to_s.downcase == meth.to_s.downcase}[0]
  18        self[key]
  19      elsif (self.keys.include?(meth.to_s.downcase[0..-2]) && meth.to_s.downcase[-1..-1] == "=") || (self.keys.include?(meth.to_s.downcase[0..-2].to_sym)  && meth.to_s.downcase[-1..-1] == "=")
  20        key = self.keys.select{|key| key.to_s.downcase == meth.to_s.downcase[0..-2]}[0]
  21        self[key] = args[0]
  22      else
  23        raise("#{meth} not a key")
  24      end
  25    end
  26  end
  27  class Hash; include Hash_to_Struct; end

Better File.find

Better File.find

   1  
   2  =begin File.find by c00lryguy
   3    Find all files in given path
   4    extension and filename optional
   5    
   6    File.find(path, ext, filename)
   7    
   8    Can also use hash as arguments:
   9      File.find(:path=>"C:\\",   <= ending backslash optional
  10                :ext=>".ini",    <= beginning period optional
  11                :filename=>"foo")
  12                
  13    Examples:
  14    
  15    File.find("E:\\") => all files on E:\
  16    File.find(:path=>"E:\\",:ext=>"rb") => all .rb files in E:\
  17    File.find("E:\\", ".rb", "foo") => all files named foo.rb in E:\
  18    
  19    Can also use multiple filenames and extensions:
  20    
  21    File.find(:path=>"E:\\",
  22              :ext=>[".ini", ".rb"],
  23              :filename=>["foo", bar]) => all files named foo or bar with either .ini or .rb extensions
  24  =end
  25  
  26  class File
  27    class << self
  28      def find(*args)
  29        require 'ostruct'
  30        
  31        input_query, filelist = {}, []
  32        
  33        #parse arguments
  34        if args.length == 1 && args[0].class == Hash
  35          input_query = args[0]
  36        elsif args.length >= 1
  37          if args.length > 3 : raise("Too many arguments\nCorrect arguments: path, ext, filename\n") end
  38          input_query[:path] = 
  39          input_query[:ext] = args[1] if args[1]
  40          input_query[:filename] = args[2] if args[2]
  41        end
  42        
  43        query = OpenStruct.new({:path=>nil, :ext=>"*", :filename=>"*"}.merge(input_query)) #creates query.path, query.ext, and query.filename
  44        if query.path == nil : raise("Argument 'path' not given") end
  45        
  46        
  47        query.ext.to_a.each{ |e|
  48          e[/\A\./] ? e=e[1..e.length-1] : e=e  #remove beginning periond on each ext if exists
  49          query.filename.to_a.each{ |f|
  50            query.path[/\\\z/] ? query.path=query.path[0..query.path.length-2] : query.path=query.path  #remove end backslash on path if exists
  51            filelist = filelist + Dir[ File.join(query.path.split(/\\/), "**", "#{f}.#{e}") ]
  52          }
  53        }
  54        
  55        return filelist
  56      end
  57    end
  58  end

Simple File.find

   1  
   2  # Simple File.find by c00lryguy
   3  # Thanks to justinwr for adding what I forgot to do
   4  # ------------------------------
   5  # Usage: 
   6  #     * = wildcard in filename
   7  #   File.find("E:\\") => All files in E:\
   8  #   File.find("E:\\Ruby", "*.rb") => All .rb files in E:\Ruby
   9  #   File.find("E:\\", "*.rb", false) => All .rb files in E:\, but not in its subdirs
  10  class File
  11    def self.find(dir, filename="*.*", subdirs=true)
  12      Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename) ]
  13    end
  14  end
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS