INCLUDE_DATA

 
»
S
I
D
E
B
A
R
«
aquisto cialis levitra svizzera tadalafil bestellen viagra prix acheter du viagra acquista levitra cialis sur internet citrate de sildenafil compro viagra levitra ricetta kamagra pharmaceuticals cialis vente libre cialis suisse levitra italia vente de cialis viagra suisse kamagra gel viagra quanto costa cialis 20 mg prix cialis 10mg viagra verkauf cialis moins cher viagra prezzo levitra kopen acheter tadalafil acheter kamagra tadalafil generico impuissance sexuelle cialis preco levitra donna viagra 100 mg comprar cialis generico comprar levitra cialis a vendre procurer du cialis generische cialis vardenafil generico vardenafil generika levitra sur internet generische viagra acheter cialis generique acquista viagra achat cialis viagra 100 mg cialis generico kamagra en france viagra ordonnance acheter viagra achat vardenafil pastilla levitra viagra cialis differenze impotenza sessuale venta viagra medicament cialis curare impotenza kamagra te koop achat cialis 20mg levitra pharmacie cialis receta acquisto viagra net cialis en ligne achat de viagra cialis generique acheter acheter du cialis cialis 20 mg vardenafil 10 mg viagra alternativo citrate de sildenafil cialis sin receta viagra kopen acheter cialis en pharmacie kamagra bestellen comprar viagra pela internet viagra prescrizione levitra donne vente cialis venta de sildenafil achete levitra acheter cialis france venta de levitra viagra kosten cialis marche pas comprar vardenafil disfunzione erettile rimedi vardenafil generique viagra recensioni cialis generico prezzo viagra versand cialis europe viagra venta libre impotenza rimedi cialis rezeptfrei acheter kamagra france levitra ohne rezept acquisto viagra in contrassegno prix de cialis cialis prescrizione viagra acquisto online achat viagra pildoras cialis kamagra generique cialis prezzo cialis inde cialis sur le net acheter cialis en espagne levitra ordonnance viagra naturel cialis 10 mg acquistare levitra procurer du viagra acquisto viagra senza ricetta viagra controindicazioni levitra 20 mg compra viagra impuissance erection acheter cialis pharmacie prezzi levitra viagra ohne rezept kamagra apcalis comprar viagra commander du cialis cialis ricetta medica sildenafil 50 mg sildenafil venta viagra italia pilule levitra sildenafil generico viagra prijs
In Memory Database Testing with SQLite and NHibernate
Nov 11th, 2009 by Jason

A few weeks ago, I was faced with the challenge of testing how an application interacted with a database without mangling the data within it. What my company of employment had been doing prior was doing all database testing within a transaction and rolling back at the end. The issue with this approach was that behaviors like cascading wouldn’t occur, thus making the tests less than useful.

SQLite

Enter SQLite. SQLite is a basic, lightweight sql database wrapped into a single library. A benefit of its use is that SQLite can create an in-memory database that cleans up after all connections are closed. Its excellent for testing. Starting with a clean slate each test run can provide for consistent, reproducible results.

Prerequisites

NHibernate comes with support for SQLite from the NHibernate.Driver.SQLite20Driver class. However, you must provide two other files for it to work. First, sqlite3.dll, an unmanaged library of SQLite; include it in your test project and ensure “copy to output directory” is marked. Second, System.Data.SQLite, a managed library that allows ADO.NET to interact with SQLite; include this as a reference in your test project.

A Base Class

