<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users95</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/95" rel="self"/>
  <title>hungryblank</title>
  <updated>Fri Oct 12 13:39:10 -0700 2007</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor395</id>
    <published>2007-10-12T13:39:10-07:00</published>
    <title>[Ruby] On Rubyize this : 3rd edition</title>
    <content type="html">&lt;p&gt;This solution uses a different regexp&lt;/p&gt;

&lt;p&gt;/#{%w( stupid moron dumbass retard ).join('|')}/i&lt;/p&gt;

&lt;p&gt;generates this regexp&lt;/p&gt;

&lt;p&gt;/stupid|moron|dumbass|retard/i&lt;/p&gt;

&lt;p&gt;that can be used to match any insult in the given sentence&lt;/p&gt;

&lt;pre&gt;def remove_insults(sentence)
  sentence.gsub(/#{%w( stupid moron dumbass retard ).join('|')}/i) { |insult| '*' * insult.size }
end

puts remove_insults(&amp;quot;you truly are a 'Stupid' moron, stupid sir!&amp;quot;)&lt;/pre&gt;</content>
    <author>
      <name>hungryblank</name>
      <email>hungryblank@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/74-rubyize-this-3rd-edition/refactors/395" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor237</id>
    <published>2007-10-03T03:34:27-07:00</published>
    <title>[Ruby] On Random number generator</title>
    <content type="html">&lt;p&gt;and in case you really want just numbers...&lt;/p&gt;

&lt;pre&gt;500.times { puts((1..16).map { rand(10) }.to_s) }&lt;/pre&gt;</content>
    <author>
      <name>hungryblank</name>
      <email>hungryblank@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/47-random-number-generator/refactors/237" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor236</id>
    <published>2007-10-03T03:25:20-07:00</published>
    <title>[Ruby] On Random number generator</title>
    <content type="html">&lt;p&gt;stressing a bit the [] method...&lt;/p&gt;

&lt;pre&gt;500.times { puts((1..16).inject('') { |a,n| a &amp;lt;&amp;lt; '1234567890'[rand(10),1] }) }

# or

500.times { puts((1..16).map { '1234567890'[rand(10),1] }.join) }&lt;/pre&gt;</content>
    <author>
      <name>hungryblank</name>
      <email>hungryblank@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/47-random-number-generator/refactors/236" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor235</id>
    <published>2007-10-03T02:50:12-07:00</published>
    <title>[Ruby] On Show a default value if object or field is empty?</title>
    <content type="html">&lt;p&gt;As a first step you can avoid using the block in the method.
&lt;br /&gt;The following doesn't need a block.
&lt;br /&gt;Note that in your code if the object is not nil and the method doesn't return blank the method is called twice on the object.&lt;/p&gt;

&lt;p&gt;Below there's a code that doesn't use blocks and calls the method only once.&lt;/p&gt;

&lt;p&gt;A better solution would be to have your 'object' to return the default value for the method instead of the blank.
&lt;br /&gt;Other than that the default values should be constants defined in your classes so they'll be easy to change when needed.&lt;/p&gt;

&lt;pre&gt;# samples
# show_value_or_default(user, :phone)
# show_value_or_default(user2, :phone, &amp;quot;leeg&amp;quot;)

def show_value_or_default(object, method, default = &amp;quot;-&amp;quot;)
  return value if object.respond_to?(method) &amp;amp;&amp;amp; !(value = object.send(method)).blank?
  default
end&lt;/pre&gt;</content>
    <author>
      <name>hungryblank</name>
      <email>hungryblank@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/50-show-a-default-value-if-object-or-field-is-empty/refactors/235" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor172</id>
    <published>2007-10-01T15:40:24-07:00</published>
    <title>[Ruby] On Grouping an array of hashes</title>
    <content type="html">&lt;p&gt;I thought for you it was important array_of_arrays_of_hashes[idx] would return the array with the idx caller id.
&lt;br /&gt;But is not because after you apply the compact method and this breaks the relationship between array_of_arrays_of_hashes index and callid.
&lt;br /&gt;In this case the code below should be enough.&lt;/p&gt;

&lt;pre&gt;array_of_arrays_of_hashes = array_of_hashes.map { |e| e[:callid] }.uniq.inject([]) do |grouped_array, callid|
  grouped_array &amp;lt;&amp;lt; array_of_hashes.select { |h| h[:callid] == callid }
end&lt;/pre&gt;</content>
    <author>
      <name>hungryblank</name>
      <email>hungryblank@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/39-grouping-an-array-of-hashes/refactors/172" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor153</id>
    <published>2007-10-01T09:38:19-07:00</published>
    <title>[Ruby] On compound interest</title>
    <content type="html">&lt;p&gt;This is just an example on how you can wrap the calculation in a class.
&lt;br /&gt;one method provide the calculation of the value at any time in the future.&lt;/p&gt;

&lt;pre&gt;class Investment &amp;lt; Struct.new(:start_year, :start_value, :interest_rate)

  def value_at(year)
    start_value * (1 + interest_rate) ** (year - start_year)
  end

end

# # Sample usage
# we invest 100 in 1990 at 5% interest and want to know the value per
# every year from 1990 to 2005

i = Investment.new(1990, 100, 0.05)
(1990..2005).each { |year| puts i.value_at(year) }&lt;/pre&gt;</content>
    <author>
      <name>hungryblank</name>
      <email>hungryblank@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/34-compound-interest/refactors/153" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor97</id>
    <published>2007-09-28T04:39:41-07:00</published>
    <title>[Ruby] On Extracting style from a CSS file</title>
    <content type="html">&lt;p&gt;I just played some mins around the regexp with no luck
&lt;br /&gt;so I came out with this&lt;/p&gt;

&lt;p&gt;CssObj.extract_file(filename)&lt;/p&gt;

&lt;p&gt;will return an array of objects, each object respond to 'selector' and 'attributes' methods.
&lt;br /&gt;Some major cleaning is required&lt;/p&gt;

&lt;pre&gt;CssObj = Struct.new(:selector, :attributes)
class CssObj
  def self.extract_file(filename)
    css = File.read(filename)
    tokens = css.split(/\}/m)
    tokens.map { |token| CssObj.new(*token.split(/\{/m)) }
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>hungryblank</name>
      <email>hungryblank@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/16-extracting-style-from-a-css-file/refactors/97" rel="alternate"/>
  </entry>
</feed>

