[QUIZ] Hello, world? (#158)

G

Gordon Thiesfeld

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
 
J

James Tucker

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


#!/usr/bin/env ruby -KU

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

Jeremy McAnally

require 'rubyinline'

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

hello_world
end

--Jeremy

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



--
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://mrneighborly.com
http://rubyinpractice.com
 
W

_why

- 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
 
E

elof

require 'net/http'
require 'uri'

#
# Google knows everything ;-)
#

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('\[<b>QUIZ<\/b>\]\s(\w+, \w+).+as often as you can(\W)')[1..2].join("")
 
G

Gaspard Bucher

require 'net/http'
require 'uri'

#
# Google knows everything ;-)
#

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('\[<b>QUIZ<\/b>\]\s(\w+, \w+).+as often as you can(\W)')[1..2].join("")

Excellent !
 
J

Jari Williamsson

Here's one:

puts DATA.read
__END__
Hello, World!


Best regards,

Jari Williamsson
 
M

Mtko Pwnage

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
 
R

Rubén Medellín

The three rules of Ruby Quiz 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
 
S

Sharon Phillips

Looks better in a fixed width font :)

[[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
 
J

Justin Collins

Rubén Medellín said:
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
 
S

Stefano Crocco

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
 
S

Sharon Phillips

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],
:eek:=> [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,:eek:,:space,:w,:eek:,: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
 
F

F. Senault

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
 
A

Adam Shelly

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
 
G

Gaston Ramos

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.
--=20
"Computer science education cannot make anybody an expert programmer any mo=
re
than studying brushes and pigment can make somebody an expert painter."

(Eric Raymond)


+-------------------------------------+
Gast=F3n Ramos
http://gastonramos.wordpress.com/
GNU/Linux Counter user #450312
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top