So A Guy Comes To My Door, Wanting To Sell Me Some Meat…

As the title suggests, about 30 minutes ago a guy comes to the door, wanting to sell me some meat. That sounds like the setup for a bad joke, but it isn’t. My wife told him to wait outside, and then she came and got me. I went out on the porch and he proceeded to tell me that he was from “The Cattle Exchange” and that he had some great deals on “steaks, pork, chicken and seafood.” I walked up to his truck, and he proceeded to show me a box of nice-looking, vacuum-packaged steaks. After showing me all the meat, he whipped out a price list. Here’s a recap of the ensuing discussion

Him: Now, if you were to order this online, it would cost you $451. But when I’m out here, doing what I do, I’m supposed to get $299…

Me: Wow. That’s still a lot of money.

Him: … and if you buy the case, I’ll throw in a case of chicken for $20.

Me: That’s still an awful lot of money for meat.

Him: Well, what if I cut another $100 off that price?

Me: You know, I’m gonna pass. We just don’t eat that much steak. Plus, that’s still a lot of money, to a company I’ve never heard of, and a guy who showed up at my door wanting to sell me some meat. No offense. (That last was said in my best Paulie Walnuts voice, with the appropriate hand gestures.) I’ll look your company up online and maybe think about it.

He then packed up his wares and left.

The whole thing just felt wrong. My first thought was that either this guy is ripping off his company, and selling it for less than he was supposed to, or his company was ripping people off on their website. I came back inside and looked up the company. It turns out my Spidey Sense™ was spot on with this outfit. Check out this headline Atlanta Cattle Exchange Complaints – Door-to-door meat sellers rip off! The comments are pretty enlightening, too. Looks like I made the right decision in not trusting him. It’s basically a scam from start to finish.

So, if a guy shows up at your door trying to sell you some meat, you should… wait for it… send him packing. Get it? Get it? Meat… packing… Thank you, thank you. I’ll be here all week. Try the veal and remember to tip your waitress.

iPhone Interface For My Blog

09/28/2009 Update: Now added a link to the view from Android.

Last week I learned about WPTouch, which is a plugin for WordPress that reformats the theme for the iPhone, Android and other mobile devices. It was an easy install, and I am now happy to report that if you view my blog on a mobile device, you’ll see the new UI. Here’s what it looks like on an iPhone

IMG_0465

If anybody has an Android phone, or some other supported mobile device, send me a screenshot so I can see what it looks like.

Thanks to Steve Ziegler, here’s what it looks like from an Android device. Thanks, Steve!

I’m Liking Scala’s XML Literals

How many times have you written a class that you needed to save and restore to/from XML? How did you do it? There are libraries that will do this for you, but I don’t know if any of them have taken the lead. Usually, you have to either have external XML templates that will be filled in, or you have to create the XML through code using DOM or JDOM or some other DOM. I think Scala has a better way.

Enter Scala’s XML literals. In Scala, you can embed XML right in the middle of regular Scala code. For example,

val num = 23

val xml = 
	<person>
		<name>Tom</name>
	</person>
	
println(num)
println(xml)

In this code, we assign to the val called num the Int value of 23. We then assign the xml val an XML Node, that begins with “person”. That’s neat, but not terribly useful. Here’s where it gets really interesting.

import scala.xml.{Node, XML}

class Person(val id: Int,
			 val firstName: String,
			 val lastName: String,
			 val title: String) {

	override def toString = {
		id + ", " + firstName + " " + lastName + ": " + title
	}
	
	def toXML = {
		<person>
			<id>{id}</id>
			<firstName>{firstName}</firstName>
			<lastName>{lastName}</lastName>
			<title>{title}</title>
		</person>
	}
}

object Person {
	def fromXML(node: Node): Person = {
		new Person(
			(node  "id").text.toInt,
			(node  "firstName").text,
			(node  "lastName").text,
			(node  "title").text
		)
	}
}

val p = new Person(23, "Tom", "Servo", "Robot")
println(p)

val xml = p.toXML

println(xml)

XML.saveFull("Person.xml", xml, "UTF-8", true, null)

val xmlFromFile = XML.loadFile("Person.xml")

val p1 = Person.fromXML(xmlFromFile)
println(p1)

That’s a longer example, but there’s a lot of interesting stuff in there. Lines 3 – 20 are defining a Scala class, but look at lines 12 – 19. Those lines define a method called toXML that returns the current instance of the class, serialized to XML. I’ve defined the XML structure that I want, and Scala will fill in the values by replacing the stuff between the curly braces with whatever those expressions evaluate to. In this case, it’s just the instance variables, but it could be anything you want, including more XML. So if you have a more complex data structure, it could call the toXML method on its instance variables, and they could handle their own serialization, which would then be placed where it should be in the parent XML document. Nifty, eh?

Now, look at lines 22 – 31. This bit of code is defining a companion object to the Person class, which defines a method to deserialize XML into an instance of class Person. Strictly speaking, I didn’t have to use a companion object; I could have just defined the fromXML method at the top level, but I think this makes the code cleaner. What this method does is take an XML node, tear it apart, and return an instance of class Person, with its instance variables populated.

Lines 33 – 45 exercise this code. The result of running this example look like this

box:~/tmp> scala person.scala 
23, Tom Servo: Robot
<person>
			<id>23</id>
			<firstName>Tom</firstName>
			<lastName>Servo</lastName>
			<title>Robot</title>
		</person>
23, Tom Servo: Robot
box:~/tmp>

Pretty neat, eh? In the exercise code, we created a Person object, printed it out, serialized it to XML, printed that out, then saved the XML to a file. We then read that file back in, and deserialized it into another instance of class Person. Using XML literals for de/serialization is a nice application. I can see using this quite a lot. I like how it keeps the XML format right there with the code it will represent, so that when you change the code, you’ll remember to change the XML, too.