[QUIZ] Hello, world? (#158)

The three rules of Ruby Quiz 2:
1. Please do not post any solutions or spoiler discussion for this quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A permanent, new website is in the works for Ruby Quiz 2. Until then, please visit the temporary website at

     <http://matthew.moss.googlepages.com/home>.

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone on Ruby Talk follow the discussion. Please reply to the original quiz message, if you can.

···

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Hello, world?

The first program any new programmer typically sees is one that prints out "Hello, world!" to the console. This tends to be something experienced programmers also see when learning a new language. The first Hello World program was written in B [1] by Kernighan and looked like this:

         main( ) {
             extrn a, b, c;
             putchar(a); putchar(b); putchar(c); putchar('!*n');
         }

         a 'hell';
         b 'o, w';
         c 'orld';

Most programmers are probably more familiar with the typical C implementation:

         main() {
             printf("Hello, world!\n");
         }

Ruby can present the same output very simply:

         puts "Hello, world!"

But that's too simple... I mean, really... *anyone* can print a simple string to standard output. Can't we get more interesting? Hmmm, how about:

         puts sprintf("%s, %s!", "Hello", "world")

Eh, that looks too much like C. Maybe...

         puts %w(Hello world).join(", ") + "!"

Yeah, that's definitely looking Ruby-ish.

Your task this week is to print "Hello, world!" to standard output using Ruby in atypical fashion. Some guildlines:

- DO submit multiple variants in your submission, but we don't need 100 variants from everyone. Try to limit yourself to your best dozen.
- DO keep things reasonably simple. I would expect many solutions to be one- or two-liners, some solutions to involve classes and functions, and a variety in-between. But we're looking for Ruby-isms, not volume of code, so don't write pages upon pages of code just to print "Hello, world!"
- DON'T obfuscate unnecessarily. We're looking for interesting Ruby tricks, not utter confusion. A little obfuscation is okay, but a lot is to be avoided.
- DON'T send me my own examples from above. I already did them. Do other stuff. It *is* okay if your solution is similar to mine, provided you make some interesting modifications.

[1] http://cm.bell-labs.com/cm/cs/who/dmr/btut.html

Greetings!

Here's the result of my quiz solution(s) so far:

$ ruby 158_hello_world.rb
Hello, World!

<grin>

Regards,

Bill

Greetings,

My quiz solution(s) are here:

http://tastyspleen.net/~billk/ruby/quiz/158-hello-world/158_hello_world.rb

I did five Hello World variations. Thus, the output of the program
when run, is simply:

Hello, World!

I think I had the most fun with #4, the maze solver:

m = <<MAZE

···

###+---+---+
^ H| l| o|
#| e| l| |
#+---+-+-+ |
#| d !*| , |
#| +--++ -+#
#|l | _ |#
#+--+ +-+ |#
####|r |W|#
##+-+- -+ |#
##| o |#
##+--+----+#
MAZE

Regards,

Bill

Here's 6 little solutions (pastied http://pastie.caboo.se/160330 too). The
last two just print the file name, so they'll have to be in a "Hello, world!"
file (or as close as your OS supports).

## attempt 1

def print_caller
  print caller.first.match(/`(.*)'$/)[1]
end

def H; print_caller; end
def e; print_caller; end
def l; print_caller; end
def o; print_caller; end
def comma; print ','; end
def space; print ' '; end
def w; print_caller; end
def r; print_caller; end
def d!; print_caller; end
def newline; print "\n"; end

H(); e; l; l; o; comma; space; w; o; r; l; d!; newline

## attempt 2

class Hello
  def self.printer s; define_method(s) { print s; self }; end
  %w{H e l o w r d!}.each { |s| printer(s) }
end

Hello.new.H.e.l.l.o.w.o.r.l.d!

## attempt 3

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")

## attempt 4, quine-ish

#!/usr/bin/env ruby

require 'enumerator'

lines = File.readlines(__FILE__)
[20, 0,
  0, 11,
  4, 0,
  4, 0,
  2, 17,
  5, 3,
  0, 14,
18, 28,
  2, 17,
  0, 5,
  4, 0,
  4, 16,
  0, 1,
  1, 0].each_slice(2) { |row, col| print lines[row][col..col] }

Hash

## attempt 5

# Must be in a file called "Hello, world!"
begin
  raise :foo
rescue StandardError => se
  puts se.backtrace.first.match(/([^\/\\:]+):/)[1]
end

## attempt 6

# Must be in a file called "Hello, world!"
puts __FILE__

I decided to take a "meta" approach and created a quick and dirty code
generator. I taught it some basic Ruby Identities and hen let it have
at it. With just the 5 silly identities that it currently has it can
generate never ending variations, though they get repetitive very
quickly --nevertheless there are already some unexpected solutions. I
suppose with maybe a few dozen identities it could get pretty
interesting.

Try running like so:

  ruby helloword.rb 100
  ruby helloword.rb test 100
  ruby helloword.rb output 100

The number is the number of solutions to generate, 'test' will run it
through test/unit to make sure it all checks out and 'output' will
print a whole bunch of "Hello, World!"s :slight_smile:

Oh, and yes, I know it's not all that robust. It was just a fun quick
hack. But feel free to improve it and let me know if you have any good
identities to add.

T.

···

---

class SimpleCodeGenerator

  def self.identities
    @identities ||= []
  end

  def self.identity(&block)
    identities << block
  end

  attr_reader :original
  attr_accessor :alternates

  def initialize(original)
    @original = original
    @alternates = [original]
  end

  def generate(limit=100)
    work = original.dup
    while(alternates.size < limit) do
      alts = alternates.dup
      size = alternates.size
      alts.each do |alt|
        self.class.identities.each do |identity|
          self.alternates |= [identity[alt]]
          break if alternates.size >= limit
        end
      end
    end
  end

  def show_code
    alternates.each do |code|
      puts code
    end
  end

  def show_output
    alternates.each do |code|
      puts run(code)
    end
  end

  def generate_tests
    original_run = run(original)
    runner = method(:run)
    testcase = Class.new(Test::Unit::TestCase)
    alternates.each_with_index do |code, index|
      testcase.class_eval do
        define_method("test_#{index}") do
          assert_equal(original_run, runner[code])
        end
      end
    end
  end

  def run(code)
    so = $stdout
    sio = StringIO.new
    $stdout, $stderr = sio, sio
    eval code, $TOPLEVEL_BINDING
    result = $stdout.string
    $stdout = so
    result
  end

  # code identities

  identity do |code|
    code.sub(/puts ["](.*)["]/, 'print "\1\n"')
  end

  identity do |code|
    code.sub(/puts ["](.*)["]/, 'printf "%s\n" % ["\1"]')
  end

  identity do |code|
    code.gsub(/["](.*)["]/, '"\1".reverse.reverse')
  end

  identity do |code|
    code.gsub(/['](.*)[']/){ $1.inspect }
  end

  identity do |code|
    code.gsub(/['](.*)[']/){ "#{$1.split(//).inspect}.join('')" }
  end

end

if __FILE__ == $0
  cnt = (ARGV.find{ |a| /\d+/ =~ a } || 20).to_i

  scg = SimpleCodeGenerator.new("puts 'Hello, World!'")
  scg.generate(cnt)

  case ARGV[0]
  when 'test'
    require 'test/unit'
    scg.generate_tests
  when 'output'
    scg.show_output
  else
    scg.show_code
  end
end

Here's all I came up with:

class Hello; def self.world!; [self, __callee__].join(' '); end; end

puts Hello.world!

Only works with Ruby 1.9 though...

···

On Feb 29, 7:06 pm, Matthew D Moss <matthew.m...@gmail.com> wrote:

The three rules of Ruby Quiz 2:
1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
permanent, new website is in the works for Ruby Quiz 2. Until then,
please visit the temporary website at

 &lt;http://matthew.moss.googlepages.com/home&gt;\.

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Hello, world?

The first program any new programmer typically sees is one that
prints out "Hello, world!" to the console. This tends to be something
experienced programmers also see when learning a new language. The
first Hello World program was written in B [1] by Kernighan and
looked like this:

     main\( \) \{
         extrn a, b, c;
         putchar\(a\); putchar\(b\); putchar\(c\); putchar\(&#39;\!\*n&#39;\);
     \}

     a &#39;hell&#39;;
     b &#39;o, w&#39;;
     c &#39;orld&#39;;

Most programmers are probably more familiar with the typical C
implementation:

     main\(\) \{
         printf\(&quot;Hello, world\!\\n&quot;\);
     \}

Ruby can present the same output very simply:

     puts &quot;Hello, world\!&quot;

But that's too simple... I mean, really... *anyone* can print a
simple string to standard output. Can't we get more interesting?
Hmmm, how about:

     puts sprintf\(&quot;%s, %s\!&quot;, &quot;Hello&quot;, &quot;world&quot;\)

Eh, that looks too much like C. Maybe...

     puts %w\(Hello world\)\.join\(&quot;, &quot;\) \+ &quot;\!&quot;

Yeah, that's definitely looking Ruby-ish.

Your task this week is to print "Hello, world!" to standard output
using Ruby in atypical fashion. Some guildlines:

- DO submit multiple variants in your submission, but we don't need
100 variants from everyone. Try to limit yourself to your best dozen.
- DO keep things reasonably simple. I would expect many solutions to
be one- or two-liners, some solutions to involve classes and
functions, and a variety in-between. But we're looking for Ruby-isms,
not volume of code, so don't write pages upon pages of code just to
print "Hello, world!"
- DON'T obfuscate unnecessarily. We're looking for interesting Ruby
tricks, not utter confusion. A little obfuscation is okay, but a lot
is to be avoided.
- DON'T send me my own examples from above. I already did them. Do
other stuff. It *is* okay if your solution is similar to mine,
provided you make some interesting modifications.

[1]http://cm.bell-labs.com/cm/cs/who/dmr/btut.html

def method_missing(method) puts method end
def Object.const_missing(const) print const.to_s + ", " end
[Hello, World!]

To me, the essence of a "hello, world" program is to get the phase on the computer screen. But "on the screen" doesn't have to mean "write to stdout". My three solutions all avoid stdout.

The first solution highjacks Apple's TextEdit application.

<code>
require "appscript"
include Appscript
te = app('TextEdit')
te.launch
te.documents.end.make(:new => :document,
                       :with_properties => {:text => "hello, world\n"})
</code>

The second solution uses an AppleScript dialog.

<code>
require "osax"
include OSAX
osax.display_dialog("hello, world")
</code>

These first two solutions are Apple-centric. I don't apologize for that. I also employed the classic Kernighan and Ritchie version, just "hello, world", no caps or exclamation point.

The third solution uses Tk and should run on any system with a functioning Ruby/Tk library, but you may have to specify another font. This one is not so minimalist as the first two. Here I use what today seems to be the most common version of the phrase ("Hello, World!") just because it looks prettier in 72-point Brush Script.

<code>
require 'tk'

root = Tk.root
root.title('Ruby/Tk Hello')
win_w, win_h = 360, 160
win_x = (root.winfo_screenwidth - win_w) / 2
root.geometry("#{win_w}x#{win_h}+#{win_x}+50")
root.resizable(false, false)

TkLabel.new(root) {
    text 'Hello, World!'
    relief 'solid'
    borderwidth 1
    font('family'=>'Brush Script MT', 'size'=>72)
    place("x"=>10, "y"=>10, "height"=>92, "width"=>340)
}

TkButton.new(root) {
    text 'Goodbye'
    command { Tk.root.destroy }
    place("x"=>138, "y"=>122, "height"=>28, "width"=>83)
}

Tk.mainloop
</code>

The use of 'place', rather than 'pack' may seem strange. I confess I wrote the Ruby/Tk version quite some time ago when I was experimenting with the 'place' layout manager.

Regards, Morton

Nothing too much for now. Wasn't sure _how_ unobfuscated to do them, so i just did simple ones. Except for my first one, which flashes different colors.

Hit <enter> to stop the loop!

http://pastie.caboo.se/160366

~ Ari
English is like a pseudo-random number generator - there are a bajillion rules to it, but nobody cares.

Here's my solution:

hello, world = "", 2645608968345021733469237830984
(hello << (world % 256); world /= 256) until world == 0
puts hello

Have a good one,

Justin

···

On Fri, Feb 29, 2008 at 7:06 PM, Matthew D Moss <matthew.moss@gmail.com> wrote:

The three rules of Ruby Quiz 2:
1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
permanent, new website is in the works for Ruby Quiz 2. Until then,
please visit the temporary website at

    <http://matthew.moss.googlepages.com/home&gt;\.

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Hello, world?

The first program any new programmer typically sees is one that
prints out "Hello, world!" to the console. This tends to be something
experienced programmers also see when learning a new language. The
first Hello World program was written in B [1] by Kernighan and
looked like this:

        main( ) {
            extrn a, b, c;
            putchar(a); putchar(b); putchar(c); putchar('!*n');
        }

        a 'hell';
        b 'o, w';
        c 'orld';

Most programmers are probably more familiar with the typical C
implementation:

        main() {
            printf("Hello, world!\n");
        }

Ruby can present the same output very simply:

        puts "Hello, world!"

But that's too simple... I mean, really... *anyone* can print a
simple string to standard output. Can't we get more interesting?
Hmmm, how about:

        puts sprintf("%s, %s!", "Hello", "world")

Eh, that looks too much like C. Maybe...

        puts %w(Hello world).join(", ") + "!"

Yeah, that's definitely looking Ruby-ish.

Your task this week is to print "Hello, world!" to standard output
using Ruby in atypical fashion. Some guildlines:

- DO submit multiple variants in your submission, but we don't need
100 variants from everyone. Try to limit yourself to your best dozen.
- DO keep things reasonably simple. I would expect many solutions to
be one- or two-liners, some solutions to involve classes and
functions, and a variety in-between. But we're looking for Ruby-isms,
not volume of code, so don't write pages upon pages of code just to
print "Hello, world!"
- DON'T obfuscate unnecessarily. We're looking for interesting Ruby
tricks, not utter confusion. A little obfuscation is okay, but a lot
is to be avoided.
- DON'T send me my own examples from above. I already did them. Do
other stuff. It *is* okay if your solution is similar to mine,
provided you make some interesting modifications.

[1] http://cm.bell-labs.com/cm/cs/who/dmr/btut.html

# i -- raising an HelloWorldError

class HelloWorldError < StandardError
  def to_s
    "Hello, World!"
  end
end
module Kernel
  def raise(e)
    puts e
  end
end
raise HelloWorldError.new

# ii -- a lost and found HelloWorld

class HelloWorld
  def to_s
    "Hello, World!"
  end
end
HelloWorld.new
ObjectSpace.each_object { |o| puts o if o.is_a?(HelloWorld) }

# iii -- a little curry

hello = lambda { |a| "Hello, #{a.call}"}
world = lambda { "World!" }
puts hello.call(world)

# iv -- in only one line but to ruby instances

puts `ruby -e 'puts "Hello, World!"'`

# v -- and finally a probabilistic approach
# beware this one is not tested yet!
# which means the the test is still running and at
# least it didn't raise any errors so far :wink:

goal = "Hello, World!\n"
bytes = goal.split(//).map { |e| e[0] }.uniq

seed = 0
until false
  srand(seed)
  i = 0
  while i < goal.size
    c = rand(bytes.size)
    break if bytes[c] != goal[i]
    i += 1
  end
  break if i == goal.size
  seed += 1
end

goal.size.times { print bytes[rand(bytes.size)].chr }

g phil

Only a set of very basic solutions:

#!/usr/bin/env ruby19

puts '-- Exact solutions --'

STDOUT << 'Hello' << ', ' << 'world' << '!' << "\n"

puts 'Holle, werld!'.tr('eo', 'oe')

puts '!dlrow ,olleH'.reverse

def method_missing(*a); puts 'Hello, world!'; end; say_hi

a = [1,2,3]; def a.to_s; 'Hello, world!'; end; puts "#{a}"

require 'zlib'
hw = Zlib::Deflate.new.deflate("Hello, world!", Zlib::FINISH)
puts Zlib::Inflate.new.inflate(hw)

puts [1819043144, 1998597231, 1684828783,
538976289].pack('VVVV').strip

puts ('%x' % 10484973469620254453249450853906).scan(/../).pack('h2' *
13)

puts '-- Approximative solutions --'

puts 'Hello, world!'.split('').map {|c| /\l/ =~ c ? '%c' % (c.ord +
rand(3) - 1) : c}.join

puts 'Hello, world!'.gsub(/\b\w{4,}?\b/) {|t| t[1..-2] =
t[1..-2].split('').shuffle.join; t }

puts [6.51886850036479e+265, 6.01347722536415e-154].pack('EE').strip

Just wanted to say "Hello" :wink:

# Probably my only solution that makes sense :wink:
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 :wink:
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 :wink:
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

Here are my solutions.

class Greeting

  def self.[](thing)
    new(thing)
  end

  def initialize(thing)
    @thing = thing
  end

  def inspect
    "#{self.class}, #{@thing}"
  end

  alias to_s inspect

  def self.method_missing(method)
    new(method)
  end

end

def Object.const_missing(const)
  const_set(const, Class.new(Greeting))
end

def method_missing(arg)
  "#{arg}\n"
end

$, = ", "
$stderr = $stdout

p Hello.new(:world!)
puts Hello[:world!]
warn Hello.world!

print Hello, world!
print [Hello, world!]

hash = { Hello => :world! }
puts hash

Lame of me I know, but I could only come up with the following immediately, and yes, it's quite obfuscated. Anyway, enjoy:

#!/usr/bin/env ruby -KU

$><< [ DATA.read.strip! ] <<
[]<<<<§[ %r [\ ]w\w+ iomx ] <<
                               world
§
                               ?!.chr
__END__
                               hello

Matthew D Moss|

require 'open-uri'

open('http://matthew.moss.googlepages.com/hello%2Cworld%3F&#39;\) {|page| if page.read =~ /prints out &quot;(.*?)&quot; to the console/ : puts $1 end}

···

--
I. P. 2008-03-03T21:07

require 'rubyinline'

inline do |builder|
  builder.c "
    void hello_world() {
      printf("Hello, world!\n");
    }"

  hello_world
end

--Jeremy

···

On Fri, Feb 29, 2008 at 7:06 PM, Matthew D Moss <matthew.moss@gmail.com> wrote:

The three rules of Ruby Quiz 2:
1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
permanent, new website is in the works for Ruby Quiz 2. Until then,
please visit the temporary website at

     <http://matthew.moss.googlepages.com/home&gt;\.

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Hello, world?

The first program any new programmer typically sees is one that
prints out "Hello, world!" to the console. This tends to be something
experienced programmers also see when learning a new language. The
first Hello World program was written in B [1] by Kernighan and
looked like this:

         main( ) {
             extrn a, b, c;
             putchar(a); putchar(b); putchar(c); putchar('!*n');
         }

         a 'hell';
         b 'o, w';
         c 'orld';

Most programmers are probably more familiar with the typical C
implementation:

         main() {
             printf("Hello, world!\n");
         }

Ruby can present the same output very simply:

         puts "Hello, world!"

But that's too simple... I mean, really... *anyone* can print a
simple string to standard output. Can't we get more interesting?
Hmmm, how about:

         puts sprintf("%s, %s!", "Hello", "world")

Eh, that looks too much like C. Maybe...

         puts %w(Hello world).join(", ") + "!"

Yeah, that's definitely looking Ruby-ish.

Your task this week is to print "Hello, world!" to standard output
using Ruby in atypical fashion. Some guildlines:

- DO submit multiple variants in your submission, but we don't need
100 variants from everyone. Try to limit yourself to your best dozen.
- DO keep things reasonably simple. I would expect many solutions to
be one- or two-liners, some solutions to involve classes and
functions, and a variety in-between. But we're looking for Ruby-isms,
not volume of code, so don't write pages upon pages of code just to
print "Hello, world!"
- DON'T obfuscate unnecessarily. We're looking for interesting Ruby
tricks, not utter confusion. A little obfuscation is okay, but a lot
is to be avoided.
- DON'T send me my own examples from above. I already did them. Do
other stuff. It *is* okay if your solution is similar to mine,
provided you make some interesting modifications.

[1] http://cm.bell-labs.com/cm/cs/who/dmr/btut.html

--
http://jeremymcanally.com/
http://entp.com

Read my books:
Ruby in Practice (http://manning.com/mcanally/\)
My free Ruby e-book (http://humblelittlerubybook.com/\)

Or, my blogs:

http://rubyinpractice.com

I know this is a bit golfy, but I thought I'd try to only use strings that had
already been interned into Ruby.

  require 'rbconfig'
  bui = /^bui(.{2})$/
  $stdout << "#{{}.class}"[0,1] <<
   ("#{{}.methods}"[/c(\w{4})c/] && $1.reverse) <<
   (([0]*2).inspect[2,2]) <<
   Config::CONFIG.keys.grep(bui).first.gsub(bui,
     "#{Kernel.methods.grep(/^th/)[0][2,3].reverse}\\1") <<
   ObjectSpace._id2ref(338)

Here's a shorter one:

  puts IO.read($:[3]+"/drb/drb.rb")[/logger.log."(.{13})"/, 1]

_why

···

On Sat, Mar 01, 2008 at 09:06:05AM +0900, Matthew D Moss wrote:

- DON'T obfuscate unnecessarily. We're looking for interesting Ruby tricks,
not utter confusion. A little obfuscation is okay, but a lot is to be
avoided.

Please, don't ask me HOW i can sleep at nights....

#--- BEGIN code
                  def method_missing(a = p, *c); return ; nruter ;(c*
p = a);gnissim_dohtem fed
                                                    end ; dne
                                 alias m method_missing ;
gnissim_dohtem m; saila
                                         class NilClass ; ssalCliN
ssalc
                                     alias inspect to_s ; s_ot
tcepsni ;saila
                                                    end ; dne
                                          class Integer ; regetnI
ssalc
def method_missing(a=chr,*b);print chr;return a.to_s[0] ; [0];s_ot.a
nruter;rhc tnirp;(b*rhc=a);gnissim_dohtem fed
                                                    end ; dne
                          def d! ; return(d.e and puts) ; (stup dna;
e.d);nruter ; !d fed
                                              dne ; end ; dne ; end
                               def p(p = a, *b) ; begin ; nigeb ; (b*
a = p); p fed
                         rescue ; print p.to_s ; return ; nruter ;
s_ot.p tnirp ; eucser
                                              dne ; end ; dne ; end
                           dne def a b = c ; return nil ; lin nruter ;
c = b a; fed end
                                              a = a def fed a = a
                                                 return nruter
                                              a = p end ; fed ; def
dne p = a
                                                    p a ; a p
                                                    end ;
dne
      def h ax0=0xa;return 0xa unless ax0;dne;y=x p;fed = def p
x=y;end; 0xa;sselnu ax0;nruter ax0=0xa;h fed
                                               bx0, dx0 = 0xd, 0xb
                                        bx0 + dx0 + 0xb + bx0 + 0xd +
0xb
                                                    end ; dne
                             def Object.const_missing a ; a
gnissim_tsnoc.tcejbO; fed
                           return send(a.to_s.downcase) ;
(esacnwod.s_ot.a);dnes nruter
                                                    end ; dne
                                 H.e.l.l.o._.w.o.r.l.d! ; !
d.l.r.o.w._.o.l.l.e.H
#-- END code

···

On Feb 29, 6:06 pm, Matthew D Moss <matthew.m...@gmail.com> wrote:

The three rules of Ruby Quiz 2:

Looks better in a fixed width font :slight_smile:

[[1,3,1,1,5,1,1,5,1,6,3,5,1,3,1,2,3,2,4,2,1,5,4],
  [1,3,1,1,1,5,1,5,1,5,1,3,1,4,1,3,1,1,1,3,1,1,1,3,1,1,1,5,1,3,1],
  [5,1,4,2,1,5,1,5,1,3,1,4,1,1,1,1,1,1,1,3,1,1,4,2,1,5,1,3,1],
  [1,3,1,1,1,5,1,5,1,5,1,3,1,4,1,1,1,1,1,1,1,3,1,1,1,3,1,1,1,5,1,3,1],
  [1,3,1,1,5,1,5,1,5,2,3,6,1,1,1,3,3,2,1,3,1,1,5,1,4]].each do |line|
   line.each_with_index{|c,i|print((i%2==0 ? '*' : ' ')* c)}
   puts
end

I haven't read through all the solutions yet, so I hope I'm not repeating what someone else has done.

Cheers,
Dave