<?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:users1557</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1557" rel="self"/>
  <title>Martin Pl&#195;&#182;ger</title>
  <updated>Fri Aug 27 14:35:45 -0700 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor527887</id>
    <published>2010-08-27T14:35:45-07:00</published>
    <title>[Ruby] On Language Selection</title>
    <content type="html">&lt;p&gt;I'd also put it in a helper (putting logic directly in the view isn't very nice to maintain). I would not set the html_options-hash with an empty class though... IMHO&lt;/p&gt;

&lt;p&gt;BTW: if/unless expression can be put in parentheses and used as a parameter in method calls. Also have a look at the link_to_if and link_to_unless methods...&lt;/p&gt;

&lt;pre&gt;def locale_links
  [:pl, :en].map { |locale| link_to locale.to_s.upcase, {:locale =&amp;gt; locale}, (:class =&amp;gt; :language_selected if I18n.locale == locale)}.join ' | '
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1417-language-selection/refactors/527887" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor525807</id>
    <published>2010-08-16T17:39:06-07:00</published>
    <title>[Ruby] On How to make it more idiomatic?</title>
    <content type="html">&lt;p&gt;merge! is more efficient than merge (creates copies of the old hash). Just a tiny change...&lt;/p&gt;

&lt;pre&gt;array.inject({}) { |hash, key| hash.merge! key =&amp;gt; func(key) }&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1402-how-to-make-it-more-idiomatic/refactors/525807" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor525806</id>
    <published>2010-08-16T17:30:00-07:00</published>
    <title>[Ruby] On Calculate sum of items in array of hashes</title>
    <content type="html">&lt;p&gt;You don't need to pass the zero to inject. It then uses the first value of items for the initial value of &amp;quot;memo&amp;quot; (this is okay when adding the values). And you don't need to prefix items with self (a matter of taste).
&lt;br /&gt;Another thing is the double usage of &amp;quot;sum&amp;quot; for the block-variable and the result (it works because the outer variable is not initialized till the block finishes. If it was initialized before the block the behaviour depends on your Ruby-version 1.8 vs. 1.9).&lt;/p&gt;

