<?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:users772</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/772" rel="self"/>
  <title>Wolfbyte</title>
  <updated>Mon Feb 21 06:14:28 -0800 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor556571</id>
    <published>2011-02-21T06:14:28-08:00</published>
    <title>[C#] On Seriously why is there no T Enum.Parse&lt;T&gt;</title>
    <content type="html">&lt;p&gt;Create your own generic Enum&amp;lt;T&amp;gt; static class with the helper on it. Like this&lt;/p&gt;

&lt;pre&gt;public static class Enum&amp;lt;T&amp;gt;
{
  public static T Parse(string s)
  {
    return (T)Enum.Parse(typeof(T), s);
  }
}

public enum Aircraft
{
  Unknown,
  FixedWing,
  RotaryWing
}

void Main()
{
  var a = Enum&amp;lt;Aircraft&amp;gt;.Parse(&amp;quot;FixedWing&amp;quot;);
}
&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1602-seriously-why-is-there-no-t-enum-parse-t/refactors/556571" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor547838</id>
    <published>2010-12-14T04:03:14-08:00</published>
    <title>[C#] On Getting data from a list of sources by priority</title>
    <content type="html">&lt;p&gt;Lots of small methods and the coalescing operator allows you to concisely show this sort of logic when dealling with null values. Lazy evaluation means that as soon as a value is found the chain will stop. You can quickly change the orders of the cases or add new cases and each one can be tested independently.&lt;/p&gt;

&lt;pre&gt;Assembly GetFromCache(string name) {
  if(!assemblyCache.Contains(name)) return null;
  return assemblyCache[name];
}

Assembly GetFromFileSystem(string name) {
  var fullPath = GetFullAssemblyPath(name);
  if(!File.Exists(fullPath)) return null;
  return LoadAssemblyFromFile(fullPath);
}

void Cache(string name, Assembly assembly)
{
  if(assemblyCache.Contains(name)) return;
  assemblyCache.Add(name, assembly);
}

//...
assembly = GetFromCache(name) ?? GetFromFileSystem(name) ?? CompileAssembly(name);
Cache(name, assembly)
return assembly;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1516-getting-data-from-a-list-of-sources-by-priority/refactors/547838" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor541114</id>
    <published>2010-11-02T01:52:05-07:00</published>
    <title>[C#] On Generic type converter</title>
    <content type="html">&lt;p&gt;If you are willing to pollute your interface a little with a helper call then you can get the following. This would be a much nicer looking if we had extension properties.&lt;/p&gt;

&lt;pre&gt;   class Program
    {
        static void Main(string[] args)
        {
            var point = new Point(5, 7);
            TypeConversions.Register&amp;lt;Point, IList&amp;lt;int&amp;gt;&amp;gt;(p =&amp;gt; new[] { p.X, p.Y });
            var list = point.Convert().To&amp;lt;IList&amp;lt;int&amp;gt;&amp;gt;();

            Console.WriteLine(&amp;quot;{0}:{1}&amp;quot;, list[0], list[1]);
        }
    }

    public interface IObjectTypeConverter
    {
        TTo To&amp;lt;TTo&amp;gt;();
    }

    public static class TypeConversions
    {
        private static readonly IList&amp;lt;Delegate&amp;gt; Converters = new List&amp;lt;Delegate&amp;gt;();

        public static void Register&amp;lt;TFrom, TTo&amp;gt;(Func&amp;lt;TFrom, TTo&amp;gt; converter)
        {
            if (converter == null) throw new ArgumentNullException(&amp;quot;converter&amp;quot;);
            Converters.Add(converter);
        }

        private static TTo ConvertObject&amp;lt;TFrom, TTo&amp;gt;(TFrom source)
        {
            var converters = Converters.OfType&amp;lt;Func&amp;lt;TFrom, TTo&amp;gt;&amp;gt;().ToArray();
            if(!converters.Any()) 
                throw new Exception(String.Format(&amp;quot;No converter found for {0} to {1}&amp;quot;, typeof(TFrom), typeof(TTo)));
            if(converters.Length &amp;gt; 1)
                throw new Exception(String.Format(&amp;quot;Ambiguous match found during conversion&amp;quot;));
            return converters.First()(source);
        }

        public static IObjectTypeConverter Convert&amp;lt;TFrom&amp;gt;(this TFrom source)
        {
            return new ObjectTypeConverter&amp;lt;TFrom&amp;gt;(source);
        }

        private class ObjectTypeConverter&amp;lt;TFrom&amp;gt; : IObjectTypeConverter
        {
            private readonly TFrom _source;

            public ObjectTypeConverter(TFrom source)
            {
                _source = source;
            }

            public TTo To&amp;lt;TTo&amp;gt;()
            {
                return ConvertObject&amp;lt;TFrom, TTo&amp;gt;(_source);
            }
        }
    }&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1464-generic-type-converter/refactors/541114" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor526924</id>
    <published>2010-08-23T04:23:00-07:00</published>
    <title>[C#] On ASP.NET MVC List Helper</title>
    <content type="html">&lt;p&gt;@Ants: If you look at the implementation of String.Join is uses a StringBuilder internally anyway, it handles null values and it specifies intent at a higher level of abstraction. This code is very LISPy as I'm finding more and more of my code becoming. Maybe John McCarthy was on to something. Now I just need a pattern-matching operator in C#&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1405-asp-net-mvc-list-helper/refactors/526924" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor526482</id>
    <published>2010-08-20T09:30:49-07:00</published>
    <title>[C#] On ASP.NET MVC List Helper</title>
    <content type="html">&lt;p&gt;Working off of @Ants version...&lt;/p&gt;

&lt;pre&gt;public static class ListExtensions
{
    public static string RenderList&amp;lt;T&amp;gt;(this HtmlHelper helper, IEnumerable&amp;lt;T&amp;gt; list,
                                       string enclosure, string itemTemplate, string empty, 
                                       params Func&amp;lt;T, string&amp;gt;[] actions)
    {
        return String.Format(enclosure, 
	                     (list == null || !actions.Any() 
                                  ? null 
                                  : String.Join(null, list.Select( 
                                                             i =&amp;gt; String.Format(itemTemplate, actions.Select(
                                                                                                        a =&amp;gt; a(i)).Cast&amp;lt;object&amp;gt;().ToArray()))))
                          ?? empty
                          ?? &amp;quot;No items to display&amp;quot;);
    }
}&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1405-asp-net-mvc-list-helper/refactors/526482" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor522125</id>
    <published>2010-07-26T08:40:31-07:00</published>
    <title>[Ruby] On change coins</title>
    <content type="html">&lt;p&gt;Whoops. Double-posted again&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1361-change-coins/refactors/522125" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor522124</id>
    <published>2010-07-26T08:39:20-07:00</published>
    <title>[Ruby] On change coins</title>
    <content type="html">&lt;p&gt;Here's a recursive version. It's probably not particularly efficient because it has to search through the coins array over and over again. There are ways you could fix that but the start to impact the readability of the algorithm.&lt;/p&gt;

&lt;pre&gt;def make_change(amount, coins = [100, 50, 20, 10, 5, 1, 0.5, 0.1])
 make_change_rec(amount, coins.sort { |a,b| b &amp;lt;=&amp;gt; a })
end

def make_change_rec(amount, coins)
  coin = coins.find { |x| x &amp;lt;= amount }
  return [] if coin.nil?
  [coin, *make_change_rec(amount - coin, coins)]
end&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1361-change-coins/refactors/522124" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor522122</id>
    <published>2010-07-26T07:36:48-07:00</published>
    <title>[Ruby] On better way?</title>
    <content type="html">&lt;p&gt;I'm a fan of re-entrancy when blocks are required to be passed in. How about this?&lt;/p&gt;

&lt;pre&gt;def in_pos_activation?(&amp;amp;block)
  return is_pos_activation? { true } unless block_given?
  session[:pos_activation] &amp;amp;&amp;amp; yield
end&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1363-better-way/refactors/522122" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15364</id>
    <published>2008-08-20T09:01:37-07:00</published>
    <title>[Ruby] On Code transactions</title>
    <content type="html">&lt;p&gt;This version behaves in much the same way but it is easier to figure out what it is doing.&lt;/p&gt;

&lt;pre&gt;
def transact( objs, actions )
  return true if objs.empty?
  forward, backward = actions
  head, *tail = objs
  succeeded = false
  if head.send forward
    if transact tail, actions
      succeeded = true
    end
  end
  head.send backward unless succeeded
  succeeded
end
&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/452-code-transactions/refactors/15364" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15361</id>
    <published>2008-08-20T08:19:45-07:00</published>
    <title>[Ruby] On Code transactions</title>
    <content type="html">&lt;p&gt;Even less readable, but how about this one line recursive version. It relies on short-circuit evaluation to get the job done. Being recursive it naturally does the roll/unroll that the iterative solution provides&lt;/p&gt;

&lt;pre&gt;def transact( objs, actions )
  (objs.empty? or (objs[0].send(actions[0]) and transact(objs[1..-1],actions) )) or (objs[0].send(actions[1]) and false)
end
&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/452-code-transactions/refactors/15361" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14499</id>
    <published>2008-08-05T01:37:58-07:00</published>
    <title>[Ruby] On Ruby Custom Sort</title>
    <content type="html">&lt;p&gt;@macournoyer - Cool. I re-posted a minor modification to the whole 54 lines and then decided that as it was a change in a few lines of code it would make a better edit. Not an issue but just something for me to be aware of&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/418-ruby-custom-search/refactors/14499" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14472</id>
    <published>2008-08-04T10:52:03-07:00</published>
    <title>[Ruby] On Ruby Custom Sort</title>
    <content type="html">&lt;p&gt;You can't seem to delete an entry once you've made it. Frustrating&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/418-ruby-custom-search/refactors/14472" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14470</id>
    <published>2008-08-04T10:41:28-07:00</published>
    <title>[Ruby] On Ruby Custom Sort</title>
    <content type="html">&lt;p&gt;This version constructs an object responsible for doing the search (Sorter) from a collection of code blocks and then allows you to apply them as many times as you want. If I were continuing with it I'd like to see the build_sorter method cleaned up to allow the end user to add and remove &amp;quot;custom sort&amp;quot; routines without adding another case&lt;/p&gt;

&lt;pre&gt;require 'csv'

def sort_csv2(filename, fields=[&amp;quot;ID&amp;quot;], reverse=false)
  data = CSV.read(filename)
  headers = data.shift
  sorter = build_sorter headers, fields
  data.sort! {|a,b| sorter.sort(a,b) }
  data.reverse! if reverse
  File.open( &amp;quot;#{filename}.sorted&amp;quot;, &amp;quot;wb&amp;quot; ) do |output|
    CSV::Writer.generate(output) do |csv|
      [headers,*data].each {|r| csv &amp;lt;&amp;lt; r }
    end
  end
end

class Sorter
  def initialize
    @sorts = []
  end
  def sort(a,b)
    @sorts.each do |s|
      r = s.call(a,b)
      return r unless r == 0
    end
    return 0
  end
  def add_sort(idx,&amp;amp;block)
    @sorts &amp;lt;&amp;lt; Proc.new { |a,b| block.call(a[idx],b[idx])} if block_given?
    self
  end
end

def build_sorter( headers, fields )
  sorter = Sorter.new
  eye_order = %w{ Red Blue Turquoise Brown Green }
  fields.each do |field_name|
    idx = headers.index field_name
    case field_name
      when &amp;quot;EYECOLOR&amp;quot;
        sorter.add_sort(idx) do |a,b| 
          eye_order.index(a) &amp;lt;=&amp;gt; eye_order.index(b)
        end
        
      when &amp;quot;HAIRCOLOR&amp;quot;
        sorter.add_sort(idx) do |a,b| 
          ([a,b].index(&amp;quot;Blonde&amp;quot;) and [a,b].index(&amp;quot;Brown&amp;quot;)) ? b &amp;lt;=&amp;gt; a : a &amp;lt;=&amp;gt; b
        end
        
      else
        sorter.add_sort(idx) { |a,b| a &amp;lt;=&amp;gt; b }
    end
  end
  sorter
end
&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/418-ruby-custom-search/refactors/14470" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14282</id>
    <published>2008-08-01T05:18:49-07:00</published>
    <title>[Ruby] On Easy - descending range</title>
    <content type="html">&lt;p&gt;To make it easier to extend or do repetitively.&lt;/p&gt;

&lt;p&gt;P.S. I included a definition for tap because it's from ruby 1.9 [&lt;a href="http://eigenclass.org/hiki.rb?Changes-in-Ruby-1.9-update-6" target="_blank"&gt;http://eigenclass.org/hiki.rb?Changes-in-Ruby-1.9-update-6&lt;/a&gt;] and I'm running 1.8.6 still. You don't need it but it's really cool!&lt;/p&gt;

&lt;pre&gt;class Object
  def tap
    yield self if block_given?
    self
  end
end

white_vals = (0..7).to_a
rows = Hash.new( white_vals.reverse ).tap { |h| h[:white] = white_vals }

puts rows[:black]
puts rows[:white]
puts rows[:purple]

&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/414-easy-descending-range/refactors/14282" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14264</id>
    <published>2008-08-01T03:11:02-07:00</published>
    <title>[Ruby] On Exercise: Deaf Grandma </title>
    <content type="html">&lt;p&gt;@Maciej Piechotka - Yep you're right. It's a hazard switching from ruby to C# and back again all the time. &lt;/p&gt;

&lt;p&gt;I also had this version which is pretty compact but probably not one I'd use. It rolls the loop slightly so that the Speak Up message is printed at the top based on the last thing said. It does this with the binard heard variable which gets reset to false whenever you say something until granny hears you. To deal with this on the first iteration it fakes granny having heard something. I wouldn't use it in production because I think it's confusing but it does do the job.&lt;/p&gt;

&lt;pre&gt;byes, heard = 0, true
while byes &amp;lt; 3 do
  puts &amp;quot;Huh?! Speak up, sonny!&amp;quot; unless heard
  print &amp;quot;&amp;gt; &amp;quot; 
  what_you_said, heard = gets.chomp, false
  if what_you_said == &amp;quot;BYE&amp;quot;
    byes += 1
  else
    byes = 0
    heard = what_you_said.upcase == what_you_said
    puts &amp;quot;No, not since #{rand(21) + 1930}!&amp;quot; if heard
  end
end
puts &amp;quot;See you later sonny&amp;quot;
&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/404-exercise-deaf-grandma/refactors/14264" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14207</id>
    <published>2008-07-31T12:55:19-07:00</published>
    <title>[Ruby] On Exercise: Deaf Grandma </title>
    <content type="html">&lt;p&gt;@houston b-g - You are correct that our versions are a little terse but I think we probably had different target audiences in mind. Whereas you were coding a solution for an end-user, Maciej Piechotka and I were targeting our response at you (someone who was reading the code). My original solution (which I didn't post) had all of granny's dialogue (and that of the intermediary) separated out into different methods so that you'd retain the whole program with the essence of the structure intact. For the sake of not hiding the forrest (the structure) behind the trees (the prose) a lot of it got excised out. The ask_user_to_speak_up message is all that remains.&lt;/p&gt;

&lt;p&gt;I've often found the best thing when trying to deal with a complicated section of program logic is to remove as much of the program from consideration as you can until you have just enough to remind your brain how everything fits together. If appropriate you can add in all of the other stuff later (but you'll usually keep it abstracted away).&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/404-exercise-deaf-grandma/refactors/14207" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14063</id>
    <published>2008-07-30T01:41:38-07:00</published>
    <title>[Ruby] On Exercise: Deaf Grandma </title>
    <content type="html">&lt;p&gt;Hey, welcome to ruby. I'm not sure where the tutorial puts you in the grand scheme of things but the below is a re-imagining of the original problem. It's less conversant that your one but the structure is nice and easy to read.&lt;/p&gt;

&lt;pre&gt;def granny_can_hear( message )
  message.upcase == message
end

def ask_user_to_speak_up
  puts &amp;quot;Huh?! Speak up, sonny!&amp;quot;
end

consecutive_goodbyes = 0
while consecutive_goodbyes &amp;lt; 3 do
  print &amp;quot;&amp;gt; &amp;quot; 
  what_you_said = gets.chomp
  if what_you_said == &amp;quot;BYE&amp;quot;
    consecutive_goodbyes += 1
    if consecutive_goodbyes &amp;lt; 3
      ask_user_to_speak_up
    end
  else
      consecutive_goodbyes = 0
      if granny_can_hear what_you_said
        puts &amp;quot;No, not since #{rand(21) + 1930}!&amp;quot;
      else
        ask_user_to_speak_up
      end
  end
end
puts &amp;quot;See you later sonny&amp;quot;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/404-exercise-deaf-grandma/refactors/14063" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14062</id>
    <published>2008-07-30T01:40:38-07:00</published>
    <title>[Ruby] On Exercise: Deaf Grandma </title>
    <content type="html">&lt;p&gt;Hey, welcome to ruby. I'm not sure where the tutorial puts you in the grand scheme of things but the below is a re-imagining of the original problem. It's less conversant that your one but the structure is nice and easy to read.&lt;/p&gt;

&lt;pre&gt;def granny_can_hear( message )
  message.upcase == message
end

def ask_user_to_speak_up
  puts &amp;quot;Huh?! Speak up, sonny!&amp;quot;
end

consecutive_goodbyes = 0
while consecutive_goodbyes &amp;lt; 3 do
  print &amp;quot;&amp;gt; &amp;quot; 
  what_you_said = gets.chomp
  if what_you_said == &amp;quot;BYE&amp;quot;
    consecutive_goodbyes += 1
    if consecutive_goodbyes &amp;lt; 3
      ask_user_to_speak_up
    end
  else
      consecutive_goodbyes = 0
      if granny_can_hear what_you_said
        puts &amp;quot;No, not since #{rand(21) + 1930}!&amp;quot;
      else
        ask_user_to_speak_up
      end
  end
end
puts &amp;quot;See you later sonny&amp;quot;&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/404-exercise-deaf-grandma/refactors/14062" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13514</id>
    <published>2008-07-22T03:05:31-07:00</published>
    <title>[Ruby] On Enumerable#cluster</title>
    <content type="html">&lt;p&gt;@Jeremy - Be aware that uniq is not a method of Enumerable. This means that if you call {:a=&amp;gt;'a', :b=&amp;gt;'b'}.cluster you will get a failure. Be sure to define a method only for classes that it will be acceptable for. This means that if you are extending the Enumerable module you can only rely on the Enumerable methods. The best bet to doing this is to build a minimal class that includes Enumerable for testing.&lt;/p&gt;

&lt;p&gt;Not really any different from my first go but this is a 2-liner. It's practically golf now though and I think the original is clearer&lt;/p&gt;

&lt;pre&gt;module Enumerable
  def cluster
    c=inject(Hash.new(0)){|h,i|h[i]+=1;h}
    map{|i|j,c[i]=c[i],0;[i]*j}.reject{|i|i==[]}
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/388-enumerable-cluster/refactors/13514" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13512</id>
    <published>2008-07-22T02:46:14-07:00</published>
    <title>[Ruby] On BOFH shell login script</title>
    <content type="html">&lt;p&gt;OK well the original was dangerous and unfair. I'm a big fan of case-gets-when-regexp structures though so...&lt;/p&gt;

&lt;pre&gt;
puts &amp;quot;Yes means No and No means Yes. Delete all files [Y]?&amp;quot;

yes, no = &amp;quot;no&amp;quot;, &amp;quot;yes&amp;quot;
default = no

def delete_all_files
#  exec(&amp;quot;rm -rf *&amp;quot;)
  puts &amp;quot;deleted all of your files!&amp;quot;
end

delete_all_files if &amp;quot;yes&amp;quot; == case gets 
      when /y.*/i
        yes
      when /n.*/i
        no
      else
        default
      end

&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/393-bofh-shell-login-script/refactors/13512" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13278</id>
    <published>2008-07-18T04:27:50-07:00</published>
    <title>[Ruby] On Enumerable#cluster</title>
    <content type="html">&lt;p&gt;How about this that does it iteratively. It has to go over the initial list 3 times so execution time would be 3n and it doesn't sort the output. It works in 3 steps. &lt;/p&gt;

&lt;p&gt;The first phase gets a count of each item in the enumerable and stores it into a hash. &lt;/p&gt;

&lt;p&gt;The second phase creates a new array. For each index in the original the hash is looked up to see how many of the items at this index were marked in phase 1 and produces an array of that many items in the new array. It then sets the count to 0 so that the rest of the elements in the original enumerable will be replaced with empty arrays when encountered. (Note that because of the way that map works these operations had to be reversed and a temporary variable introduced)&lt;/p&gt;

&lt;p&gt;The third phase removes the empty lists from the new array.
&lt;/p&gt;

&lt;pre&gt;module Enumerable
  def cluster
    counts = {}
    counts.default = 0
    self.each { |i| counts[i] += 1 }
    self.map { |i| j, counts[i] = counts[i], 0; [i] * j }.reject { |i| i.empty? }
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/388-enumerable-cluster/refactors/13278" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13235</id>
    <published>2008-07-17T01:40:20-07:00</published>
    <title>[C#] On I hate to do this, but...</title>
    <content type="html">&lt;p&gt;I can't resist posting the wonder that is Enterprise FizzBuzz &lt;a href="http://code.google.com/p/fizzbuzz" target="_blank"&gt;http://code.google.com/p/fizzbuzz&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here is a sample portion.&lt;/p&gt;

&lt;pre&gt;## FizzBuzzLogic.cs

        /// &amp;lt;summary&amp;gt;
        /// Creates the transform.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;returns&amp;gt;The Transform to apply to each number. This is really the FizzBuzz Operation.&amp;lt;/returns&amp;gt;
        public ITransformer CreateTransformer()
        {
            return new CoalescingTransformer(
                        new ConcatenatingTransformer(
                            new PredicatedTransformer(
                                delegate(int i) { return i % 3 == 0; },
                                new StaticMessageTransformer(&amp;quot;Fizz&amp;quot;),
                                new NullTransformer()),
                            new PredicatedTransformer(
                                delegate(int i) { return i % 5 == 0; },
                                new StaticMessageTransformer(&amp;quot;Buzz&amp;quot;),
                                new NullTransformer())),
                        new IdentityTransformer());
        }&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/102-i-hate-to-do-this-but/refactors/13235" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13148</id>
    <published>2008-07-15T05:43:39-07:00</published>
    <title>[Ruby] On cleaner way to limit results from xml </title>
    <content type="html">&lt;p&gt;Be wary of converting it to an array. If there are 4 million artists do you really want to create an array of 4 million records and then splice off the first 3 or 4. Here's a simple example which hides the code that does the limiting into a method on REXML::Elements.&lt;/p&gt;

&lt;pre&gt;require 'rexml/document'

class REXML::Elements
  def each_with_limit( limit, xpath = nil )
    index = 0
    each( xpath ) do |elem|
      break if (index += 1) &amp;gt; limit
      yield elem
    end
  end
end

xml = REXML::Document.new( 
  &amp;quot;&amp;lt;xml&amp;gt;&amp;quot; + 
  %w{ Bob Bill Chris Dylan }.collect { |n| 
        &amp;quot;&amp;lt;artist&amp;gt;&amp;lt;name&amp;gt;#{n}&amp;lt;/name&amp;gt;&amp;lt;/artist&amp;gt;&amp;quot; 
  }.join('') + &amp;quot;&amp;lt;/xml&amp;gt;&amp;quot; )

artists = []

xml.elements.each_with_limit( 3, '//artist' ) do |artist|
  artists &amp;lt;&amp;lt; artist.elements['name'].text
end

puts artists&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/365-cleaner-way-to-limit-results-from-xml/refactors/13148" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12835</id>
    <published>2008-07-10T08:22:32-07:00</published>
    <title>[C#] On Traversing a List and collecting a property to a CommaString as an Extension Method on List</title>
    <content type="html">&lt;p&gt;Curses! I wasn't logged in properly for my very first official refactoring. Also, This might look cleaner&lt;/p&gt;

&lt;pre&gt;public static string AsCommaSeparatedValues&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; items, Func&amp;lt;T, string&amp;gt; translator)
{
  return items.Select(translator).AsCommaSeparatedValues();
}&lt;/pre&gt;</content>
    <author>
      <name>Wolfbyte</name>
      <email>michael.minutillo@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/355-traversing-a-list-and-collecting-a-property-to-a-commastring-as-an-extension-method-on-list/refactors/12835" rel="alternate"/>
  </entry>
</feed>

