Hello, world? (#158)

Here are my solutions.

class Greeting

def self.
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

|Matthew D Moss|

require ‘open-uri’

open(‘http://matthew.moss.googlepages.com/hello%2Cworld%3F’) {|page| if
page.read =~ /prints out "(.*?)" to the console/ : puts $1
end}

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

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 [email protected]
wrote:

  1. Enjoy!
    The first program any new programmer typically sees is one that
    a ‘hell’;
    Ruby can present the same output very simply:

be one- or two-liners, some solutions to involve classes and

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


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

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

Or, my blogs:

http://rubyinpractice.com

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.

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

require ‘net/http’
require ‘uri’

Google knows everything :wink:

gke_url = URI.parse(“http://www.google.com”)
gke = Net::HTTP.start(gke_url.host, gke_url.port) { |http|
http.get(“/search?q=ruby%20quiz%20158”)
}
puts gke.body.match(‘[QUIZ</b>]\s(\w+, \w+).+as often as you
can(\W)’)[1…2].join(“”)

On Sun, Mar 2, 2008 at 9:05 PM, Justin E. [email protected]
wrote:

Here’s my solution:

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

Nice.

Todd

}
puts gke.body.match(’[QUIZ</b>]\s(\w+, \w+).+as often as you can(\W)’)[1…2].join("")

Excellent !

Here’s one:

puts DATA.read
END
Hello, World!

Best regards,

Jari W.

Hello,
Sorry that this reply seems simple, but ruby is the first programming
language I’ve learned and I’ve only been practicing it for a few weeks.
this is what I came up with:

def hello
a = [“H”, “e”, “l”, “l”, “o”, “,”, " ", “W”, “o”, “r”, “l”, “d”, “!”]
puts(a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]+a[10]+a[11]+a[12])
end

hello

On Feb 29, 6:06 pm, Matthew D Moss [email protected] wrote:

The three rules of Ruby Q. 2:

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

Rubén Medellín wrote:

On Feb 29, 6:06 pm, Matthew D Moss [email protected] wrote:

The three rules of Ruby Q. 2:

Please, don’t ask me HOW i can sleep at nights…

Great. Now I can’t sleep %-P

Rubén Medellín wrote:

On Feb 29, 6:06 pm, Matthew D Moss [email protected] wrote:

The three rules of Ruby Q. 2:

Please, don’t ask me HOW i can sleep at nights…

#— BEGIN scary stuff

#-- END scary stuff

I declare you the winner. Even though this isn’t a competition. I
thought there was something really messed up with the formatting until I
actually ran it.

Wow. Many kudos to you.

-Justin

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

I may have missed it, but I didn’t see this solution:

require ‘enumerator’
“Hello world!\n”.enum_for(:each_byte).inject($stdout){|res, b| res <<
b.chr}

Which in 1.9 looks much better:

“Hello world!\n”.each_char.inject($stdout){|res, c| res << c}

Stefano

Here’s another in a similar vein to my previous one (oh for the good
old days…)

charset= {
:h=> [130,130,130,254,134,134,134,0],
:e=> [254,128,128,248,192,192,254,0],
:l=> [128,128,128,192,192,192,254,0],
:o=> [126,130,130,134,134,134,252,0],
:w=> [130,130,130,150,150,150,108,0],
:r=> [252,130,130,252,194,194,194,0],
:d=> [252,130,130,194,194,194,252,0],
:space=> [0,0,0,0,0,0,0,0]}

msg= [:h,:e,:l,:l,:o,:space,:w,:o,:r,:l,:d]

def to_pixels val
val.to_s(2).rjust(8,‘0’).gsub(‘0’,’ ').gsub(‘1’,‘X’)
end

0.upto(7) do |line|
msg.each do |ch|
print to_pixels(charset[ch][line])
end
puts
end

Cheers,
Dave

Justin C. wrote:

#-- END scary stuff

I declare you the winner. Even though this isn’t a competition. I
thought there was something really messed up with the formatting until
I actually ran it.

Wow. Many kudos to you.

-Justin

Rubén’s without line wrapping: *http://tinyurl.com/2pydc2
*

Le 01 mars à 01:06, Matthew D Moss a écrit :

Here’s my simple method_missing one :

class H
@acc = ‘H’

def H.bang!
puts @acc << " !"
end

def H.method_missing(m)
@acc << m.to_s.sub(/_/, ’ ')
self
end
end

H.e.l.l.o._.W.o.r.l.d.bang!

Fred

On 2/29/08, Matthew D Moss [email protected] wrote:

Your task this week is to print “Hello, world!” to standard output
using Ruby in atypical fashion.

A few basic ones that I haven’t seen anyone submit yet.
#unicode
puts [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100,
33].pack(‘U*’)

#longs.
puts [1214606444, 1865162839, 1869769828, 555753482].pack(‘N*’)

#deltas.
c=0
puts [72, 29, 7, 0, 3, -67, -12, 55, 24, 3, -6, -8, -67].map{|i|
(c+=i).chr}*‘’

-Adam

My 2 solutions:

[‘h’,‘e’,‘l’,‘l’,‘o’,‘, ‘,‘w’,‘o’,‘r’,‘l’,‘d’,’!’].inject{|v, m| v + m}

module Hello; def self.included(klass); klass.hello ‘, world!’; end; end

class World
def self.method_missing(method_name, *args)
puts method_name.to_s + args[0]
end
include Hello
end

Regards.

“Computer science education cannot make anybody an expert programmer any
more
than studying brushes and pigment can make somebody an expert painter.”

(Eric Raymond)

±------------------------------------+
Gastón Ramos

GNU/Linux Counter user #450312