The following is a basic example of how to set up a base class for in memory database testing. Any tests that require database interaction should inherit from this class and will have the Session object available. Sessions last as long as each test fixture. Also, typeof(Plan) refers to the type of any of your domain classes.

 	public class InMemoryDatabaseTest
	{
		private static Configuration _configuration;
		private readonly object _baton = new object();
		private readonly ISessionFactory _sessionFactory;
		protected readonly ISession Session;

		public InMemoryDatabaseTest()
		{
			if (_configuration == null)
				lock (_baton)
					if (_configuration == null)
					{
						typeof(VersionDate2).GetField("_sqlType",BindingFlags.Static | BindingFlags.NonPublic).SetValue(null,new SqlType(DbType.DateTime));
						_configuration = new Configuration()
							.SetProperty(Environment.ReleaseConnections,"on_close")
							.SetProperty(Environment.ProxyFactoryFactoryClass,
							             "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")
							.SetProperty(Environment.TransactionStrategy, "NHibernate.Transaction.AdoNetTransactionFactory")
							.SetProperty(Environment.CacheProvider, "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache")
							.SetProperty(Environment.ConnectionDriver, typeof (SQLite20Driver).AssemblyQualifiedName)
							.SetProperty(Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider")
							.SetProperty(Environment.Isolation, "ReadCommitted")
							.SetProperty(Environment.ConnectionString, "Data Source=:memory:;Version=3;New=True;")
							.SetProperty(Environment.ShowSql,"True")
							.SetProperty(Environment.Dialect, typeof (SQLiteDialect).AssemblyQualifiedName)
							.AddAssembly(Assembly.GetAssembly(typeof (Plan)));

			_sessionFactory = _configuration.BuildSessionFactory();
			Session = _sessionFactory.OpenSession();
			var schemaExport = new SchemaExport(_configuration);
			schemaExport.Execute(false, true, false, Session.Connection, null);
		}

		public void Dispose()
		{
			Session.Dispose();
		}
	}

Additional Features

Support for Schemas

SQLite doesn’t provide native support for schemas. We use SQL Server, and this led to a problem when any of our mappings pointed to tables in anything other than the dbo schema. A simple fix to this is to replace all periods with underscores prior to creating your session factory. This gives the desired behavior without requiring any modification to your mapping files.

                    if (_configuration == null)
                    {
                        _configuration = new Configuration()
                        //Configuation...
                        foreach (PersistentClass classMapping in _configuration.ClassMappings)
                        {
                            if (classMapping.Table.Name.Contains("."))
                                classMapping.Table.Name = classMapping.Table.Name.Replace(".", "_");
                        }
                    }

Working with AutoMockContainer

If you’re a user of Moq-Contrib’s AutoMockContainer (it must be patched to work with the latest version of Moq), you can register the Session object into the container to have it provided instead of a mocked session. It’s as simple as adding the following after creating your session object.

            MockContainer.Register(Session);

Testing with NHProf

If you use NHibernate Profiler (if you don’t, you should be), your SQLite database interaction can be monitored using that as well. Add an AssemblyInitialize (or your testing framework’s variant) to your InMemoryDatabaseTest class and all interactions will be profiled.

		[AssemblyInitialize]
		public static void AssemblyInit(TestContext testContext)
		{
			NHibernateProfiler.Initialize();
		}
Moq-Contrib Moq 4.0 Compatability Patch
Oct 13th, 2009 by Jason

As a new but frequent user of the Moq mocking framework for .NET, I was disappointed to find Moq-Contrib to be out of date as I wanted the AutoMockContainer. Performing dependency injection by hand and updating my tests for every code change was a pain. I dug into some source, found a simple fix and it is the patch that follows.

Index: Source/Mvc/HttpContextMock.cs
===================================================================
--- Source/Mvc/HttpContextMock.cs	(revision 23)
+++ Source/Mvc/HttpContextMock.cs	(working copy)
@@ -58,11 +58,11 @@
 			this.HttpServerUtility = new HttpServerUtilityMock();
 			this.HttpSessionState = new HttpSessionStateMock();

-			this.ExpectGet(c => c.Application).Returns(this.HttpApplicationState.Object);
-			this.ExpectGet(c => c.Request).Returns(this.HttpRequest.Object);
-			this.ExpectGet(c => c.Response).Returns(this.HttpResponse.Object);
-			this.ExpectGet(c => c.Server).Returns(this.HttpServerUtility.Object);
-			this.ExpectGet(c => c.Session).Returns(this.HttpSessionState.Object);
+			this.SetupGet(c => c.Application).Returns(this.HttpApplicationState.Object);
+			this.SetupGet(c => c.Request).Returns(this.HttpRequest.Object);
+			this.SetupGet(c => c.Response).Returns(this.HttpResponse.Object);
+			this.SetupGet(c => c.Server).Returns(this.HttpServerUtility.Object);
+			this.SetupGet(c => c.Session).Returns(this.HttpSessionState.Object);
 		}

 		/// <summary>
Index: Source/Mvc/HttpRequestMock.cs
===================================================================
--- Source/Mvc/HttpRequestMock.cs	(revision 23)
+++ Source/Mvc/HttpRequestMock.cs	(working copy)
@@ -56,8 +56,8 @@
 		/// </summary>
 		public HttpRequestMock()
 		{
-			this.ExpectGet(f => f.Form).Returns(form);
-			this.ExpectGet(f => f.Headers).Returns(headers);
+			this.SetupGet(f => f.Form).Returns(form);
+			this.SetupGet(f => f.Headers).Returns(headers);
 		}

 	}
