<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: string code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 08:15:18 GMT</pubDate>
    <description>DZone Snippets: string code</description>
    <item>
      <title>java String</title>
      <link>http://snippets.dzone.com/posts/show/5798</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public static void main(String args[]) {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 20 Jul 2008 11:58:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5798</guid>
      <author>zsearch (yonglongzhou)</author>
    </item>
    <item>
      <title>Javascript count line breaks</title>
      <link>http://snippets.dzone.com/posts/show/5770</link>
      <description>Returns the number of line breaks in a string.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function lineBreakCount(str){&lt;br /&gt;	/* counts \n */&lt;br /&gt;	try {&lt;br /&gt;		return((str.match(/[^\n]*\n[^\n]*/gi).length));&lt;br /&gt;	} catch(e) {&lt;br /&gt;		return 0;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 Jul 2008 08:57:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5770</guid>
      <author>featurelover (Jochen Preusche)</author>
    </item>
    <item>
      <title>Acronym Builder</title>
      <link>http://snippets.dzone.com/posts/show/5737</link>
      <description>// Generates an acronym from a string. For example "Hello World Ruby SCrIpT" goes to "HWRS".&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    acronym = ''&lt;br /&gt;    bytes = [] # Byte array&lt;br /&gt;    long_string = ' ' + long_string # so the first letter, if capital, is included&lt;br /&gt;    long_string.each_byte {|byte| bytes.push(byte)} # Build array of bytes&lt;br /&gt;    bytes.each_with_index do |byte, index|&lt;br /&gt;      if byte &gt; 64 and byte &lt; 96 # This is the range for ASCII capital letters, your encoding may vary&lt;br /&gt;        # Capital letter&lt;br /&gt;        if bytes[index-1] == 32 # We check behind to see if the letter is preceded by a space. Again, encoding may vary. 32 is ASCII space.&lt;br /&gt;          acronym = acronym + long_string[index,1]&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Jul 2008 10:36:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5737</guid>
      <author>JamesHarrison (James Harrison)</author>
    </item>
    <item>
      <title>String.random</title>
      <link>http://snippets.dzone.com/posts/show/5681</link>
      <description>&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  def self.random(length=20)&lt;br /&gt;    chars = ('a'..'z').to_a + ('A'..'Z').to_a + ("0".."9").to_a&lt;br /&gt;    hash = ""; length.times { hash &lt;&lt; chars[rand(chars.size)] }; hash&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Jun 2008 15:58:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5681</guid>
      <author>mcmire ()</author>
    </item>
    <item>
      <title>Split array or string into smaller arrays</title>
      <link>http://snippets.dzone.com/posts/show/5641</link>
      <description>Splits an array or string into smaller arrays of the given size. Inspired on the ruby one ;).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import math&lt;br /&gt;# v = value to split, l = size of each chunk&lt;br /&gt;f = lambda v, l: [v[i*l:(i+1)*l] for i in range(int(math.ceil(len(v)/float(l))))]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; f('000000111111222222333333444444567', 6)&lt;br /&gt;['000000', '111111', '222222', '333333', '444444', '567']&lt;br /&gt;&lt;br /&gt;&gt;&gt;&gt; f(tuple('000000111111222222333333444444567'), 6)&lt;br /&gt;[('0', '0', '0', '0', '0', '0'), ('1', '1', '1', '1', '1', '1'), ('2', '2', '2', '2', '2', '2'), ('3', '3', '3', '3', '3', '3'), ('4', '4', '4', '4', '4', '4'), ('5', '6', '7')]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Long live python :). Of course, a lisp/haskell implementation probably blows this snippet out.</description>
      <pubDate>Fri, 13 Jun 2008 19:37:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5641</guid>
      <author>santiago.aguiar (Santiago Aguiar)</author>
    </item>
    <item>
      <title>Very simple ruby code to chunk a string</title>
      <link>http://snippets.dzone.com/posts/show/5621</link>
      <description>I wanted to chunk a string and saw lots of 100 line examples. This is the trivial thing I came up with. Does have a small bug when the string length is an exact multiple of the len variable (as in this example!) that adds an extra empty string on the end.&lt;br /&gt;&lt;br /&gt;But it worked for what I needed.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  string = "some long test string or other"&lt;br /&gt;  r = []&lt;br /&gt;  len = 15&lt;br /&gt;  start = 0&lt;br /&gt;  while(start &lt;= string.length) do                                              &lt;br /&gt;    r &lt;&lt; string[start...start+len] &lt;br /&gt;    start += len &lt;br /&gt;  end&lt;br /&gt;  r # ["some long test ", "string or other", ""]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 09 Jun 2008 15:34:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5621</guid>
      <author>fjfish (Francis Fish)</author>
    </item>
    <item>
      <title>Basic Trie data structure in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5616</link>
      <description>&lt;code&gt;&lt;br /&gt;# Inspired by http://www.rubyquiz.com/quiz103.html&lt;br /&gt;# cf. also http://en.wikipedia.org/wiki/Trie&lt;br /&gt;&lt;br /&gt;#!/usr/local/bin/ruby -w&lt;br /&gt;&lt;br /&gt;require 'pp'&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;&lt;br /&gt;   def deep_merge!(second)&lt;br /&gt;      # cf. http://snippets.dzone.com/posts/show/4146&lt;br /&gt;      merger = proc { |key,v1,v2| Hash === v1 &amp;&amp; Hash === v2 ? v1.merge(v2, &amp;merger) : v2 }&lt;br /&gt;      self.merge!(second, &amp;merger)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def nested_hash(array)&lt;br /&gt;      node = self&lt;br /&gt;      array.each do |i|&lt;br /&gt;         node[i]=Hash.new if node[i].nil?&lt;br /&gt;         node = node[i]&lt;br /&gt;      end &lt;br /&gt;      self&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def merge_nested_hash!(nested_hash)&lt;br /&gt;      deep_merge!(nested_hash)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   # code basis taken from: "Find every path and it's value in a Hash" by Florian A&#223;mann,&lt;br /&gt;   # http://snippets.dzone.com/posts/show/3565&lt;br /&gt;&lt;br /&gt;   def each_trie_path&lt;br /&gt;      raise ArgumentError unless block_given?&lt;br /&gt;      self.class.each_trie_path(self) { |path, object| yield(path, object) }&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   protected&lt;br /&gt;   def self.each_trie_path(object, path = [], &amp;block)  &lt;br /&gt;&lt;br /&gt;      if object.is_a?(Hash)&lt;br /&gt;         object.each do |key, value|&lt;br /&gt;&lt;br /&gt;            if key == true &amp;&amp; value == {}&lt;br /&gt;               if path == [:root]  # special case for empty string: [[:root], {true=&gt;{}}]&lt;br /&gt;                  yield(path, {true=&gt;{}})&lt;br /&gt;                  next&lt;br /&gt;               end&lt;br /&gt;               yield(path, object)&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;            self.each_trie_path(value, [path , key].flatten, &amp;block)&lt;br /&gt;         end&lt;br /&gt;      else &lt;br /&gt;         yield(path, object)&lt;br /&gt;      end&lt;br /&gt;   end &lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Trie&lt;br /&gt;&lt;br /&gt;   @hash = Hash.new.merge_nested_hash!(Hash.new)&lt;br /&gt;   #@hash = Hash.new.merge_nested_hash!({:root=&gt;{}})&lt;br /&gt;   #@hash = Hash.new.merge_nested_hash!(Hash.new.nested_hash([:root]))&lt;br /&gt;   class &lt;&lt; self; attr_accessor :hash; end    # Trie.hash&lt;br /&gt;&lt;br /&gt;   def initialize&lt;br /&gt;      Trie.hash.merge_nested_hash!({:root=&gt;{}})&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def add_int(int)   # for int &gt;= 0&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  # integer array; ex: [4,6,2]&lt;br /&gt;      ia.unshift(:root).push(true)&lt;br /&gt;      Trie.hash.merge_nested_hash!(Hash.new.nested_hash(ia))&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def matchi(int)  # match integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node[digit]&lt;br /&gt;         #node = node.fetch(digit,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpi(int)   # match first part of integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }&lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node[digit]&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def add_word(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      ca.unshift(:root).push(true)&lt;br /&gt;      Trie.hash.merge_nested_hash!(Hash.new.nested_hash(ca))&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def match(word)   # match entire word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node[char]&lt;br /&gt;         #node = node.fetch(char,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpw(word)   # match first part of word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node[char]&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;trie = Trie.new&lt;br /&gt;p Trie.hash&lt;br /&gt;&lt;br /&gt;trie.add_word("word")&lt;br /&gt;pp Trie.hash            &lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.match("word2")   &lt;br /&gt;trie.add_word("word2")&lt;br /&gt;p trie.match("word2")   &lt;br /&gt;pp Trie.hash            # {:root=&gt;{"w"=&gt;{"o"=&gt;{"r"=&gt;{"d"=&gt;{true=&gt;{}, "2"=&gt;{true=&gt;{}}}}}}}}&lt;br /&gt;&lt;br /&gt;trie.add_word("")&lt;br /&gt;p trie.match("")&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.mfpw("wor")     # match first part of word&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;trie.add_int(12345)&lt;br /&gt;p Trie.hash&lt;br /&gt;&lt;br /&gt;trie.add_int(12980345)&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;trie.add_int(8512)&lt;br /&gt;pp Trie.hash&lt;br /&gt;p trie.matchi(8512)&lt;br /&gt;p trie.matchi(85)&lt;br /&gt;p trie.mfpi(85)     # match first part of integer&lt;br /&gt;p trie.matchi(51)&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;pp Trie.hash&lt;br /&gt;paths = []&lt;br /&gt;Trie.hash.each_trie_path { |path, value| paths.push([ path ]) }&lt;br /&gt;#Trie.hash.each_trie_path { |path, value| paths.push([ path, value ]) }&lt;br /&gt;#pp paths&lt;br /&gt;puts&lt;br /&gt;paths.each { |x| p x }&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;require 'pp'&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;   def Hash.new_nested_hash&lt;br /&gt;     Hash.new{|h,k| h[k]=Hash.new(&amp;h.default_proc) }&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Trie&lt;br /&gt;&lt;br /&gt;   @hash = Hash.new_nested_hash&lt;br /&gt;   #@hash = Hash.new_nested_hash.update(true=&gt;{})  # add empty string by default&lt;br /&gt;   class &lt;&lt; self; attr_accessor :hash; end    # Trie.hash&lt;br /&gt;&lt;br /&gt;   def &lt;&lt;(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      wl = ca.size            # word length&lt;br /&gt;      str = ""&lt;br /&gt;      wl.times { |x| str &lt;&lt; "[ca.at(#{x})]" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; "[true]"&lt;br /&gt;      #p str     # example: "Trie.hash[ca.at(0)][ca.at(1)][ca.at(2)][ca.at(3)][true]"&lt;br /&gt;      eval(str)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def match(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      wl = ca.size            # word length&lt;br /&gt;      str = ""&lt;br /&gt;&lt;br /&gt;      wl.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),nil)" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),nil).fetch(ca.at(1),nil).fetch(ca.at(2),nil).fetch(true,nil)"&lt;br /&gt;      ret = eval(str) rescue nil&lt;br /&gt;&lt;br /&gt;=begin&lt;br /&gt;      # alternative&lt;br /&gt;      wl.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),{})" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),{}).fetch(ca.at(1),{}).fetch(ca.at(2),{}).fetch(true,nil)"&lt;br /&gt;      ret = eval(str)&lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;      ret == {} ? true : false   # {} is the default value created by Trie.hash or Hash.new_nested_hash respectively&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;trie = Trie.new&lt;br /&gt;trie &lt;&lt; "word"&lt;br /&gt;pp Trie.hash            &lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.match("word2")   &lt;br /&gt;trie &lt;&lt; "word2"&lt;br /&gt;p trie.match("word2")   &lt;br /&gt;pp Trie.hash            # {"w"=&gt;{"o"=&gt;{"r"=&gt;{"d"=&gt;{true=&gt;{}, "2"=&gt;{true=&gt;{}}}}}}}&lt;br /&gt;&lt;br /&gt;trie &lt;&lt; ""&lt;br /&gt;p trie.match("")&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# alternative with default :root element&lt;br /&gt;&lt;br /&gt;require 'pp'&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;   def Hash.new_nested_hash&lt;br /&gt;     Hash.new{|h,k| h[k]=Hash.new(&amp;h.default_proc) }&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;&lt;br /&gt;   # code basis taken from: "Find every path and it's value in a Hash" by Florian A&#223;mann,&lt;br /&gt;   # http://snippets.dzone.com/posts/show/3565&lt;br /&gt;&lt;br /&gt;   def each_trie_path&lt;br /&gt;      raise ArgumentError unless block_given?&lt;br /&gt;      self.class.each_trie_path(self) { |path, object| yield(path, object) }&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   protected&lt;br /&gt;   def self.each_trie_path(object, path = [], &amp;block)  &lt;br /&gt;&lt;br /&gt;      if object.is_a?(Hash)&lt;br /&gt;         object.each do |key, value|&lt;br /&gt;&lt;br /&gt;            if key == true &amp;&amp; value == {}&lt;br /&gt;               if path == [:root]  # special case for empty string: [[:root], {true=&gt;{}}]&lt;br /&gt;                  yield(path, {true=&gt;{}})&lt;br /&gt;                  next&lt;br /&gt;               end&lt;br /&gt;               yield(path, object)&lt;br /&gt;            end&lt;br /&gt;&lt;br /&gt;            self.each_trie_path(value, [path , key].flatten, &amp;block)&lt;br /&gt;         end&lt;br /&gt;      else &lt;br /&gt;         yield(path, object)&lt;br /&gt;      end&lt;br /&gt;   end &lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Trie&lt;br /&gt;&lt;br /&gt;   @hash = Hash.new_nested_hash&lt;br /&gt;   class &lt;&lt; self; attr_accessor :hash; end    # Trie.hash&lt;br /&gt;&lt;br /&gt;   def initialize&lt;br /&gt;      Trie.hash[:root]&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def add_int(int)   # for int &gt;= 0&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  # integer array; ex: [4,6,2]&lt;br /&gt;      ia.unshift(:root)&lt;br /&gt;      str = ""&lt;br /&gt;      ia.size.times { |x| str &lt;&lt; "[ia.at(#{x})]" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; "[true]"&lt;br /&gt;      eval(str)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def matchi(int)  # match integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }  &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node.fetch(digit,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpi(int)   # match first part of integer&lt;br /&gt;      ia = int.to_s.scan(/[[:digit:]]/).map { |i| i.to_i }&lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ia.each do |digit|&lt;br /&gt;         node = node.fetch(digit,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def &lt;&lt;(word)&lt;br /&gt;      ca = word.split(//u)    # UTF-8 aware character array&lt;br /&gt;      ca.unshift(:root)&lt;br /&gt;      #ca = [:root, *word.split(//u)]&lt;br /&gt;      #ca = [:root].concat(word.split(//u))&lt;br /&gt;      str = ""&lt;br /&gt;      ca.size.times { |x| str &lt;&lt; "[ca.at(#{x})]" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; "[true]"&lt;br /&gt;      #p str     # example: "Trie.hash[ca.at(0)][ca.at(1)][ca.at(2)][ca.at(3)][true]"&lt;br /&gt;      eval(str)&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def match(word)&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      ca.unshift(:root)&lt;br /&gt;      #ca = [:root, *word.split(//u)]&lt;br /&gt;      #ca = [:root].concat(word.split(//u))&lt;br /&gt;      str = ""&lt;br /&gt;      ca.size.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),nil)" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),nil).fetch(ca.at(1),nil).fetch(ca.at(2),nil).fetch(true,nil)"&lt;br /&gt;      ret = eval(str) rescue nil&lt;br /&gt;&lt;br /&gt;=begin&lt;br /&gt;      # alternative&lt;br /&gt;      ca.size.times { |x| str &lt;&lt; ".fetch(ca.at(#{x}),{})" }&lt;br /&gt;      str = "Trie.hash" &lt;&lt; str &lt;&lt; ".fetch(true,nil)"&lt;br /&gt;      #p str   # example: "Trie.hash.fetch(ca.at(0),{}).fetch(ca.at(1),{}).fetch(ca.at(2),{}).fetch(true,nil)"&lt;br /&gt;      ret = eval(str)&lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;      ret == {} ? true : false   # {} is the default value created by Trie.hash or Hash.new_nested_hash respectively&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def match2(word)   # match entire word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node.fetch(char,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      node.fetch(true,nil) ? true : false&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   def mfpw(word)   # match first part of word&lt;br /&gt;      ca = word.split(//u)    &lt;br /&gt;      node = Trie.hash.fetch(:root,nil)&lt;br /&gt;      ca.each do |char|&lt;br /&gt;         node = node.fetch(char,nil)&lt;br /&gt;         return false unless node&lt;br /&gt;      end&lt;br /&gt;      return true&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;trie = Trie.new&lt;br /&gt;trie &lt;&lt; "word"&lt;br /&gt;pp Trie.hash            &lt;br /&gt;p trie.match("word")      &lt;br /&gt;p trie.match("word2")   &lt;br /&gt;trie &lt;&lt; "word2"&lt;br /&gt;p trie.match("word2")   &lt;br /&gt;pp Trie.hash            # {:root=&gt;{"w"=&gt;{"o"=&gt;{"r"=&gt;{"d"=&gt;{true=&gt;{}, "2"=&gt;{true=&gt;{}}}}}}}}&lt;br /&gt;&lt;br /&gt;trie &lt;&lt; ""&lt;br /&gt;p trie.match("")&lt;br /&gt;pp Trie.hash&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;p trie.match2("word")      &lt;br /&gt;p trie.mfpw("wor")     # match first part of word&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;trie.add_int(8512)&lt;br /&gt;pp Trie.hash&lt;br /&gt;p trie.matchi(8512)&lt;br /&gt;p trie.matchi(85)&lt;br /&gt;p trie.mfpi(85)     # match first part of integer&lt;br /&gt;p trie.matchi(51)&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;puts&lt;br /&gt;pp Trie.hash&lt;br /&gt;paths = []&lt;br /&gt;Trie.hash.each_trie_path { |path, value| paths.push([ path ]) }&lt;br /&gt;#Trie.hash.each_trie_path { |path, value| paths.push([ path, value ]) }&lt;br /&gt;#pp paths&lt;br /&gt;puts&lt;br /&gt;paths.each { |x| p x }&lt;br /&gt;puts&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 07 Jun 2008 20:54:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5616</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>String Translit for Rails</title>
      <link>http://snippets.dzone.com/posts/show/5609</link>
      <description>// Translit for Rails using ActiveSupport. (convert &#233; in e and other utf8 codes in latin chars)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  def translit&lt;br /&gt;    ActiveSupport::Multibyte::Handlers::UTF8Handler.normalize(self,:d).split(//u).reject { |e| e.length &gt; 1 }.join&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 06 Jun 2008 22:03:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5609</guid>
      <author>mattetti (Matt Aimonetti)</author>
    </item>
    <item>
      <title>file extension methods</title>
      <link>http://snippets.dzone.com/posts/show/5509</link>
      <description>Allows you to use file extensions as methods&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class File&lt;br /&gt;  # Feel free to add more here, as you need them.&lt;br /&gt;  Extensions = %r=^(txt|rb|markdown|textile|haml|sass|css|html|xhtml)$=i&lt;br /&gt;  &lt;br /&gt;  module Extension&lt;br /&gt;    def method_missing(meth, *args)&lt;br /&gt;      if Extensions =~ meth.to_s&lt;br /&gt;        [self, '.', meth.to_s].join&lt;br /&gt;      else&lt;br /&gt;        super&lt;br /&gt;      end # if&lt;br /&gt;    end # method_missing&lt;br /&gt;  end # Extension&lt;br /&gt;end # File&lt;br /&gt;&lt;br /&gt;class Symbol&lt;br /&gt;  include File::Extension&lt;br /&gt;end&lt;br /&gt;class String&lt;br /&gt;  include File::Extension&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;'myfile'.html&lt;br /&gt;# =&gt; "myfile.html"&lt;br /&gt;&lt;br /&gt;:a_file.rb&lt;br /&gt;# =&gt; "a_file.rb"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 17 May 2008 02:08:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5509</guid>
      <author>elliottcable (elliott cable)</author>
    </item>
    <item>
      <title>Strip accents</title>
      <link>http://snippets.dzone.com/posts/show/5499</link>
      <description>// Strip accents from a string. For example, "Sigur R&#243;s" =&gt; "Sigur Ros".&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def strip_accents(string):&lt;br /&gt;  import unicodedata&lt;br /&gt;  return unicodedata.normalize('NFKD', unicode(string)).encode('ASCII', 'ignore')&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 16 May 2008 00:19:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5499</guid>
      <author>pyninja (Adeel Khan)</author>
    </item>
  </channel>
</rss>
