Extend Ruby Enumerables With An EachN Processor

Here’s a useful enhancement to arrays that allows you to process elements in a block n at a time. N is inferred by the arity of your block signature.

require 'enumerator'

module Enumerable
  def eachn(&block)
    n = block.arity
    each_slice(n) {|i| block.call(*i)}
  end
end

a = (1..10).to_a
a.eachn {|x,y,z| p [x,y,z]}

Namaste…

Sleek CLI wrappers around Ruby libs

When using Ruby’s splat operator for method invocation, eg. my_method(*my_array), it takes the array and blows it out into the parameters required to fill the method’s signature. This makes it super easy to call methods from classes via the command line, by simply passing the arguments (ARGV) to a Ruby command line app and then directly down into a Ruby class API. The key to my approach is to do this in a very DRY way with as little code as possible. Here’s how I do it.

First we’ll create a file called seeker.rb with a class a API that looks like this:

class Seeker
  def work_file(infile, outfile, suffix, delimeter)
    p "parameter 1: infile = #{infile}"
    p "parameter 2: outfile = #{outfile}"
    p "parameter 3: suffix = #{suffix}"
    p "parameter 4: delimeter = #{delimeter}"
    #do important stuff, etc..
  end
end

Now, we can create a generic wrapper script to drive it like this by doing the following. Create a file called just seeker. Make sure to chmod +x seeker. Add the following text:

#!/usr/bin/env ruby

require File.expand_path('seeker.rb', __FILE__)
Seeker.new.send(ARGV.shift.to_sym, *ARGV)

With this generic message passing script in place, we can call the API like this:

$./seeker work_file foo.txt bar.txt .com ,

Running this command, we get the following output:

parameter 1: infile = foo.txt
parameter 2: outfile = bar.txt
parameter 3: suffix = .com
parameter 4: delimeter = ,

The ease with which we’re able to pass input from the cli straight into our class API comes from the splat operator’s ability to turn an ordered set of cli args into Ruby method params. Notice that we chewed off ARGV[0] as the name of the method we wanted to run. Using the builtin “send” method, which sends a generic message to an instance of a Ruby class, we can invoke any method by simply specifying it as the first cli arg followed by the parameters to be passed into the method, which are just the subsequent cli args.

With a general purpose wrapper like this we can invoke any method on any target class with no additional code. This is a testament to the sheer goodness of Ruby.

Creating a “Supernatural” Self-Adapting REST Proxy in C#

.NET 4.0’s DynamicObject provides a quick and easy way to hook into and control how a call for a method is dispatched at runtime.  This so called “late dispatch” capability is exactly what we need to easily create dynamic facades over out-of-process APIs, such as those of remote REST services.

In this post, I’ll show you how to take advantage of dynamic dispatch, in order to create an adaptive web agent that gets driven through a normal C# class API.  What I mean by normal, is that familiar dot syntax for calling methods from our objects.  However, in this case the methods we will be calling are not actually there; they are “ghost methods”.   They do not exist on the object whose receiving the call.  This might sound a bit strange to the uninitiated, but fear not, you’re about to get your secret decoder rings.  C#’s DynamicObject gives us the opportunity to delegate to remote service APIs transparently, without the knowledge or concern of the caller. 

To demonstrate this in action, I created a small class called Flickr.  It inherits from DynamicObject and serves as our call dispatcher to Flickr’s REST API, which is available here: http://api.flickr.com/services/rest/.  Here is the API documentation: http://www.flickr.com/services/api/.  You can get API keys here: http://www.flickr.com/services/apps/create/apply.

Each call you see in this TestMethod is actually to a method that does not exist in the definition of the Flickr class.

[TestMethod]
public void Testing_Flickr()
{
    dynamic flickr = new Flickr();
 
    var xml1 = flickr.people_findByUsername(username: "duncandavidson");
 
    var xml2 = flickr.collections_getTree(user_id: "59532755@N00");
 
    var xml3 = flickr.urls_getUserPhotos(user_id: 59532755@N00);
}

Each of these calls succeeds, and returns a response from Flickr’s service.  Note a few things about this code.  Flickr’s API has methods that look like this flickr.people.findByUsername and flickr.urls.getUserPhotos.  Notice the similarity above.  What I’ve done is replaced the dots with underscores, in order to make the functions legal C# method names.  As you’ll see below, we reformat the method name before attempting to call it on the remote Flickr service.  Additionally, note that we are using named parameters above in order to pass in both parameter name and parameter value to our ghost methods.  This allows us to pass any number of key-value paired parameters to any method name we want.  Here is the implementation of the Flickr class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Net;
 