Index: Source/Mvc/HttpResponseMock.cs
===================================================================
--- Source/Mvc/HttpResponseMock.cs	(revision 23)
+++ Source/Mvc/HttpResponseMock.cs	(working copy)
@@ -57,9 +57,9 @@
 			this.OutputStream = new Mock<Stream>();
 			this.Cache = new HttpCachePolicyBaseMock();

-			ExpectGet(m => m.Output).Returns(this.Output.Object);
-			ExpectGet(m => m.OutputStream).Returns(this.OutputStream.Object);
-			ExpectGet(m => m.Cache).Returns(this.Cache.Object);
+			SetupGet(m => m.Output).Returns(this.Output.Object);
+			SetupGet(m => m.OutputStream).Returns(this.OutputStream.Object);
+			SetupGet(m => m.Cache).Returns(this.Cache.Object);
 		}

 		// TODO: mock other properties.

Ensure that the Moq 4.0 dll is in the Lib/Moq folder before building.

Using Yield in C#
May 22nd, 2009 by Jason

The yield Statement

yield is an overlooked, yet powerful feature.

yield basics

yield places values into an IEnumerable object.
To further understand what yield does, here’s an example with trace statements between each yield statement:

//:SimpleYield.cs
using System;
using System.Collections.Generic;

class SimpleYield
{
static IEnumerable GetValues(){
Console.WriteLine("Returning 1");
yield return 1;
Console.WriteLine("Returning 2");
yield return 2;
Console.WriteLine("Returning 3");
yield return 3;
}
static void Main(){
//Iterate over YieldMethod's IEnumerable
foreach(int i in GetValues())
Console.WriteLine(i);
}
}/*Output:
Returning 1
1
Returning 2
2
Returning 3
3
*///:~

GetValues( )’s yield statements return hard-coded integer values. Main( ) iterates these values, printing each. Notice how GetValue( )’s trace statements interlace with Main( )’s.
A yield statement comes in two different forms, yield return and yield break. yield return places the evaluated expression as the current value of the IEnumerable. yield break marks the end of an iterator:

//:YieldBreak.cs
using System;
using System.Collections.Generic;

class YieldBreak{
static IEnumerable GetValues(){
Console.WriteLine("Returning 1");
yield return 1;
Console.WriteLine("Returning 2");
yield return 2;
Console.WriteLine("Breaking");
yield break;                //Break Here
Console.WriteLine("Returning 3");
yield return 3;
}
static void Main(){
foreach(int i in GetValues())
Console.WriteLine(i);
}
}/*Output:
Returning 1
1
Returning 2
2
Breaking
*///:~

yield break prevents remaining code from executing. It reports back to the foreach that there are no values remaining in the IEnumerable. C#’s compiler reports a warning about any unreachable code following yield break.
Exercise 1: Use Visual Studio’s debugger to step through (F11) SimpleYield.cs. Follow the code path as the foreach executes.
Exercise 2: Create a GetValue( ) method that returns a random number of integer values. Use yield return and yield break.

///: Exercise2.cs
using System;
using System.Collections.Generic;

class Exercise2{
public static IEnumerable GetValues(){
Random rand = new Random();
int current = 0;
while (true){
current = rand.Next(20);
if (current == 17)
yield break;
yield return current;
}
}

public static void Main(){
foreach (int i in GetValues()){
Console.WriteLine(i);
}
}
}
///:~

yield Benefits

