Matthew Moss
3/7/2008 3:00:00 PM
A simple problem deserves a simple answer... except when you explicitly ask
for something *other* than the simple answer. Which I did ask. And I got
answers... lots of them, with a wide variety of techniques, though a few of
the methods were repeated, each with slight variations.
First, we have the *joining letters* technique. Array's join method makes
it easy to create a string from parts, and a number of solutions used this
or string concatenation to build up the "Hello, world!" string. Often the
primary difference in these solutions was from where the individual letters
were taken.
Here is one example of the joining letters technique, from Robert Dober:
puts [?H, ?e, ?l, ?l, ?o, ?,, ?\s, ?W, ?o, ?r, ?l, ?d, ?!].
inject("") { |s, char| s << char }
Remember that a ? in front of a character returns the ASCII value of that
character. Robert joins these values from his array not with the join metho=
d,
but with Enumerable's inject method and String's concatenation operator, wh=
ich
will convert an argument between 0 and 255 to a character before concatenat=
ion.
Second, there were a lot of applications of the *method_missing* technique.
Usually this involved taking the name of the call and using it as part of t=
he
output. There were a number of solutions that looked similar to Jesse
Merriman's first-in method_missing solution:
class Hello
def method_missing m; print m; self; end
end
Hello.new.H.e.l.l.o.send(', ').w.o.r.l.d!.send("\n")
Since the Hello class doesn't define any methods except method_missing, any
attempt to call a method (except, of course, those defined by Object) will
end up in this method_missing call with the argument m containing the
attempted method name, which then is immediately printed to standard output=