public class Flickr : DynamicObject
{
    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        dynamic request;
 
        request = new Func<object>(() =>
        {
            //format methodname
            var remoteMethodName = binder.Name.Replace('_', '.');
 
            //format parameters
            var queryStringBuilder = new StringBuilder();
            for (var i = 0; i < binder.CallInfo.ArgumentNames.Count; i++)
            {
                queryStringBuilder.Append(binder.CallInfo.ArgumentNames[i] + "=" + args[i] + "&");
            }
 
            //build rest message
            var baseUrl = http://api.flickr.com/services/rest/;
            var message = string.Format(baseUrl + "?method=flickr.{0}&{1}api_key=YOUR_KEY_HERE",
                                        remoteMethodName,
                                        queryStringBuilder.ToString());
 

            //send
            var strResponse = new WebClient().DownloadString(message);
 
            //respond
            return strResponse;
        });
 
        result = (request as Delegate).DynamicInvoke(); //no need to pass args. they were set via closures above
        return true;
    }
}
 

As you can see, its DynamicObject’s TryInvokeMember method that is our seam for redelegating the call out to the web.   You may be asking yourself, why in the world would I want to do this.  The biggest advantage is that your local proxy API will always stay in sync with the Flickr service.  Even if they, extend the remote API I can call those methods without having to write a single line of additional plumbing code.  The reason is the responder to the call is not the actual Flickr class, but rather Flickr itself out on the web.  This seems simple and natural.  As it turns out, this type of runtime metaprogramming is becoming the preferred approach to interacting with cloud services via dynamic languages such as Python, Ruby, and JavaScript.  It enables applications to adapt to changes in one another’s interfaces automatically.   Now that C# has been endowed with dynamic, we can capitalize on the feature to build client-server apps that are less brittle and require little to no maintenance around the edges.  Perhaps best of all, the client api automatically already supports new behaviors immediately when they show up on the server. Thats instant gratification for the extraordinarily low price of free.

Giving Your Data Some Higher Order Muscle With C#

Today a colleague and I were going through some code.  I have recently been trying to impart to him the power and beauty in the functional programing paradigm in C#.  Today, the opportunity to demonstrate it presented itself as I was showing him how to use extension methods to extend Entities and ValueObjects with a suite-to-purpose functional API.  As we began the code, I realized that what I really wanted to show him was the concept with no additional fluff.  First, the extension method part.

Here’s what I came up with:

public static class IntExtensions
{
    public static void Times(this int count, Action<int> action)
    {
        for (var i = 0; i < count; i++)
        {
            action(i);
        }
    }
}

This small extension to the builtin int type, gives us a convenient and expressive functional API, driven directly from Int32 typed variables themselves.  Now, we can use it as follows.

[TestMethod]
public void SampleRepository_Can_Create_New()
{
     10.Times(i =>
     {
          var sample = TestObjects.BuildSample();
          SampleRepository.Save(sample);
     });

     var samples = SampleRepository.GetAll();
     
     samples.Count().Times(i => Debug.WriteLine("your index is " + i));

     Assert.IsTrue(samples.Count() == 10, "Should have 10 samples");
}

Rubyists recognize this API as its built into the language. Its simple stepwise iteration driven directly off numeric types. In C# we can use extension methods to shim this behavior into our scalers. APIs that read like 5.Times(doSomething); read like English.  This makes our code  more comprehensible by everyone, and that ladies and gentlemen is worth its weight in Gold Pressed Latinum

Ruby AOP in 12 lines of Code

First, we shim the Ruby Object class with a profiling aspect, in this case an additional method called profile that will wrap any existing method with timing code that we tell it to.

Lets put this in a file called: aop_extension.rb.

class Object

  def Object.profile symbol

    _symbol = ("rprof_" + symbol.to_s).to_sym

    alias_method _symbol, symbol

    # Define the new wrapper method 

    self.send(:define_method, symbol.to_s) { |*args|

      start_time = Time.now

      self.send(_symbol, *args)

      puts (Time.now - start_time).to_s + " have elapsed"

    }

    puts "The new method " + _symbol.to_s + " has been created for method " + symbol.to_s

  end

end

Now lets define a class that we can use as a test subject.  Create a file called greeter.rb.  Add the following:

 

require "./aop_extension.rb"

 

class Greeter

   def hello

    puts "hello"

  end

  profile :hello

end

Notice that in the constructor code we tell the inherited profile method to go after the “hello” method.  This creates a proxy method that will run the targeted method on behalf of callers.

Now lets see this thing in action.  For this we’ll create a file called main.rb.  Add the following:

require "./greeter.rb"

 

t = Greeter.new