One strong feature of yield is its ability to defer processing. It delays any calculation until absolutely necessary. Deferred processing makes more responsive programs. yield spreads wait time amongst all iterations by creating values only when necessary:

///: DeferredProcessingTime.cs
using System;
using System.Collections.Generic;
using System.Threading;

class DeferredProcessingTime{
public static IEnumerable CalculateAtOnce(){
int[] intArray = new int[10];
for (int i = 0; i &amp;amp;lt; 10; i++){
Thread.Sleep(1000);
intArray[i] = i;
}
return intArray;
}
public static IEnumerable DeferredCalculate(){
for (int i = 0; i &amp;amp;lt; 10; i++){
Thread.Sleep(1000);
yield return i;
}
}
public static void Main(){
Console.WriteLine("Calculate at once");
foreach (int i in CalculateAtOnce()) //Ten seconds before printing
Console.Write(i);
Console.WriteLine("\nUsing Yield");
foreach (int z in DeferredCalculate())
Console.Write(z); //One second between each write
}
}/*Output:
Calculate at once
0123456789
Using Yield
0123456789
*///:~

The above example emulates an algorithm that takes a long time to process by using Thread.Sleep( ) before adding each value.
Both foreachs over CalculateAtOnce( ) and DefferedCalculate( ) print the same results to the console. However the execution behavior is noticeably different. CalculateAtOnce( )’s foreach waits for 10 seconds and prints 0 through 9. DeferredCalculate( )’s foreach prints each value per second.
Another benefit of yield’s deferred processing is that it does not require a collection stored in memory. yield can iterate over a large set of data without consuming large amounts of memory. Take the following example. You iterate over a set of data with 10 million integer values. Without yield, the entire collection of values must be returned. With yield, each integer value is returned when requested.

///:DeferredProcessingMemory.cs
using System;
using System.Collections.Generic;
using System.Threading;

class DeferredProcessingMemory{
public static IEnumerable CalculateAtOnce(){
int[] intArray = new int[10000000];
for (int i = 0; i &amp;amp;lt; 10000000; i++)
intArray[i] = i;
return intArray;
}
public static IEnumerable DeferredCalculate(){
for (int i = 0; i &amp;amp;lt; 10000000; i++)
yield return i;
}
public static void Main(){
Console.WriteLine("Using Yield");
IEnumerable deferred = DeferredCalculate();
Console.ReadLine(); //Memory usage: ~2MB
Console.WriteLine("Calculating at once");
IEnumerable atOnce = CalculateAtOnce();
Console.ReadLine(); //Memory usage: ~40MB
}
}/*Output:
Using Yield

Calculating at once

*///:~

Start the Windows Task Manager (Ctrl+Shift+Esc) before running this example. Find your process, and notice the amount of memory usage from using DeferredCalculate( ). Press enter and see the drastic increase that CalculateAtOnce( ) makes; intArray’s 10 million in-memory values are the cause.
yield gives potential to handle data sets of any size without concern for memory limitations.
Exercise 3: Use yield to iterate over an infinite data set of random integers.

///:Exercise3.cs
using System;
using System.Collections.Generic;

class Exercise3{
public static Random rand = new Random();
public static IEnumerable GetValues(){
while(true)
yield return rand.Next();
}
public static void Main(){
foreach(int i in GetValues())
Console.WriteLine(i);
}
}
///:~

Under the Hood

Many C# keywords involve the generation of MSIL code, methods, or even classes. yield is no exception.
Red Gate’s .NET Reflector shows the generated C# from using the yield statement. The disassembled assembly of SimpleYield.cs contains an extra class implementing IEnumerable.

public class GeneratedClass : IEnumerable, IEnumerable,
IEnumerator, IEnumerator, IDisposable{
//...CUT...
public bool MoveNext(){
switch (this.state){
case 0:
this.state = -1;
Console.WriteLine("Returning 1");
this.current = 1;
this.state = 1;
return true;

case 1:
this.state = -1;
Console.WriteLine("Returning 2");
this.current = 2;
this.state = 2;
return true;

case 2:
this.state = -1;
Console.WriteLine("Returning 3");
this.current = 3;
this.state = 3;
return true;

case 3:
this.state = -1;
break;
}
return false;
}
//...CUT...
}

