[QUIZ] Ducksay (#52)

R

Ruby Quiz

The three rules of Ruby Quiz:

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 by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

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

by Jonas Pfenniger

__________________________________
< Welcome to this week's Ruby Quiz >
----------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

This week, we will make some ascii-art for fun.

Produce a script that generates funny talking animals. The executable
consists of three parts.

Part One : The balloon
=====================

The balloon is the surrounding part, in which the text will be shown. You can
make it like you want.

Examples :

One-liner
_______________
< I love ruby ! >
---------------

Multiple lines with text wrapping and carriage return
__________________________________________
/ Q:How do you stop an elephant from \
| charging? |
\ A:Take away his credit cards. /
------------------------------------------

Part Two : The template
=======================

To make it flexible, we'll need a template language that can combine the animal
and the text. You'll have to think about how you want to make it flexible
enough.

Example :

# thoughts is the balloon's tail
def animal(thoughts = '\', eyes = 'oo', tongue=' ')
%[#{baloon}
#{thoughts} ^__^
#{thoughts} (#{eyes})\\_______
(__)\\ )\\/\\
#{tongue} ||----w |
|| ||
]
end

Part Three : The command-line arguments
=======================================

There are no special arguments. I would suggest asking for a text input, but you
can also get the text from `fortune` or make it editable in place. Optional
arguments can be given to change the selected template and some of its
variables like the eyes, the tongue, and the balloon's tail.

Use your imagination !

So that's it for the quiz. Extra points are given to the person who
provides a duck template.

Credits
=======

- cowsay : http://www.cowsay.net/
 
E

Ezra Zygmuntowicz

This looks like a really fun quiz!

The three rules of Ruby Quiz:

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 by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

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

by Jonas Pfenniger

__________________________________
< Welcome to this week's Ruby Quiz >
----------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

This week, we will make some ascii-art for fun.

Produce a script that generates funny talking animals. The executable
consists of three parts.

Part One : The balloon
=====================

The balloon is the surrounding part, in which the text will be
shown. You can
make it like you want.

Examples :

One-liner
_______________
< I love ruby ! >
---------------

Multiple lines with text wrapping and carriage return
__________________________________________
/ Q:How do you stop an elephant from \
| charging? |
\ A:Take away his credit cards. /
------------------------------------------

Part Two : The template
=======================

To make it flexible, we'll need a template language that can
combine the animal
and the text. You'll have to think about how you want to make it
flexible
enough.

Example :

# thoughts is the balloon's tail
def animal(thoughts = '\', eyes = 'oo', tongue=' ')
%[#{baloon}
#{thoughts} ^__^
#{thoughts} (#{eyes})\\_______
(__)\\ )\\/\\
#{tongue} ||----w |
|| ||
]
end

Part Three : The command-line arguments
=======================================

There are no special arguments. I would suggest asking for a text
input, but you
can also get the text from `fortune` or make it editable in place.
Optional
arguments can be given to change the selected template and some of its
variables like the eyes, the tongue, and the balloon's tail.

Use your imagination !

So that's it for the quiz. Extra points are given to the person who
provides a duck template.

Credits
=======

- cowsay : http://www.cowsay.net/

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
(e-mail address removed)
 
J

James Edward Gray II

This looks like a really fun quiz!

I thought so too. Submitters always make better quizzes than me,
which is why I encourage them so much. ;)

This is a terrific problem for beginners, so hopefully we'll see some
new names in the solutions!

James Edward Gray II
 
J

JB Eriksson

------=_Part_13779_11337384.1129982235705
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I thought so too. Submitters always make better quizzes than me,
which is why I encourage them so much. ;)

This is a terrific problem for beginners, so hopefully we'll see some
new names in the solutions!

James Edward Gray II