t.hello

Finally, after all this extra heavy lifting we can see the goodness shining through in the output:

$ ruby main.rb
The new method rprof_hello has been created for method hello
hello
0.001 have elapsed

Wow, we didn’t even break a sweat on that one.

Property getter/setter extraction from C# Types

The C# Expression API allows you to scrape property and method definitions from Types and work with them as external references.  See here:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Dynamic;
using System.Runtime.CompilerServices;

public static class Extensions

 {
       public static Func<X, T> GetPropertyFunction<X, T>(this Type source, string name)
          {
             ParameterExpression param = Expression.Parameter(typeof(X), "arg");
             MemberExpression member = Expression.Property(param, name);
             LambdaExpression lambda = Expression.Lambda(typeof(Func<X, T>), member, param);
             Func<X, T> compiled = (Func<X, T>)lambda.Compile();
             return compiled;
         }
}

 

And you can use it like this:


[TestMethod]
public void TestMethod1()
         {
             var testObj = new TestObject
             {
                 ID = 1,
                 Description = "ASDFASDF",
                 Name = "GGGG",
                 UnitPrice = 6
             };

             Type type = typeof(TestObject);
             var getName = type.GetPropertyFunction<TestObject, String>("Name");
             String value = getName(testObj);

             Assert.IsTrue(value == testObj.Name);
       }

My C# ObservableDictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.Specialized;

namespace DynamicSpikes
{
    public class ObservableDictionary<String, Object> : Dictionary<string, object>, INotifyPropertyChanged, INotifyCollectionChanged
    {

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        public event NotifyCollectionChangedEventHandler CollectionChanged = delegate { };

        private void OnCollectionChanged(object sender, NotifyCollectionChangedAction action, object value)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(sender, new NotifyCollectionChangedEventArgs(action, value));
            }
        }

        private void OnPropertyChanged(object sender, string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public new void Add(string key, object value)
        {
            base.Add(key, value);
            OnCollectionChanged(this, NotifyCollectionChangedAction.Add, new KeyValuePair<string, object>(key, value));

            //OnPropertyChanged(this, "Values");
            //OnPropertyChanged(this, "Keys");
            //OnPropertyChanged(this, "Count");
        }

        public new bool Remove(string key)
        {
            var kvp = base[key];
            var result = base.Remove(key);
            if (result)
            {
                OnCollectionChanged(this, NotifyCollectionChangedAction.Remove, kvp);

                //OnPropertyChanged(this, "Values");
                //OnPropertyChanged(this, "Keys");
                //OnPropertyChanged(this, "Count");
            }
            return result;
        }

        public new object this[string key]
        {
            get
            {
                return base[key];
            }
            set
            {
                base[key] = value;
                PropertyChanged(this, new PropertyChangedEventArgs(key));
            }
        }
    }

}

Webshims – HTML5 For The Masses

I’ve been researching the available shimming libaries out there that provide old browsers with modern features.  The Webshims library does this in an efficient and unobtrusive way, always defering to the browser’s built in feature if it exists natively.  With 2 lines of code you tell it where to find its shim scripts and then to discover and patch any missing HTML5 features in your browser.  I’ve provided a complete working sample of how to ensure the HTML5 localStorage API in all browsers.

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js”></script>

<script src=”/scripts/js-webshim/dev/polyfiller.js”></script>

<script>

//path is path of polyfiller.js-code + shims/

$.webshims.loader.basePath += ‘shims/’;

//load and implement all unsupported features

$.webshims.polyfill();

</script>

<script>

$.webshims.ready(‘json-storage’, function () {

//work with JSON and localStorage

localStorage.setItem(“name”, “Hello World!”); //saves to the database, “key”, “value”

document.write(window.localStorage.getItem(“name”)); //Hello World!

localStorage.removeItem(“name”); //deletes the matching item from the database

});

</script>

<title>localStorage() – Sample</title>

</head>

<body>

</body>

</html>

Metaprogramming in Ruby and C#

First read this and understand metaprogramming in Ruby: http://www.devsource.com/c/a/Languages/An-Exercise-in-Metaprogramming-with-Ruby/

It seems that C# dynamic supports a similar scenario, but in a different way. C# dynamic objects are in fact not new Types created at runtime, but rather individual compiler-safe bags of key/value pairs that self expose the keys as properties. Thus C# dynamic objects are only available as defined at the instance level and those instances are of type DynamicObject.

The technique I demonstrate in this post: https://uberpwn.wordpress.com/2009/12/21/property-copying-with-dynamic-objects-in-net-4-0/ could be modified to take its key/value pair input from a csv to achieve the same result.