The compiler generates a switch statement to handle executing the code written in your original yielding method. Only one case statement is used for each time MoveNext( ) is called. This is how the deferred processing actually occurs.
GetValues( ) is also changed to use the GeneratedClass( ) in place of the original code.

private static IEnumerable GetValues(){
return new GeneratedClass(-2);
}

The -2 in the new GeneratedClass( ) call is used to set the initial state. The state’s value is changed when MoveNext( ) is called. Beginning each case, state is set to -1 to prevent further execution if an exception occurs. Just before a case returns, state is assigned the value of the next case.
Exercise 4: Write an implementation of yield’s generated code and execute a foreach over it.

//:Exercise4.cs
using System;
using System.Collections.Generic;
using System.Collections;

class Exercise4 : IEnumerable,IEnumerable,
IEnumerator,IEnumerator, IDisposable{
string current = "";
int state;
public IEnumerator GetEnumerator(){
return this;
}
IEnumerator IEnumerable.GetEnumerator(){
return GetEnumerator();
}
public string Current{
get { return current; }
}
public void Dispose(){}
object IEnumerator.Current{
get { return Current; }
}
public bool MoveNext(){
switch(state){
case 0:
state = -1;
current = "One";
state = 1;
return true;
case 1:
state = -1;
current = "Two";
state = 2;
return true;
case 2:
state = -3;
current = "Three";
state = 3;
return true;
}
return false;
}
public void Reset(){
throw new NotImplementedException();
}
public static void Main(){
foreach(string s in new Exercise4())
Console.WriteLine(s);
}
}/*Output:
One
Two
Three
*///:~
StatenUtil: C# String-Regex Extenstions and String Scanner
Apr 3rd, 2009 by Jason

Face it, C# is not as regex-friendly as it could be.  To do things like find a single regex match involves several lines or some nasty inline code. Using Extension methods, this can be simplified. In another addition to StatenUtil focused on regex, C# becomes a more coder-friendly language.

StringRegex.cs

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace StatenUtil.RegexExtensions
{
  public static class StringRegex
  {
    public static string RegexReplace(this string source, string regex, string replacement)
    {
      Regex r = new Regex(regex);
      return r.Replace(source, replacement);
    }
    public static string Match(this string source, string regex)
    {
      Regex r = new Regex(regex);
      Match m = r.Match(source);
      return m.Value;
    }
    public static string[] Matches(this string source, string regex)
    {
      Regex r = new Regex(regex);
      MatchCollection mc = r.Matches(source);
      List<string> matches = new List<string>();
      foreach(object o in mc)
        matches.Add(((Match)o).Value);
      return matches.ToArray();
    }
    public static string RegexRemove(this string source, string regex)
    {
      Regex r = new Regex(regex);
      return r.Replace(source, "");
    }
  }
}

This class offers four extension methods to the string class: Replace, Remove, Match first, and Match all. Aside from RegexReplace, the parameters often call for a single regular expression string.

Another addition that C# could use is a string scanner. A string scanner moves through a string one regex at a time matching the beginning of a string. This is useful for parsing languages in the way similar to JSON Parsing in Ruby.

StringScanner.cs

using System;

namespace StatenUtil.RegexExtensions
{
  public class StringScanner
  {
    public string Remaining { get; set; }
    public string Match { get; set; }
    public StringScanner(string input)
    {
      Remaining = input;
    }
    public string Scan(string regex)
    {
      Match = Remaining.Match("^"+regex);
      if (Match != null)
        Remaining = Remaining.Substring(Match.Length);
      return Match;
    }
  }
}

The StringScanner class constructor takes an input string that it will scan. Having only a Scan method, the class has the single use of moving through the input string as regex matches are made. As Scan finds a match, it returns the matching string. Also, the class has two properties: Remaining and Match. Remaining provides what is left in the string, useful for checking if the end of the input string has been met. Match provides the most recent match of the Scan method.

Site is back up and GoDaddy-free
Mar 30th, 2009 by Jason