Well, I'm new on this quiz thingy... And I've already have some results t=
o
show off (which I hope isn't a spoiler):
_________________________
/The quick brown fox jumps\
\ over the lazy dog. /
-------------------------
\
` >(o)____,
(` =3D~~/
^~^~^~^~^~`---'^~^~^~^~^~^~^~^~^~^~
I've sacrificed a couple of characters to get flexibility on the template,
however. and no tongue. I also have the commandline arguments to take care
of as well.
/Johan

------=_Part_13779_11337384.1129982235705--
 
J

James Edward Gray II

Well, I'm new on this quiz thingy... And I've already have some
results to
show off (which I hope isn't a spoiler):
_________________________
/The quick brown fox jumps\
\ over the lazy dog. /
-------------------------
\
` >(o)____,
(` =~~/
^~^~^~^~^~`---'^~^~^~^~^~^~^~^~^~^~

Looking great! Can't wait to see it.

James Edward Gray II
 
D

Dave Burt

#!/usr/bin/ruby
#
# Ducksay
#
# A response to Ruby Quiz of the Week #52 [ruby-talk:161834]
#
# It's a script that generates funny talking animals, like the one at
# http://www.cowsay.net/
#
# From the command line, use --help for usage info.
# Basically, you can give some parameters, and you have to give the duck's
# speech on STDIN.
#
# Create a new template by subclassing Duck.
#
# You can also use it from inside Ruby -- use the say method of Duck, etc.
#
# Author: Dave Burt <dave at burt.id.au>
#
# Created: 23 Oct 2005
#
# Last modified: 24 Oct 2005
#
# Fine print: Provided as is. Use at your own risk. Unauthorized copying is
# not disallowed. Credit's appreciated if you use my code. I'd
# appreciate seeing any modifications you make to it.

class String
def width
inject(0) {|w, line| [w, line.chomp.size].max }
end
def height
to_a.size
end
def top
to_a.first
end
def middle
to_a.values_at(1..-1)
end
def bottom
to_a.last
end
end

class Duck
def self.say(speech="quack?", *args)
balloon(speech) + body(*args)
end

def self.balloon(speech)
" _#{ '_' * speech.width }_\n" +
if speech.chomp =~ /\n/
"/ %-#{ speech.width }s \\\n" % speech.top.chomp +
speech.middle.map do |line|
"| %-#{ speech.width }s |\n" % line.chomp
end.join +
"\\ %-#{ speech.width }s /\n" % speech.bottom.chomp
else
"< #{ speech.chomp } >\n"
end +
" -#{ '-' * speech.width }-\n"
end

def self.body(thoughts='\\', eyes='cc', tongue=' ')
" #{thoughts}
#{thoughts}
_ ___
/ \\ / \\
\\. |: #{eyes}|
(.|:,---,
(.|: \\( |
(. y-'
\\ _ / #{tongue}
m m
"
end
end

class Cow < Duck
def self.body(thoughts='\\', eyes='oo', tongue=' ')
" #{thoughts} ^__^
#{thoughts} (#{eyes})\\_______
(__)\\ )\\/\\
#{tongue} ||----w |
|| ||
"
end
end

class DuckOnWater < Duck
def self.body(thoughts='\\', eyes='º', tongue='>')
" #{thoughts}
` #{tongue[0, 1]}(#{eyes[0, 1]})____,
(` =~~/
~^~^~^~^~`---'^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~
"
end
end

if $0 == __FILE__
if ARGV.include?("--help")
puts "usage: #$0 animal thoughts eyes tongue <speech\n"
puts "animals: Duck Cow DuckOnWater\n"
puts "e.g.: #$0 DuckOnWater o x ) </etc/fortune\n"
else
animal = Object.const_get(ARGV.shift) rescue Duck
puts animal.say(STDIN.read, *ARGV)
end
end
 
J

JB Eriksson

------=_Part_21731_7601876.1130082002140
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

my solution. not very OO at all, however. I just hope the formatting doesn'=
t
screw up on the animal template files...

/Johan B Eriksson

-----------
cowsay.rb:
-----------
# Rubyquiz #52, Ducksay
# ascii animals talking using ascii comic balloons.
#
#
# I couldn't get commandline arguments working properly on my machine, so I
skipped it.
# Instead, change the variable values to suit your needs.
#
# This solution does not automatically wordwrap the sayings. Do it manually=
 
D

Dave Burt

class Cow < Duck
Best line of Ruby code ever!

martin

It's my favourite line in the program, too. I was going to point it out in
my post, but I decided to paste instead of link.

Historical note: I originally wrote it the other way around, basing
everything on the Cow, but that's clearly not how it's meant to be.

BTW, another Cowsay reference:
http://www.nog.net/~tony/warez/cowsay.shtml

The above lists the cowsay perl software in its various forms, including the
CPAN module Acme::Cow.

Cheers,
Dave
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top