<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: key code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 18:50:14 GMT</pubDate>
    <description>DZone Snippets: key code</description>
    <item>
      <title>Reference of keyCodes </title>
      <link>http://snippets.dzone.com/posts/show/5320</link>
      <description>&lt;code&gt;&lt;br /&gt;    switch (oEvent.keyCode) {&lt;br /&gt;       case 38: //up arrow  &lt;br /&gt;       case 40: //down arrow&lt;br /&gt;       case 37: //left arrow&lt;br /&gt;       case 39: //right arrow&lt;br /&gt;       case 33: //page up  &lt;br /&gt;       case 34: //page down  &lt;br /&gt;       case 36: //home  &lt;br /&gt;       case 35: //end                  &lt;br /&gt;       case 13: //enter  &lt;br /&gt;       case 9: //tab  &lt;br /&gt;       case 27: //esc  &lt;br /&gt;       case 16: //shift  &lt;br /&gt;       case 17: //ctrl  &lt;br /&gt;       case 18: //alt  &lt;br /&gt;       case 20: //caps lock&lt;br /&gt;       case 8: //backspace  &lt;br /&gt;       case 46: //delete&lt;br /&gt;           return true;&lt;br /&gt;           break;&lt;br /&gt;&lt;br /&gt;       default: &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Note: When capturing combination keys there is dedicated boolean attributes for each of the special keys (CTRL, SHIFT, ALT).&lt;br /&gt;Reference: &lt;a href="http://www.sitepoint.com/article/life-autocomplete-textboxes/3"&gt;Make Life Easy With Autocomplete Textboxes [JavaScript &amp; AJAX Tutorials]&lt;/a&gt; [sitepoint.com]</description>
      <pubDate>Wed, 02 Apr 2008 22:31:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5320</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Copy Public Key To Host In One Line</title>
      <link>http://snippets.dzone.com/posts/show/5258</link>
      <description>&lt;code&gt;&lt;br /&gt;ssh username@host "echo `cat ~/.ssh/id_dsa.pub` &gt;&gt; ~/.ssh/authorized_keys"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 19 Mar 2008 18:23:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5258</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Random Characters with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4667</link>
      <description>Use for a password or salt or whatever...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(0..25).inject('') { |r, i| r &lt;&lt; rand(93) + 33 }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 18 Oct 2007 03:24:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4667</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Api key (or any kind of key) generator</title>
      <link>http://snippets.dzone.com/posts/show/4657</link>
      <description>Generates a random key for API neatness.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class KeyGenerator&lt;br /&gt;  require "digest/sha1"&lt;br /&gt;  def self.generate(length = 10)&lt;br /&gt;    Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)[1..length]&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 15 Oct 2007 13:03:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4657</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
    <item>
      <title>Recursively dump imbricated Map of Maps</title>
      <link>http://snippets.dzone.com/posts/show/4531</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    public void dumpMapOfMap(Map map) {&lt;br /&gt;        Set s = map.entrySet();&lt;br /&gt;        Iterator sit = s.iterator();&lt;br /&gt;        boolean isFirst = true;&lt;br /&gt;&lt;br /&gt;        while (sit.hasNext()) {&lt;br /&gt;            Map.Entry elem = (Map.Entry)sit.next();&lt;br /&gt;            String key = (String)elem.getKey();&lt;br /&gt;            Object value = elem.getValue();&lt;br /&gt;&lt;br /&gt;            if (value instanceof String) {&lt;br /&gt;                // recursivity stop condition&lt;br /&gt;                System.out.print(key);&lt;br /&gt;                System.out.print(" : ");&lt;br /&gt;                System.out.println(value);&lt;br /&gt;            } else {&lt;br /&gt;                if (!isFirst) {&lt;br /&gt;                    System.out.println("");&lt;br /&gt;                } else {&lt;br /&gt;                    isFirst = false;&lt;br /&gt;                }&lt;br /&gt;                System.out.println(key);&lt;br /&gt;                Map valueMap = (Map)elem.getValue();&lt;br /&gt;                dumpMapOfMap(valueMap);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 12:58:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4531</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Using 64 bit primary keys with migrations</title>
      <link>http://snippets.dzone.com/posts/show/4422</link>
      <description>I've got an application which may need more IDs than the standard 32 bit ones typically provide.  MySQL has a BIGINT variable type, but it is a little tricky to convince Rails and Migrations to use it.  The instructions are for MySQL, but should be easily transferable to other DBs.&lt;br /&gt;&lt;br /&gt;1.  Monkeypatch ActiveRecord::ConnectionAdapters::MysqlAdapter#native_database_types by appending it to config/environment.rb and add two new types.  I've named them int64 and int64_pk&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ActiveRecord::ConnectionAdapters::MysqlAdapter&lt;br /&gt;  def native_database_types #:nodoc:&lt;br /&gt;    {&lt;br /&gt;      :primary_key =&gt; "int(11) DEFAULT NULL auto_increment PRIMARY KEY",&lt;br /&gt;      :int64_pk    =&gt; "bigint DEFAULT NULL auto_increment PRIMARY KEY",&lt;br /&gt;      :int64       =&gt; { :name =&gt; "bigint" },&lt;br /&gt;      :string      =&gt; { :name =&gt; "varchar", :limit =&gt; 255 },&lt;br /&gt;      :text        =&gt; { :name =&gt; "text" },&lt;br /&gt;      :integer     =&gt; { :name =&gt; "int", :limit =&gt; 11 },&lt;br /&gt;      :float       =&gt; { :name =&gt; "float" },&lt;br /&gt;      :decimal     =&gt; { :name =&gt; "decimal" },&lt;br /&gt;      :datetime    =&gt; { :name =&gt; "datetime" },&lt;br /&gt;      :timestamp   =&gt; { :name =&gt; "datetime" },&lt;br /&gt;      :time        =&gt; { :name =&gt; "time" },&lt;br /&gt;      :date        =&gt; { :name =&gt; "date" },&lt;br /&gt;      :binary      =&gt; { :name =&gt; "blob" },&lt;br /&gt;      :boolean     =&gt; { :name =&gt; "tinyint", :limit =&gt; 1 },&lt;br /&gt;    }&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;2.  In the table creation migration, create the table WITHOUT a primary key column, and then add the column manually, referencing int64_pk above:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;create_table :slices, :id =&gt; false do |t|&lt;br /&gt;       t.column :cheese_id, :integer&lt;br /&gt;       t.column :plant, :string&lt;br /&gt;       t.column :date, :datetime&lt;br /&gt;     end&lt;br /&gt;    add_column :events, :id, :int64_pk&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;3.  In tables referencing this one, mark the foreign_id fields as int64:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;create_table :fondues do |t|&lt;br /&gt;      t.column :cheese_id, :int64&lt;br /&gt;      t.column :party_id, :integer&lt;br /&gt;      t.column :kirsch_id, :integer&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;4.  It turns out that the c-based mysql bindings do NOT do let you set primary keys via the sequence because it coerces the&lt;br /&gt;insert_id into 32 bits at mysql.c:532.  There are two way to fix this:&lt;br /&gt;&lt;br /&gt;a.  Use the pure-ruby gem (ie, remove the c-gem)&lt;br /&gt;&lt;br /&gt;b.  Patch and rebuild the c-gem by changing line 352 (in version 2.7):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;   return INT2NUM(mysql_insert_id(GetHandler(obj)));&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Extending this to use LL2NUM solves the issue:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;   return LL2NUM(mysql_insert_id(GetHandler(obj)));&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;5.  One last thing, in environment.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # Use SQL instead of Active Record's schema dumper when creating the test database.&lt;br /&gt;  # This is necessary if your schema can't be completely dumped by the schema dumper,&lt;br /&gt;  # like if you have constraints or database-specific column types&lt;br /&gt;  config.active_record.schema_format = :sql&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We need to deal with sql, rather than ruby, schemas.&lt;br /&gt;&lt;br /&gt;----------------------&lt;br /&gt;&lt;br /&gt;Note that I've decided that ONLY the cheese table needs 64bit IDs -- other references are still integers.  If you wanted ALL primary keys to be 64 bits, you could have the monkeypatch look like:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class ActiveRecord::ConnectionAdapters::MysqlAdapter&lt;br /&gt;  def native_database_types #:nodoc:&lt;br /&gt;    {&lt;br /&gt;      :primary_key    =&gt; "bigint DEFAULT NULL auto_increment PRIMARY KEY",&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 15 Aug 2007 13:23:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4422</guid>
      <author>sanj (Sanjay Vakil)</author>
    </item>
    <item>
      <title>Simulating a foreign key constraint in ActiveRecord (Ruby on Rails)</title>
      <link>http://snippets.dzone.com/posts/show/3400</link>
      <description>Say for example we have products and a business rule that every product must have a category. Before ActiveRecord we would have done this with a NOT NULL foreign key constraint at the database level. &lt;br /&gt;&lt;br /&gt;It wasn't entirely obvious (to me) how to achieve this in an ActiveRecord model - you need *both* of the following two lines:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  validates_presence_of :category, :message =&gt; " must be specified" &lt;br /&gt;  validates_associated :category&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Hope this helps someone. </description>
      <pubDate>Thu, 01 Feb 2007 00:55:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3400</guid>
      <author>eddiewould (Eddie)</author>
    </item>
    <item>
      <title>Google Maps API key for http://localhost/</title>
      <link>http://snippets.dzone.com/posts/show/3201</link>
      <description>// you just paste this in the script tag per the google documentation. I've stuck it hear because I use it a lot for local development.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 25 Dec 2006 01:03:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3201</guid>
      <author>duncanponting (Duncan Ponting)</author>
    </item>
    <item>
      <title>Convert an array to a hash with key definitions</title>
      <link>http://snippets.dzone.com/posts/show/2330</link>
      <description>First of all I'm relatively new to Ruby, so if there's a easier way&lt;br /&gt;of doing this I would love to know about it :)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def to_h(key_definition)&lt;br /&gt;    result_hash = Hash.new()&lt;br /&gt;    &lt;br /&gt;    counter = 0&lt;br /&gt;    key_definition.each do |definition|&lt;br /&gt;      if not self[counter] == nil then&lt;br /&gt;        result_hash[definition] = self[counter].strip&lt;br /&gt;      else&lt;br /&gt;        # Insert the key definition with a empty value.&lt;br /&gt;        # Because we probably still want the hash to contain the key.&lt;br /&gt;        result_hash[definition] = ""&lt;br /&gt;      end&lt;br /&gt;      # For some reason counter.next didn't work here....&lt;br /&gt;      counter = counter + 1&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    return result_hash&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use it like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;key_definitions = Array['foo', 'bar', 'foobar', 'extra']&lt;br /&gt;some_values     = Array['bla', 99, 'blabla']&lt;br /&gt;&lt;br /&gt;some_values.to_h(key_definitions)   # =&gt; {'foo' =&gt; 'bla', 'bar' =&gt; 99, 'foobar' =&gt; 'blabla', 'extra' =&gt; ''}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Jul 2006 15:24:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2330</guid>
      <author>El_Oy (Eloy Duran)</author>
    </item>
    <item>
      <title>Extracting all keys from a multi-dimensional hash</title>
      <link>http://snippets.dzone.com/posts/show/1908</link>
      <description>Extract all complete key sequences from a multi-dimensional hash (with the last key not pointing to another hash; cf. h[1][2][3] vs h[1][2][3][4] below).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;&lt;br /&gt;   def extract_keys&lt;br /&gt;&lt;br /&gt;      keys = []&lt;br /&gt;&lt;br /&gt;      each_pair do |k1, v1|&lt;br /&gt;&lt;br /&gt;         if v1.is_a?(Hash)&lt;br /&gt;&lt;br /&gt;            v1.each_pair { |k2, v2|&lt;br /&gt;               if !v2.is_a?(Hash) then keys &lt;&lt; [k1, k2]; next end&lt;br /&gt;            v2.each_pair { |k3, v3|&lt;br /&gt;               if !v3.is_a?(Hash) then keys &lt;&lt; [k1, k2, k3]; next end&lt;br /&gt;            v3.each_pair { |k4, v4|&lt;br /&gt;               if !v4.is_a?(Hash) then keys &lt;&lt; [k1, k2, k3, k4]; next end&lt;br /&gt;            v4.each_pair { |k5, v5|&lt;br /&gt;               if !v5.is_a?(Hash) then keys &lt;&lt; [k1, k2, k3, k4, k5]; next end&lt;br /&gt;            v5.each_pair { |k6, v6|&lt;br /&gt;               if !v6.is_a?(Hash) then keys &lt;&lt; [k1, k2, k3, k4, k5, k6]; next end&lt;br /&gt;               # add more v[n].each_pair ... loops to process more hash dimensions&lt;br /&gt;            } } } } }      # "}" * 5&lt;br /&gt;&lt;br /&gt;         else&lt;br /&gt;            keys &lt;&lt; [k1]&lt;br /&gt;         end&lt;br /&gt;&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      keys&lt;br /&gt;&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   def all_values&lt;br /&gt;      extract_keys.map do |subar|&lt;br /&gt;         key = ""&lt;br /&gt;         subar.size.times { |i| key &lt;&lt; "[subar[#{i}]]" }&lt;br /&gt;         hash_str = "self" &lt;&lt; key &lt;&lt; " rescue nil"   # example: "self[subar[0]][subar[1]][subar[2]][subar[3]] rescue nil"&lt;br /&gt;         hash_value = eval(hash_str) &lt;br /&gt;      end&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#-------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   # Find every path and it's value in a Hash, http://snippets.dzone.com/posts/show/3565&lt;br /&gt;   # Author: Florian A&#223;mann&lt;br /&gt;&lt;br /&gt;   def each_path&lt;br /&gt;      raise ArgumentError unless block_given?&lt;br /&gt;      self.class.each_path(self) { |path, object| yield(path, object) }&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   protected&lt;br /&gt;   #def self.each_path(object, path = '', &amp;block)&lt;br /&gt;   def self.each_path(object, path = [], &amp;block)   # alternative&lt;br /&gt;      if object.is_a?(Hash)&lt;br /&gt;         object.each do |key, value|&lt;br /&gt;            #self.each_path(value, "#{ path }#{ key }/", &amp;block)&lt;br /&gt;            self.each_path(value, [path , key].flatten, &amp;block)   # alternative&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;h = {"a"=&gt;"b", "c"=&gt;"d", 1=&gt;{2=&gt;{"e"=&gt;"f", 3=&gt;{4=&gt;"value"}}}} &lt;br /&gt;&lt;br /&gt;puts h[1][2].class          # Hash&lt;br /&gt;puts h[1][2]["e"].class     # String&lt;br /&gt;&lt;br /&gt;extracted_keys = h.extract_keys&lt;br /&gt;puts extracted_keys.inspect         # [["a"], [1, 2, "e"], [1, 2, 3, 4], ["c"]]&lt;br /&gt;&lt;br /&gt;puts h[1][2].has_key?("e")                 # true&lt;br /&gt;puts extracted_keys.include?([1, 2, "e"])  # true&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h = {700=&gt;{4=&gt;"value"}, "a"=&gt;"b", 3=&gt;{4=&gt;"value"}, "c"=&gt;"d", 1=&gt;{2=&gt;{"e"=&gt;"f", 3=&gt;{4=&gt;"value"}, 300=&gt;{4=&gt;"value"}}}} &lt;br /&gt;p h&lt;br /&gt;p h.extract_keys    #=&gt; [["a"], [1, 2, "e"], [1, 2, 300, 4], [1, 2, 3, 4], ["c"], [700, 4], [3, 4]]&lt;br /&gt;p h.all_values      #=&gt; ["b", "f", "value", "value", "d", "value", "value"]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#-----------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;paths = []&lt;br /&gt;complex_hash = Hash[&lt;br /&gt;  :a =&gt; { :aa =&gt; '1', :ab =&gt; '2' },&lt;br /&gt;  :b =&gt; { :ba =&gt; '3', :bb =&gt; '4' }&lt;br /&gt;]&lt;br /&gt;complex_hash.each_path { |path, value| paths &lt;&lt; [ path, value ] }&lt;br /&gt;&lt;br /&gt;p paths    # =&gt; [[[:b, :ba], "3"], [[:b, :bb], "4"], [[:a, :ab], "2"], [[:a, :aa], "1"]]&lt;br /&gt;puts&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;h = {"a"=&gt;"b", "c"=&gt;"d", 1=&gt;{2=&gt;{"e"=&gt;"f", 3=&gt;{4=&gt;"value"}}}} &lt;br /&gt;h = {"a"=&gt;"b", "l" =&gt; lambda { |x| x+1 }, 1=&gt;{2=&gt;{"e"=&gt;"f", 3=&gt;{4=&gt;"value"}}}} &lt;br /&gt;h = {700=&gt;{4=&gt;"value"}, "a"=&gt;"b", nil =&gt; "NILVALUE", 3=&gt;{4=&gt;"value"}, "c"=&gt;"d", 1=&gt;{2=&gt;{"e"=&gt;"f", 3=&gt;{4=&gt;"value"}, 300=&gt;{4=&gt;"value"}}}} &lt;br /&gt;&lt;br /&gt;p h&lt;br /&gt;p h.extract_keys&lt;br /&gt;&lt;br /&gt;keys = []&lt;br /&gt;h.each_path { |path, value| keys &lt;&lt; path }&lt;br /&gt;p keys   # complete key sequences (with last key not pointing to another hash)&lt;br /&gt;&lt;br /&gt;paths = []&lt;br /&gt;h.each_path { |path, value| paths &lt;&lt; [ path, value ] }&lt;br /&gt;p paths   # complete key sequences plus values&lt;br /&gt;&lt;br /&gt;vals = []&lt;br /&gt;h.each_path { |path, value| vals &lt;&lt; value }&lt;br /&gt;p vals   # all values of complete key sequences&lt;br /&gt;&lt;br /&gt;p h.all_values   # same&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Apr 2006 20:29:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1908</guid>
      <author>ntk ()</author>
    </item>
  </channel>
</rss>