NoDaddyThis past week has been a terrible experience for both me and my website. It was nearing time for my domain to expire, so I decided that I would transfer away from the evil that is GoDaddy (I moved the hosting several months ago). Little did I know that they would attempt one last strike at me before letting me slip away for good. I initiated the domain transfer on the 23rd, and I received an email making claim that it would take approximately 3 days to complete. I could handle that.

On the 25th, I go to my website only to find that it has been replaced with a GoDaddy landing page. I nearly flipped. I was supposed to be moving away from them, not having them take over. I put it off for a day, hoping that it would be a very short term deal. The next day, I see that it’s still there and I want to get this squared away. So I give GoDaddy a call to wind up listening to hold music for nearly a half hour before I decide that I’m sick of wasting my cellphone minutes.

On Saturday, the 28th the problem still remained and since I have free weekends, I gave them another phone call. 20 minutes into hold I get an answer from some guy. I explained to him about what my situation was and asked him to check out my domain to make sure everything was okay. He checked it out, said it was all good and out of their hands.

Today, I found out that GoDaddy had in fact CANCELLED the transfer because the domain was “locked.” Of course, I went to GD’s site to check this out, and it was marked as “unlocked” just as I had set it before doing the transfer. I contacted my registrar to ask them to try again today, and was able to have the site up within about 2 hours. My domain’s no longer listed through GoDaddy, and I am thrilled to never have to deal with them again.

My recommendation to anyone reading is DO NOT do any sort of business with GoDaddy. Even if they have low prices and scantly clad women in their commercials, it’s just not worth it.

Photography Friday XI: Sunstone Hunting
Mar 20th, 2009 by Jason

Yes, I understand I missed last week. How about a four-pic-feature this week to make up for it. Sound fair?

PoundSunstoneTube of StonesHunting

Hunting for suntones is rough. You can work for 15 minutes on a piece of rock to find nothing. But the search for the big one is worth it.

JSON Parsing in Ruby
Mar 18th, 2009 by Jason

Yesterday, I spent awhile working on a RubyQuiz Challenge involving the parsing of JSON. Although I did create it with minimal viewing of the solution, I cannot take credit for the code or the idea.  I did notice that on the site there was no actual merging of their snippets, so I felt inclined to share.

jsonParser.rb