&lt;pre&gt;sum = items.inject { |memo, item| memo + item.fragments.count }&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1401-calculate-sum-of-items-in-array-of-hashes/refactors/525806" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor521551</id>
    <published>2010-07-22T07:32:39-07:00</published>
    <title>[Ruby] On better way?</title>
    <content type="html">&lt;p&gt;@Adam: you call the block even if session[:pos_activation] evaluates to false unlike the original code. (Don't know if it matters or has side-effects)&lt;/p&gt;

&lt;pre&gt;def is_pos_activation?
  return yield if block_given? &amp;amp;&amp;amp; session[:pos_activation]
  session[:pos_activation]
end

#OR

def is_pos_activation?
  (block_given? &amp;amp;&amp;amp; session[:pos_activation]) ? yield : session[:pos_activation]
end

#OR

def is_pos_activation?
  session[:pos_activation] &amp;amp;&amp;amp; (block_given? ? yield : true)
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1363-better-way/refactors/521551" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor513106</id>
    <published>2010-06-08T16:55:21-07:00</published>
    <title>[Ruby] On how to refactor tricky logic involving consecutive sets?</title>
    <content type="html">&lt;p&gt;Hope, I understood what you meant (text not code):
&lt;br /&gt;10 =&amp;gt; 10
&lt;br /&gt;20 =&amp;gt; 20
&lt;br /&gt;30 =&amp;gt; 30
&lt;br /&gt;40 =&amp;gt; 10
&lt;br /&gt;50 =&amp;gt; 20
&lt;br /&gt;...
&lt;br /&gt;Everything else (not a multiple of ten and &amp;lt;= zero) =&amp;gt; 0&lt;/p&gt;

&lt;pre&gt;def check_win_streak streak
  return if streak % 10 != 0 || streak &amp;lt;= 0
  award_streak_badge [10, 20, 30][streak / 10 % 3 - 1]
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1314-how-to-refactor-tricky-logic-involving-consecutive-sets/refactors/513106" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor512029</id>
    <published>2010-06-01T14:58:18-07:00</published>
    <title>[Ruby] On Generate post query for array</title>
    <content type="html">&lt;p&gt;Rails has a Hash#to_param-method&lt;/p&gt;

&lt;pre&gt;{ :param_one =&amp;gt; 'foo', :param_two =&amp;gt; 'bar' }.to_param
&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1305-generate-post-query-for-array/refactors/512029" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor510690</id>
    <published>2010-05-27T18:05:57-07:00</published>
    <title>[Ruby] On I'm having trouble thinking in Ruby today.</title>
    <content type="html">

&lt;pre&gt;text =
&amp;quot;75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23&amp;quot;

# convert this text into an array of Fixnum arrays (this is Ruby)
rows = text.lines.map { |line| line.split.map &amp;amp;:to_i }.reverse

# start at the bottom of the pyramid and reduce it to a max
# empty Array is just fine, because the 1st row is not &amp;quot;maxed&amp;quot; anyway
max = rows.inject [] do |maxes, row|
  # add the elements of the maxes array to rows
  maxes.each.with_index { |max, index| row[index] += max }
  # make the next maxes array from pairs of 
  row.length &amp;gt; 1 ? row.each_cons(2).map(&amp;amp;:max) : row
end

puts max&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1298-i-m-having-trouble-thinking-in-ruby-today/refactors/510690" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor499780</id>
    <published>2010-04-29T04:44:45-07:00</published>
    <title>[Ruby] On HTML query string</title>
    <content type="html">&lt;p&gt;Hash#map or Hash#collect works fine for that.&lt;/p&gt;

&lt;pre&gt;b = {:a =&amp;gt; 'option1', :b =&amp;gt; 'option2' }
b.map { |key, value| &amp;quot;#{key}=#{value}&amp;quot; }.join '&amp;amp;'&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1277-html-query-string/refactors/499780" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor498395</id>
    <published>2010-04-26T06:50:01-07:00</published>
    <title>[Ruby] On Can I do this while loop in a better way ?</title>
    <content type="html">&lt;p&gt;I'm not quite satisfied with the indexes inside the #collect-method. But I think this should be resolved by improving the scraping of the html-code before the loop, not by the loop later on.&lt;/p&gt;

&lt;pre&gt;require 'rubygems'
require 'hpricot'
require 'open-uri'

doc = Hpricot(open('http://www.mpi.mb.ca/salvage/auction.asp?salenm=1694'))

cars = doc.search(&amp;quot;tr &amp;gt; td &amp;gt; font&amp;quot;).collect { |i| i.inner_html }

car_array = cars.each_slice(7).collect do |car|
  :num =&amp;gt; car[0], :year =&amp;gt; car[1],
  :make_model =&amp;gt; car[2], vin: =&amp;gt; car[3]
  :engine =&amp;gt; car[4], :status =&amp;gt; car[5],
  :km =&amp;gt; car[6]
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1272-can-i-do-this-while-loop-in-a-better-way/refactors/498395" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor485576</id>
    <published>2010-03-21T21:43:07-07:00</published>
    <title>[Ruby] On Exception Handling in Ruby</title>
    <content type="html">&lt;p&gt;I just recycled Yury's great refactoring and used Array instead of hashes. Hope it is not less readable.&lt;/p&gt;

&lt;pre&gt;mappings = ['body', 1], ['height', 2], ['status', 7], ['country', 8, 1], ['zip', 9], ['location', 7]
mappings.each do |key, content_index, regexp_index| 
  data[key] = content[content_index].split(/\W{2,}/)[regexp_index || 0].strip rescue 'null'
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1233-exception-handling-in-ruby/refactors/485576" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor472237</id>
    <published>2010-03-08T20:53:03-08:00</published>
    <title>[Ruby] On Ruby Hash Extraction Using Regex on Key</title>
    <content type="html">&lt;p&gt;@mxcl: Seems to be the most succinct and readable solution... except: you can get rid of the braces around the block-arguments. Since #collect on a hash returns two args they will be assigned correcly. And call #map (which is shorter ;-)):
&lt;/p&gt;

&lt;pre&gt;attributes.map { |key, value| &amp;quot;#{$1}.#{value}&amp;quot; if key =~ /^svc_(\w{2})_id$/ }.compact&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1187-ruby-hash-extraction-using-regex-on-key/refactors/472237" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor469452</id>
    <published>2010-03-05T20:51:58-08:00</published>
    <title>[Ruby] On Creating a binary file with Bindata gem </title>
    <content type="html">&lt;p&gt;I don't know much about binary-files. Hope, my solution works and is not too cryptic.
&lt;br /&gt;I put each Type and the corresponding data in an Array. So I can call the #new-method and hand in the value at the end.
&lt;br /&gt;Hope this gives rise to some more succinct and more readable solutions... I don't know BinData at all.&lt;/p&gt;

&lt;p&gt;BTW: You don't need to assign and reassign the obj-variable each time.&lt;/p&gt;

&lt;pre&gt;File.open 'binary.bin', 'wb' do |file|
  b = BinData
  head = b::String, 'Binary file start-point', b::Uint32be, 0, b::Bit16,0, b::Uint24be, @fragments.size
  data = @fragments.collect { |f| [b::Uint8be, f.encoding, b::Uint8be, f.atype, b::String, f.content] }.flatten
  (head + data).each_slice(2) {|type, value| type.new(:initial_value =&amp;gt; value).write file }
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1196-creating-a-binary-file-with-bindata-gem/refactors/469452" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor469353</id>
    <published>2010-03-05T14:14:48-08:00</published>
    <title>[Ruby] On Nested Hash from delimiter-separated String</title>
    <content type="html">&lt;p&gt;@mxcl, @Devin: I fiddled with #inject, too, but I got it wrong. Great solutions!
&lt;br /&gt;@mxcl: You don't have to assign to rv in the inject-block =&amp;gt; even shorter:  string.split('.').reverse.inject(value) { |rv, e| { e =&amp;gt; rv } }&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1204-nested-hash-from-delimiter-separated-string/refactors/469353" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor468955</id>
    <published>2010-03-04T22:30:40-08:00</published>
    <title>[Ruby] On Nested Hash from delimiter-separated String</title>
    <content type="html">&lt;p&gt;Use recursion on a recursive problem...&lt;/p&gt;

&lt;pre&gt;def hash_from_string string, value
  if key = string[/^([^.]+)/]
    {key =&amp;gt; hash_from_string(string.gsub(/^[^.]+\.?/, ''), value)}
  else 
    value
  end
end

#Or shorter (but more cryptic) with the ?-operator

def hash_from_string string, value
  (key = string[/^([^.]+)/]) ? {key =&amp;gt; hash_from_string(string.gsub(/^[^.]+\.?/, ''), value)} : value
end

p hash_from_string 'some.dot.separated.list', 'value'&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1204-nested-hash-from-delimiter-separated-string/refactors/468955" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor468929</id>
    <published>2010-03-04T21:36:50-08:00</published>
    <title>[Ruby] On Function to remove consistent indentation from multiline string</title>
    <content type="html">&lt;p&gt;You could also use ONLY a RegEx (no need to define an 'indent' variable like Serguei did).
&lt;br /&gt;steenslag's and Serguei's solution do not replace the three dots '...' or did I get it wrong? Otherwise Serguei's solution is pretty fine, except that you could get rid of the temp-variable (just remove the 'length'-method-call in my example)&lt;/p&gt;

&lt;pre&gt;class String
  def squish
    self.gsub(/^.{#{self[/^\s+/].length}}/, '')
  end
end

p &amp;lt;&amp;lt;-EOS.squish
   hi
...my friend over
   there
   EOS
   
# =&amp;gt; &amp;quot;hi\nmy friend over\nthere\n&amp;quot;&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1193-function-to-remove-consistent-indentation-from-multiline-string/refactors/468929" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor462745</id>
    <published>2010-02-28T00:17:40-08:00</published>
    <title>[Ruby] On Generate Days of a Year</title>
    <content type="html">&lt;p&gt;I'm&#194;&#160;using&#194;&#160;#inject&#194;&#160;to&#194;&#160;build&#194;&#160;an&#194;&#160;Array.&#194;&#160;That&#194;&#160;way&#194;&#160;you&#194;&#160;could&#194;&#160;use&#194;&#160;#compact&#194;&#160;to&#194;&#160;remove&#194;&#160;the&#194;&#160;empty&#194;&#160;lines&#194;&#160;(that's why I use nil instead of '') before&#194;&#160;calling&#194;&#160;#puts.
&lt;br /&gt;BTW:&#194;&#160;There's&#194;&#160;a&#194;&#160;leading&#194;&#160;empty&#194;&#160;line&#194;&#160;before&#194;&#160;the&#194;&#160;month-name&#194;&#160;on&#194;&#160;the&#194;&#160;first&#194;&#160;day&#194;&#160;of&#194;&#160;each&#194;&#160;month&#194;&#160;(should&#194;&#160;be&#194;&#160;removed&#194;&#160;maybe,&#194;&#160;but&#194;&#160;it&#194;&#160;was&#194;&#160;also&#194;&#160;there&#194;&#160;in&#194;&#160;your&#194;&#160;initial&#194;&#160;code...&#194;&#160;refactorings&#194;&#160;don't&#194;&#160;change&#194;&#160;behaviour&#194;&#160;;-))&lt;/p&gt;

&lt;pre&gt;require 'date'
puts((Date.today..Date.today&amp;gt;&amp;gt;12).inject([]) do |m, d|
  d.day == 1 ? m.push(nil, d.strftime('%b'), nil) : (m &amp;lt;&amp;lt; nil if d.wday == 1)
  m &amp;lt;&amp;lt; d.strftime('%a %m.%d')
end)&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1194-generate-days-of-a-year/refactors/462745" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor417800</id>
    <published>2010-01-18T21:13:06-08:00</published>
    <title>[Ruby] On Fizz Buzz</title>
    <content type="html">&lt;p&gt;You could also use 15 as the Least common multiple of 3 and 5:&lt;/p&gt;

&lt;pre&gt;puts (1..100).map { |n| [:fizzbuzz][n%15] || [:fizz][n%3] || [:buzz][n%5] || n }&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1145-fizz-buzz/refactors/417800" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor417792</id>
    <published>2010-01-18T20:40:56-08:00</published>
    <title>[Ruby] On Fizz Buzz</title>
    <content type="html">

&lt;pre&gt;puts (1..100).map { |n| ['fizzbuzz'][n%3+n%5] || ['fizz'][n%3] || ['buzz'][n%5] || n }&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1145-fizz-buzz/refactors/417792" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor414758</id>
    <published>2010-01-16T12:35:18-08:00</published>
    <title>[Ruby] On Fizz Buzz</title>
    <content type="html">

&lt;pre&gt;def fizzbuzz max
  (1..max).map do |n|
    s = ['fizz'][n%3].to_s + ['buzz'][n%5].to_s
    s.empty? ? n.to_s : s
  end
end

puts fizzbuzz(100)&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1145-fizz-buzz/refactors/414758" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor324800</id>
    <published>2009-09-30T09:56:14-07:00</published>
    <title>[Ruby] On Incrementing an attribute</title>
    <content type="html">&lt;p&gt;To simply increment an attribute you could use #increment or #increment! (uses the setter).
&lt;br /&gt;The submitted code was not so ugly to me, but you could shorten it a bit by using the if-modifier.
&lt;br /&gt;You should access the AdvertisementHashes through the scope of the advertisement_hashes-association&lt;/p&gt;

&lt;pre&gt;def increment_clicks!(hash_string)
  self.increment :clicks if (hash = self.advertisement_hashes.find_by_hash_string(hash_string)) &amp;amp;&amp;amp; hash.destroy           # If you want to destroy the object
  # self.increment :clicks if self.advertisement_hashes.delete self.advertisement_hashes.find_by_hash_string(hash_string) # Otherwise
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1054-incrementing-an-attribute/refactors/324800" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor307280</id>
    <published>2009-09-14T12:15:26-07:00</published>
    <title>[Ruby] On Determine layout</title>
    <content type="html">&lt;p&gt;Not that short, but IMHO more readable&lt;/p&gt;

&lt;pre&gt;def determine_layout
  case params[:action]
    when 'index', 'show', 'edit', 'update' then 'application'
    else                                        'sessions'
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1032-determine-layout/refactors/307280" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor304958</id>
    <published>2009-09-11T09:16:11-07:00</published>
    <title>[Ruby] On ungliness and long too</title>
    <content type="html">&lt;p&gt;I just updated my solution and changed the order of the Regexes so that it first checks for /female/ and if it doesn't match checks for /male/. @Muke: Thanks for the hint!&lt;/p&gt;

&lt;pre&gt;#...
case name
           when /female/ then 'Female Dorm'
           when /male/   then 'Male Dorm'
#...&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1030-ungliness-and-long-too/refactors/304958" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor303948</id>
    <published>2009-09-10T14:31:18-07:00</published>
    <title>[Ruby] On ungliness and long too</title>
    <content type="html">&lt;p&gt;Another way might be a mapping-table or a hash which maps the name-parts to the matching results (size, type) and you iterate over the table/hash-keys and check if they match and then take the results.
&lt;br /&gt;But sometimes this might be to cryptic for so few cases:&lt;/p&gt;

&lt;pre&gt;mapping = {'double' =&amp;gt; ['Double Bed', 2], 'twin' =&amp;gt; ['Twin Bed', 2], 'single' =&amp;gt; ['Single Bed', 1], 'female' =&amp;gt; 'Female Dorm', ... }

type, size = mapping.find { |k, v| name.include? k }
size ||= name[/[0-9]+/] || 1
priv, ensuite = name =~ /private/ ? 1 : 0, name =~ /ensuite/ ? 1 : 0

name = name.gsub(/(dorm|male|female|double|single|beds|bed|private|ensuite|twin|[\d\(\)*])/,'').squeeze(' ')

puts &amp;quot;Name: #{name}, Size: #{size}, #{type}, #{priv}, #{ensuite}&amp;quot;&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1030-ungliness-and-long-too/refactors/303948" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor303936</id>
    <published>2009-09-10T14:05:49-07:00</published>
    <title>[Ruby] On ungliness and long too</title>
    <content type="html">&lt;p&gt;What do you exactly want with that: [\d\(\)*???
&lt;br /&gt;name = name.gsub(/(dorm|male|female|double|single|beds|bed|private|ensuite|twin|[\d\(\)*])/,'').squeeze(' ')
&lt;br /&gt;Inside the brackets the letters have other meanings... Could you give an example so that this line could be refactored?&lt;/p&gt;

&lt;pre&gt;stuff.each do |name|
  name = name.downcase
  
  #size
  size = case name
           when /double|twin/ then 2
           when /single/      then 1
           else name[/[0-9]+/] || 1 # Set size as a last resort to 1 instead of nil
         end

  #type
  type = case name
           when /female/ then 'Female Dorm'
           when /male/   then 'Male Dorm'
           when /double/ then 'Double Bed'
           when /twin/   then 'Twin Bed'
           when /single/ then 'Single Bed'
           else               'Mixed Dorm'
         end

  #You don't need to return Strings here like you did: '1' =&amp;gt; #{x} calls x.to_s on it  
  priv, ensuite = name =~ /private/ ? 1 : 0, name =~ /ensuite/ ? 1 : 0
  #If boolean results (true/false) are okay: priv, ensuite = %w(private ensuite).map { |x| name.include? x }
  
  #What do you exactly want with that: [\d\(\)*???
  name = name.gsub(/(dorm|male|female|double|single|beds|bed|private|ensuite|twin|[\d\(\)*])/,'').squeeze(' ')
  
  puts &amp;quot;Name: #{name}, Size: #{size}, #{type}, #{priv}, #{ensuite}&amp;quot;
end&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1030-ungliness-and-long-too/refactors/303936" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor290261</id>
    <published>2009-09-03T12:27:40-07:00</published>
    <title>[Ruby] On Remove element from array</title>
    <content type="html">

&lt;pre&gt;tmp = reply.case.users.map(&amp;amp;:email)
tmp.delete(reply.created_by.email)
tmp.join(',')           #ATTENTION DEAD CODE!!! DON'T NEED THAT tmp is NOT changed by this and the result is not used by you. result = tmp.join(',')...

@recipients  = &amp;quot;#{tmp}&amp;quot; # #{tmp} does a tmp.to_s. You could do @recipients = tmp.join(',') otherwise

#======&amp;gt;Refactoring 1:

mails = reply.case.users.map(&amp;amp;:email)
@recipients = mails.reject { |e| e == reply.created_by.email }.join(',')

#======&amp;gt;Refactoring 2:
#If reply.created_by is included in reply.case.users:

@recipients = reply.case.users.reject { |u| u == reply.created_by }.map(&amp;amp;:email).join(',')
#OR
@recipients = (reply.case.users - [reply.created_by]).map(&amp;amp;:email).join(',')&lt;/pre&gt;</content>
    <author>
      <name>Martin Pl&#195;&#182;ger</name>
      <email>martin.ploeger@web.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1023-remove-element-from-array/refactors/290261" rel="alternate"/>
  </entry>
</feed>

