<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Ovhaag's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 05:57:37 GMT</pubDate>
    <description>DZone Snippets: Ovhaag's Code Snippets</description>
    <item>
      <title>convert between characters and values</title>
      <link>http://snippets.dzone.com/posts/show/4041</link>
      <description>// character to ASCII value: use ?&lt;br /&gt;&lt;code&gt;&lt;br /&gt;?a     # =&gt; 97&lt;br /&gt;?\n    # =&gt; 10&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// string to integer: use []&lt;br /&gt;&lt;code&gt;&lt;br /&gt;'a'[0]        # =&gt; 97&lt;br /&gt;'hallo'[1]    # =&gt; 97&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// integer / number to character: use .chr&lt;br /&gt;&lt;code&gt;&lt;br /&gt;97.chr     # =&gt; "a"&lt;br /&gt;10.chr     # =&gt; "\n"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;//more info: "Ruby Cookbook", O'Reilly</description>
      <pubDate>Fri, 18 May 2007 15:34:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4041</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>radio button, boolean selection</title>
      <link>http://snippets.dzone.com/posts/show/3564</link>
      <description>// radio buttons generated with form helper&lt;br /&gt;// selection by boolean&lt;br /&gt;// attention interger numbers (0, 1) don't work&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    &lt;div class="row"&gt;&lt;br /&gt;      &lt;dl&gt;&lt;br /&gt;        &lt;dt&gt;Owner over 18?&lt;/dt&gt;&lt;br /&gt;        &lt;dd&gt;&lt;br /&gt;        &lt;%= radio_button( :guarantor, :over_eighteen.to_s, true )%&gt; Yes &amp;nbsp;&amp;nbsp;&lt;br /&gt;        &lt;%= radio_button( :guarantor, :over_eighteen, false )%&gt; No&lt;br /&gt;        &lt;/dd&gt;&lt;br /&gt;      &lt;/dl&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 22 Feb 2007 13:43:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3564</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>create object fom underscore_syntax</title>
      <link>http://snippets.dzone.com/posts/show/3043</link>
      <description>// camelize - from underscore_syntax to Uppercas/ClassSyntax&lt;br /&gt;// constantize - create instance&lt;br /&gt;// (params[:property]) -- read data from web-form&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    @property = property_type.camelize.constantize.new(params[:property])&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 27 Nov 2006 05:15:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3043</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>transform normal projet to rails project in radrails</title>
      <link>http://snippets.dzone.com/posts/show/2989</link>
      <description>// in radrails when you import a project from subversion&lt;br /&gt;// it is a normal project&lt;br /&gt;&lt;br /&gt;// in the menu I found no way to transform it&lt;br /&gt;// but so it worked for me: &lt;br /&gt;&lt;br /&gt;// 1. edit the .project file and copy the natueres block &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;	&lt;natures&gt;&lt;br /&gt;		&lt;nature&gt;org.radrails.rails.ui.railsnature&lt;/nature&gt;&lt;br /&gt;		&lt;nature&gt;org.rubypeople.rdt.core.rubynature&lt;/nature&gt;&lt;br /&gt;	&lt;/natures&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// 2. copy the buildSpec block too&lt;br /&gt;// (I'm not sure if this is necessary, I did both and it worked)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;	&lt;buildSpec&gt;&lt;br /&gt;		&lt;buildCommand&gt;&lt;br /&gt;			&lt;name&gt;org.rubypeople.rdt.core.rubybuilder&lt;/name&gt;&lt;br /&gt;			&lt;arguments&gt;&lt;br /&gt;			&lt;/arguments&gt;&lt;br /&gt;		&lt;/buildCommand&gt;&lt;br /&gt;	&lt;/buildSpec&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// 3. restart radrails and &lt;br /&gt;// 4. add a webrick-server to the project &lt;br /&gt;// and you are done&lt;br /&gt;</description>
      <pubDate>Sun, 12 Nov 2006 09:36:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2989</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>find out the current url / uri in *.rhtml file</title>
      <link>http://snippets.dzone.com/posts/show/2841</link>
      <description>// find out the current url / uri in *.rhtml file&lt;br /&gt;// is quite simple with the request object&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% page = request.request_uri %&gt;&lt;br /&gt;page: &lt;%= page %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;// but then different urls mean the same page &lt;br /&gt;// (../admin = ../admin/ = ../admin/index = ..admin/index/)&lt;br /&gt;// and maybe the id is unwanted too (../admin/show/8)&lt;br /&gt;// so below is an alternative with control on which parameter is used&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %&gt;&lt;br /&gt;page: &lt;%= page %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 16 Oct 2006 19:40:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2841</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>rails generator syntax</title>
      <link>http://snippets.dzone.com/posts/show/2800</link>
      <description>// generate application&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rails projectname&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate migration.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate migration migration_name&lt;br /&gt;ruby script/generate migration add_price&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate model&lt;br /&gt;// we can give a list of columns and types&lt;br /&gt;// :string, :text, :integer, :decimal, :float, :date, ...&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate model model_name&lt;br /&gt;ruby script/generate model user name:string hashed_password:string salt:string&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate controller&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate controller controller_name method_name(s)&lt;br /&gt;ruby script/generate controller store index&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// generate scaffold&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby script/generate scaffold model_name controller_name&lt;br /&gt;ruby script/generate scaffold product admin&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 08 Oct 2006 18:52:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2800</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>redefine [] operator in ruby</title>
      <link>http://snippets.dzone.com/posts/show/2799</link>
      <description>// the parameter is written behind the operator in parantheses&lt;br /&gt;// the parameter must be an integer/fixnum&lt;br /&gt;//&lt;br /&gt;// i.e. &lt;br /&gt;// def [](chunk)&lt;br /&gt;//   @internal_id_hash[chunk.chunk_id]&lt;br /&gt;// end&lt;br /&gt;// is not possible&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class ChunkList&lt;br /&gt;  ..&lt;br /&gt;&lt;br /&gt;  def [](chunk_id)&lt;br /&gt;    @internal_id_hash[chunk_id]&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 08 Oct 2006 15:48:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2799</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>singleton in ruby</title>
      <link>http://snippets.dzone.com/posts/show/2794</link>
      <description>// simple singleton&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Singleton &lt;br /&gt;  private_class_method :new&lt;br /&gt;  @@singleton = nil&lt;br /&gt;  &lt;br /&gt;  def Singleton.create&lt;br /&gt;    @@singleton = new unless @@singleton&lt;br /&gt;    @@singleton&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 07 Oct 2006 14:47:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2794</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>count characters</title>
      <link>http://snippets.dzone.com/posts/show/2778</link>
      <description>// Sample Output, "character (byte) occurs n times", 32 = space&lt;br /&gt;// s (115) occurs 3 times&lt;br /&gt;// n (110) occurs 1 times&lt;br /&gt;// ..&lt;br /&gt;//   (32) occurs 3 times&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Counter&lt;br /&gt;  &lt;br /&gt;def initialize()&lt;br /&gt;  @characters = Hash.new(0)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def read()&lt;br /&gt;  @text = IO.read("text.txt")&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def count_chars&lt;br /&gt;  @text.each_byte do |ch|&lt;br /&gt;  @characters[ch] +=1&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def report  &lt;br /&gt;  @characters.each do |key, value|&lt;br /&gt;    puts "#{key.chr} (#{key}) occurs #{value} times"&lt;br /&gt;  end  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// usage&lt;br /&gt;&lt;code&gt;&lt;br /&gt;count = Counter.new()&lt;br /&gt;count.read&lt;br /&gt;count.count_chars&lt;br /&gt;count.report&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 04 Oct 2006 21:15:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2778</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
    <item>
      <title>show image in selection-box</title>
      <link>http://snippets.dzone.com/posts/show/2766</link>
      <description>// works only in mozilla&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;    option[value=ohcon]:before { content:url("ohc.gif"); }&lt;br /&gt;    option[value=baerlin]:before { content:url("baer.gif"); }&lt;br /&gt;    option[value=zendo]:before { content:url("zendo.gif"); }&lt;br /&gt;&lt;br /&gt;--&gt;&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;..&lt;br /&gt;&lt;br /&gt;Select Company&lt;br /&gt;&lt;select&gt;&lt;br /&gt;  &lt;option value="ohcon"&gt;Oliver Haag IT consulting&lt;/option&gt;&lt;br /&gt;  &lt;option value="baerlin"&gt;B&#228;rlin Partners&lt;/option&gt;&lt;br /&gt;  &lt;option value="zendo"&gt;Zendo-Marketing&lt;/option&gt;  &lt;br /&gt;&lt;/select&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 02 Oct 2006 20:45:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2766</guid>
      <author>ovhaag (Oliver Haag)</author>
    </item>
  </channel>
</rss>