require "strscan"
class JSONParser
	AST = Struct.new(:value)
	def parse(input)
		@input = StringScanner.new(input)
		parse_value.value
	ensure
		@input.eos? or error("Unexpected data")
	end

	private

	def parse_value
		trim_space
		parse_object or
		parse_array or
		parse_string or
		parse_number or
		parse_keyword or
		error("Invalid Data")
	ensure
		trim_space
	end

	#Parses colon separated object hashes
	def parse_object
		if @input.scan(/\s*\{/)
			obj = Hash.new
			more_parts = false
			while key = parse_string
				@input.scan(/\s*:\s*/) or error("Expected : separator")
				obj[key.value] = parse_value.value
				more_parts = @input.scan(/\s*,\s*/) or break
			end
			error("Missing object pair") if more_parts
			@input.scan(/\s*}/) or error("Unclosed object")
			AST.new(obj)
		else
			false
		end
	end

	def parse_array
		if @input.scan(/\s*\[/) #Arrays start with [
			a = Array.new
			more_items = false
			while current = parse_value
			a << current.value
			more_items = @input.scan(/\s*,\s*/) or break
			end
			error("Missing value") if more_items
			@input.scan(/s*\]/) or error("Unclosed array")
			AST.new(a)
		end
	end

	#Parses string objects
	def parse_string
		if @input.scan(/"/)
			s = String.new
			while current = parse_string_content || parse_string_escape
				s << current.value
			end
			@input.scan(/"/) or error('Unclosed String')
			AST.new(s)
		else
			false
		end
	end

	def parse_string_content
		@input.scan(/[^\\"]+/) and AST.new(@input.matched)
	end

	def parse_string_escape
		if @input.scan(%r{\\["\\/]}) #slashes and quotations
			AST.new(@input.matched[-1])
		elsif @input.scan(/\\[bfnrt]/) #newlines, tabs, etc
			AST.new(eval(%Q{"#{@input.matched}"}))
		elsif @input.scan(/\\u[0-9a-fA-F]{4}/) #Hex integers
			AST.new([Integer("0x#{@input.matched[2..-1]}")].pack("U"))
		else
			false
		end
	end

	def parse_number
		@input.scan(/-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\b/) and
		AST.new(eval(@input.matched))
	end

	def parse_keyword
		@input.scan(/\b(?:true|false|null)\b/) and
		AST.new(eval(@input.matched.sub("null","nil")))
	end

	def trim_space
		@input.scan(/\s+/)
	end

	def error(message)
			raise "#{message}:  #{@input.peek(@input.string.length)}"
	end
end

For an in depth explanation of the individual portions in this code, refer to the RubyQuiz Site . One way that I’ve tested this code is by accessing the Twitter search api. The following snippet will print the 20 most recent #neumont tweets:

require 'jsonParser'
require 'cgi'
require 'open-uri'
url = "http://search.twitter.com/search.json?q=%23neumont"
parser = JSONParser.new
open(url) {|html| @output = html.read}
obj = parser.parse(@output)
obj["results"].each {|e| puts CGI.unescapeHTML(e["text"])}
Photography Friday X: Onward and Upward
Mar 6th, 2009 by Jason

Onward and Upward

Life seems to come at you with a seemingly endless number of footholds and slick spots.  Similar to climbing, you can plan, predict, and prepare, but what matters most is how you exectue.

StatenUtil: Bringing a bit of Ruby looping into C#
Feb 28th, 2009 by Jason

For anyone who’s new to the blog, StatenUtil is a personal C# utility library. Consisting mainly of extension methods, its main objective is to simplify frequently used operations. This edition includes adding some Ruby-like looping methods into C#. I understand that Func and Action objects aren’t a real replacement for blocks. However, it’s nice to have some features I miss when not using Ruby.

Int32Extensions.cs

using System;

namespace StatenUtil
{
    public static class Int32Extensions
    {
        public static void Times(this int source, Action<int> action)
        {
            for (int i = 0; i < source; i++)
            {
                action(i);
            }
        }
        public static void UpTo(this int source, int max, Action<int> action)
        {
            for (int i = source; i <= max; i++)
            {
                action(i);
            }
        }
        public static void DownTo(this int source, int min, Action<int> action)
        {
            for (int i = source; i >= min; i--)
            {
                action(i);
            }
        }
        public static int[] Range(this int source, int max)
        {
            int difference = max - source;
            if (difference < 0) return new int[0];
            int[] ret = new int[difference + 1];
            for (int i = 0; i <= difference; i++)
                ret[i] = source+i;
            return ret;
        }

    }
}

This class file revolves around adding functions to the int class. The Times method allows for a specific integer capturing lambda expression to be executed source times. For example, 6.Times(x => Console.WriteLine(x)) would print lines from zero to 6.

UpTo and DownTo are similar to the Times method. However, they loop over a range of integers rather than from zero. Compare these to Ruby’s upto and downto methods.

Range is simply a way to create an array covering a range of integers. The source is the minimum value and max is the largest of the array.

IEnumerableExtensions.cs

using System;
using System.Collections.Generic;

namespace StatenUtil
{
    public static class IEnumerableExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            IEnumerator<T> enumerator = source.GetEnumerator();
            while (enumerator.MoveNext())
                action(enumerator.Current);
        }
    }
}

Currently, this class file add inline ForEach capabilities to any generic IEnumerable object, similar to Ruby’s each. Although List has this capability, other IEnumerables like arrays do not.

That’s all for now. However, StatenUtil is a work in progress that’s always being amended. In each significant addition, the details will be shared once again.

Photography Friday IX: Wells Fargo Bank Notes
Feb 27th, 2009 by Jason

Notes

These bank notes were recorded for the purpose of Wells Fargo many years ago when traveling Westward. I believe it’s just amazing to think about how far we have come since then and can hardly imagine where we’re headed. I’m sure the writers of this never expected anything like online banking, worldwide transactions, and billions of dollars under their control. It’s inspiring to know that where we are now in life does not represent what we will be always.

»  Substance: WordPress   »  Style: Ahren Ahimsa Blog Flux Local - Utah
© Jason Staten 2009