From: Robert Dober Date: 2008-03-03T15:22:54+09:00 Subject: Re: [QUIZ] Hello, world? (#158) Just wanted to say "Hello" ;) # Probably my only solution that makes sense ;) puts "hello, world!".split.map{ |x| x.capitalize }.join(' ') # Amazingly you can do << with ints to strings puts [?H, ?e, ?l, ?l, ?o, ?,, ?\s, ?W, ?o, ?r, ?l, ?d, ?!]. inject(""){|s,char| s << char} # A tribute to perl ;) def method_missing *args, &blk puts "Hello, World" end croak # You cannot add strings and ints begin puts [?h, ?e, ?l, ?l, ?o, ?,, ?\s, ?w, ?o, ?r, ?l, ?d, ?!]. inject(""){|s,char| s + char} abort "If you do not succeed..." rescue puts "!dlroW ,olleH".reverse end # # What Mixins can do for you module Hello def hello_world "Hello," end end module World def hello_world super << " World!" end end class HelloWorld extend Hello extend World def self.hello_world puts super end end HelloWorld.hello_world # Abusing Kernel module Kernel alias_method :_puts, :puts def puts *args, &blk _puts "Hello, World!" end end puts # Metaprogramming on classes class A def self.inherited dummy print "Hello," end end class B < A class << self def inherited dummy print " World!" end end end class << C = Class::new( B ) def inherited dummy puts end end Class::new C # A Hommage to James' Gödel Quiz and showing that Integers are immutable class Integer def inject_chars istring, &blk return istring if zero? ( self / 256 ).inject_chars( blk.call( istring, self % 256 ), &blk ) end end puts 2645608968345021733469237830984.inject_chars(""){ |s,n| s+n.chr } # # # A more sensible approach of abusing Kernel::puts ;) module Sensible def puts *args return Kernel::puts( *args ) unless args.empty? Kernel::puts "Hello, World!" end end include Sensible puts puts "Hello, Ruby Quiz!" # A functional approach, introducing the splash def print_strings *strings return puts if strings.empty? print strings.shift print_strings *strings end print_strings %{Hello, World!} # Blocks and Procs, split(//) module Printable def to_proc lambda { each do |elem| print elem end } end end Array.send :include, Printable def yielder yield end yielder &("Hello, World!" << 10).split(//) # A radical and viral solution ObjectSpace.each_object(Module) do |mod| mod.send :define_method, :hello_world do puts "Hello, World!" end end 42.hello_world Object.hello_world nil.hello_world (A = Class::new).hello_world -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein