# Ruby programming challenges and riddles?

**URL:** <https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824>\
**Category:** ruby-talk\
**Created:** [24 May 2007 23:55 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824 "2007-05-24T23:55:05Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hakusa](https://avatars.discourse-cdn.com/v4/letter/h/6bbea6/32.png) [@Hakusa](https://rubytalk.org/u/Hakusa)\
**Post date:** [24 May 2007 23:55 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/1 "2007-05-24T23:55:05Z")

</div>

I recently fell in love with programming riddles and was told that  
they're the best way to learn a language. I agree with this statement  
because a good riddle challenges you to think outside the box, and  
that's a great skill to have when programming. Plus getting too cozy  
can lead to flawed and automated code because you forget WHY it worked  
before and didn't think about why it wouldn't work here.

So: Ruby programming riddles . . . I must admit I didn't start this  
discussion because I know any.

But just to get the ball rolling:  
Print 1...100 on the screen in 4 or less lines of code.

(And note that I will consider these by the most lines you can make  
out of your answer so writing it all on one line is useless.)

I know it's not the hardest, but it's just to get the ball rolling.  
I'm betting this isn't even the right place to make this thread.

---

<div class="post-metadata">

**Author:** ![Drew\_Olson](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Drew\_Olson](https://rubytalk.org/u/Drew_Olson)\
**Post date:** [25 May 2007 00:09 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/2 "2007-05-25T00:09:08Z")

</div>

Hakusa@gmail.com wrote:

> I recently fell in love with programming riddles and was told that  
> they're the best way to learn a language. I agree with this statement  
> because a good riddle challenges you to think outside the box, and  
> that's a great skill to have when programming. Plus getting too cozy  
> can lead to flawed and automated code because you forget WHY it worked  
> before and didn't think about why it wouldn't work here.
> 
> So: Ruby programming riddles . . . I must admit I didn't start this  
> discussion because I know any.
> 
> But just to get the ball rolling:  
> Print 1...100 on the screen in 4 or less lines of code.
> 
> (And note that I will consider these by the most lines you can make  
> out of your answer so writing it all on one line is useless.)
> 
> I know it's not the hardest, but it's just to get the ball rolling.  
> I'm betting this isn't even the right place to make this thread.

You may want to check out [http://www.rubyquiz.com](http://www.rubyquiz.com)

Also, in response to your question: (1...100).each{|i| puts i}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [25 May 2007 00:11 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/3 "2007-05-25T00:11:06Z")

</div>

puts "1...100"

hahaha

Actually:

puts (1..100).to\_a

Kirk Haines

> **···**
>
> On Fri, 25 May 2007, Hakusa@gmail.com wrote:
> 
> > But just to get the ball rolling:  
> > Print 1...100 on the screen in 4 or less lines of code.

---

<div class="post-metadata">

**Author:** ![Stefan\_Rusterholz](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stefan\_Rusterholz](https://rubytalk.org/u/Stefan_Rusterholz)\
**Post date:** [25 May 2007 00:14 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/4 "2007-05-25T00:14:17Z")

</div>

Hakusa@gmail.com wrote:

> But just to get the ball rolling:  
> Print 1...100 on the screen in 4 or less lines of code.

puts \*1..100 # will print each on a new line

And rubyquiz.com - as Drew already pointed out 🙂

Regards  
Stefan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Fred\_Phillips](https://avatars.discourse-cdn.com/v4/letter/f/278dde/32.png) [@Fred\_Phillips](https://rubytalk.org/u/Fred_Phillips)\
**Post date:** [25 May 2007 12:23 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/5 "2007-05-25T12:23:15Z")

</div>

Maybe you should try [http://projecteuler.net](http://projecteuler.net)

On 25/05/07, [Hakusa@gmail.com](mailto:Hakusa@gmail.com) wrote:I recently fell in love with programming  
riddles and was told that  
they're the best way to learn a language. I agree with this statement  
because a good riddle challenges you to think outside the box, and  
that's a great skill to have when programming. Plus getting too cozy  
can lead to flawed and automated code because you forget WHY it worked  
before and didn't think about why it wouldn't work here.

So: Ruby programming riddles . . . I must admit I didn't start this  
discussion because I know any.

But just to get the ball rolling:  
Print 1...100 on the screen in 4 or less lines of code.

(And note that I will consider these by the most lines you can make  
out of your answer so writing it all on one line is useless.)

I know it's not the hardest, but it's just to get the ball rolling.  
I'm betting this isn't even the right place to make this thread.

---

<div class="post-metadata">

**Author:** ![Drew\_Olson](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Drew\_Olson](https://rubytalk.org/u/Drew_Olson)\
**Post date:** [25 May 2007 00:17 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/6 "2007-05-25T00:17:45Z")

</div>

Stefan Rusterholz wrote:

> puts \*1..100 # will print each on a new line

Cool! How does this work? What's going on behind the scenes?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [25 May 2007 00:28 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/7 "2007-05-25T00:28:09Z")

</div>

Careful with .to\_a, they're getting rid of it. Break the habit while you can!

To make this a little longer than one line, and to avoid repetition of ideas....  
100.downto(1) do |yourmom|  
&nbsp;&nbsp;puts yourmom  
end

Nothing quite like a little bit of immaturity.  
heh. do yourmom.

I like these. They're easier than RubyQuiz, so I can do them, and they don't take a lot of time. Which I am lacking of right now.

immaturity ftw  
-------------------------------------------------------|  
~ Ari  
crap my sig won't fit

> **···**
>
> On May 24, 2007, at 8:11 PM, khaines@enigo.com wrote:
> 
> > On Fri, 25 May 2007, Hakusa@gmail.com wrote:
> > 
> > > But just to get the ball rolling:  
> > > Print 1...100 on the screen in 4 or less lines of code.
> > 
> > puts "1...100"
> > 
> > hahaha
> > 
> > Actually:
> > 
> > puts (1..100).to\_a

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [25 May 2007 00:34 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/8 "2007-05-25T00:34:09Z")

</div>

Someday.

No point in not using a construct of the current language just because is may not be there at some point in the far-off.

Kirk Haines

> **···**
>
> On Fri, 25 May 2007, Ari Brown wrote:
> 
> > > puts (1..100).to\_a
> > 
> > Careful with .to\_a, they're getting rid of it. Break the habit while you can!

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://rubytalk.org/u/Chris_Carter)\
**Post date:** [25 May 2007 00:40 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/9 "2007-05-25T00:40:18Z")

</div>

I was under the impression they are removing Object#to\_a, not Enumerable#to\_a...

> **···**
>
> On 5/24/07, Ari Brown \<ari@aribrown.com\> wrote:
> 
> > On May 24, 2007, at 8:11 PM, khaines@enigo.com wrote:
> > 
> > \> On Fri, 25 May 2007, Hakusa@gmail.com wrote:  
> > \>  
> > \>\> But just to get the ball rolling:  
> > \>\> Print 1...100 on the screen in 4 or less lines of code.  
> > \>  
> > \> puts "1...100"  
> > \>  
> > \> hahaha  
> > \>  
> > \> Actually:  
> > \>  
> > \> puts (1..100).to\_a
> > 
> > Careful with .to\_a, they're getting rid of it. Break the habit while  
> > you can!
> > 
> > To make this a little longer than one line, and to avoid repetition  
> > of ideas....  
> > 100.downto(1) do |yourmom|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts yourmom  
> > end
> > 
> > Nothing quite like a little bit of immaturity.  
> > heh. do yourmom.
> > 
> > I like these. They're easier than RubyQuiz, so I can do them, and  
> > they don't take a lot of time. Which I am lacking of right now.
> > 
> > immaturity ftw  
> > -------------------------------------------------------|  
> > ~ Ari  
> > crap my sig won't fit
> 
> --  
> Chris Carter  
> concentrationstudios.com  
> brynmawrcs.com

---

<div class="post-metadata">

**Author:** ![Stefan\_Rusterholz](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stefan\_Rusterholz](https://rubytalk.org/u/Stefan_Rusterholz)\
**Post date:** [25 May 2007 00:42 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/10 "2007-05-25T00:42:13Z")

</div>

Drew Olson wrote:

> Stefan Rusterholz wrote:
> 
> > puts \*1..100 # will print each on a new line
> 
> Cool! How does this work? What's going on behind the scenes?

\* is the splash operator, it expands an array to a list, if the object  
is not an array it will call .to\_a on it  
so the above is virtually the same as doing:  
puts 1, 2, 3, ..., 100

Regards  
Stefan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Stefan\_Rusterholz](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stefan\_Rusterholz](https://rubytalk.org/u/Stefan_Rusterholz)\
**Post date:** [25 May 2007 00:45 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/11 "2007-05-25T00:45:18Z")

</div>

Chris Carter wrote:

> > \> hahaha  
> > 100.downto(1) do |yourmom|  
> > -------------------------------------------------------|  
> > ~ Ari  
> > crap my sig won't fit
> 
> I was under the impression they are removing Object#to\_a, not  
> Enumerable#to\_a...

As far as I know your impression is correct. IRB backs that up:  
irb(main):001:0\> 1.to\_a  
(irb):1: warning: default `to\_a' will be obsolete  
=\> [1]  
irb(main):002:0\> (1..2).to\_a  
=\> [1, 2]

Regards  
Stefan

> **···**
>
> > On 5/24/07, Ari Brown \<ari@aribrown.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Hakusa](https://avatars.discourse-cdn.com/v4/letter/h/6bbea6/32.png) [@Hakusa](https://rubytalk.org/u/Hakusa)\
**Post date:** [25 May 2007 00:55 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/12 "2007-05-25T00:55:05Z")

</div>

> [http://www.rubyquiz.com](http://www.rubyquiz.com)

Hm. Everything I've looked at on this site is time consuming, geared  
towards experience programmers, and most of all: not making me learn  
about Ruby specific things. My favorite riddles are the kind that can  
be written and solved quickly if you know your stuff, and can teach  
you something by other's solutions if you don't. Any site that does  
that? (I'm not looking for difficulty, I'm looking for outside-the-  
box.)

---

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [25 May 2007 11:39 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/13 "2007-05-25T11:39:07Z")

</div>

\<snip\>

> I was under the impression they are removing Object#to\_a, not Enumerable#to\_a...

You're probably right. I was criticized for using to\_a in that context, so you're probably right.  
What would be an example of Object#to\_a?

---------------------------------------------------------------|  
~Ari  
"I don't suffer from insanity. I enjoy every minute of it" --1337est man alive

> **···**
>
> On May 24, 2007, at 8:40 PM, Chris Carter wrote:

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [25 May 2007 04:25 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/14 "2007-05-25T04:25:48Z")

</div>

I'm obviously biased, since I run the Ruby Quiz, but...

> > [http://www.rubyquiz.com](http://www.rubyquiz.com)
> 
> Hm. Everything I've looked at on this site is time consuming,

Quizzes run the gamut. We've had too hard problems and we've had some very easy ones. See the recent Checking Credit Cards or the older pp Pascal problem for some pretty easy stuff. In general, the quizzes with a lot of solutions are easier.

More importantly, a large percentage of the quizzes are user submitted. If you want easier problems, please send them in:

&nbsp;&nbsp;&nbsp;suggestion@rubyquiz.com

> geared towards experience programmers,

I hope we've appealed to everyone at some point, but maybe that's just wishful thinking.

> and most of all: not making me learn about Ruby specific things.

I really hope that's not true. This is the reason I built the Ruby Quiz: to teach myself and others Ruby. Hopefully we've managed to do at least a little of that.

James Edward Gray II

> **···**
>
> On May 24, 2007, at 7:55 PM, Hakusa@gmail.com wrote:

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [25 May 2007 11:42 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/15 "2007-05-25T11:42:22Z")

</div>

I hate reading code. I mean absolutely \*hate\* with a passion. I hate  
reading my own code, even. I also hate when people are smarter or  
more clever than me. I've almost always been able to pick up the  
subject material from a textbook, and jump write into the application  
of it. In college, I never went to recitations, and rarely even  
lectures.

With that being said, Ruby quiz and ruby-talk have become very  
powerful humbling and mind-expanding learning tools for me -- in not  
only Ruby, but programming concepts in general. Better than taking a  
class, by far, because it continuously gives you the chance to bite  
off more than you can chew, which is, IMO, the key to good learning in  
or outside of whatever you see as a box 🙂

Read some more code! Download stuff from rubyforge. Read through  
some of the quiz solutions. I think you'll find just what you're  
looking for. In fact, if I was going to teach an Intro to Ruby class,  
James' ruby quiz book would be required reading material.

> **···**
>
> On 5/24/07, Hakusa@gmail.com \<Hakusa@gmail.com\> wrote:
> 
> > \> [http://www.rubyquiz.com](http://www.rubyquiz.com)
> > 
> > Hm. Everything I've looked at on this site is time consuming, geared  
> > towards experience programmers, and most of all: not making me learn  
> > about Ruby specific things. My favorite riddles are the kind that can  
> > be written and solved quickly if you know your stuff, and can teach  
> > you something by other's solutions if you don't. Any site that does  
> > that? (I'm not looking for difficulty, I'm looking for outside-the-  
> > box.)

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://rubytalk.org/u/Chris_Carter)\
**Post date:** [25 May 2007 11:57 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/16 "2007-05-25T11:57:14Z")

</div>

> \<snip\>  
> \>  
> \> I was under the impression they are removing Object#to\_a, not  
> \> Enumerable#to\_a...
> 
> You're probably right. I was criticized for using to\_a in that  
> context, so you're probably right.  
> What would be an example of Object#to\_a?
> 
> ---------------------------------------------------------------|  
> ~Ari  
> "I don't suffer from insanity. I enjoy every minute of it" --1337est  
> man alive
> 
> > Object.new.to\_a

(irb):1: warning: default `to\_a' will be obsolete  
=\> [#\<Object:0x15b8d70\>]

vs.

> > (1..10).to\_a # Ranges mixin Enumerable for to\_a

=\> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

> **···**
>
> On 5/25/07, Ari Brown \<ari@aribrown.com\> wrote:
> 
> > On May 24, 2007, at 8:40 PM, Chris Carter wrote:
> 
> --  
> Chris Carter  
> concentrationstudios.com  
> brynmawrcs.com

---

<div class="post-metadata">

**Author:** ![Hakusa](https://avatars.discourse-cdn.com/v4/letter/h/6bbea6/32.png) [@Hakusa](https://rubytalk.org/u/Hakusa)\
**Post date:** [25 May 2007 12:50 UTC](https://rubytalk.org/t/ruby-programming-challenges-and-riddles/37824/17 "2007-05-25T12:50:06Z")

</div>

> I'm obviously biased, since I run the Ruby Quiz, but...
> 
> \>\>[http://www.rubyquiz.com](http://www.rubyquiz.com)
> 
> \> Hm. Everything I've looked at on this site is time consuming,
> 
> Quizzes run the gamut. We've had too hard problems and we've had  
> some very easy ones. See the recent Checking Credit Cards or the  
> older pp Pascal problem for some pretty easy stuff. In general, the  
> quizzes with a lot of solutions are easier.
> 
> More importantly, a large percentage of the quizzes are user  
> submitted. If you want easier problems, please send them in:
> 
> &nbsp;&nbsp;&nbsp;suggest...@rubyquiz.com
> 
> \> geared towards experience programmers,
> 
> I hope we've appealed to everyone at some point, but maybe that's  
> just wishful thinking.
> 
> \> and most of all: not making me learn about Ruby specific things.
> 
> I really hope that's not true. This is the reason I built the Ruby  
> Quiz: to teach myself and others Ruby. Hopefully we've managed to  
> do at least a little of that.
> 
> James Edward Gray II

OK, then it's by luck of the draw of what I read, and I've read the  
wrong things. But I learned more about Ruby by reading people's  
answers to my printing quiz then I did reading through the card game  
quiz--which to be honest confused me greatly. I think the best  
riddles, as I've said, are just a few lines of code that exploit a  
languages quirks. Reading code in general will teach you them and is a  
practice I should temp myself to keep up with, but I'd have to hunt  
and peck through the source code to find it. Short riddles have little  
too find so the quirks are more apparent.

But I think I'll browse your selection anyway. It's not exactly what  
I'd prefer in challenges, but it probably is a much better resource  
than I'm giving it credit for.

> Better than taking a  
> class, by far, because it continuously gives you the chance to bite  
> off more than you can chew, which is, IMO, the key to good learning in  
> or outside of whatever you see as a box 🙂

Hmm. Very good point. I do hate it when my school teachers protect me  
from knowing what's in the next lesson or how the (subject) relates to  
(corollary). I guess attempting a harder one is better that protecting  
myself by saying it's hard.

> **···**
>
> On May 25, 12:25 am, James Edward Gray II \<j...@grayproductions.net\> wrote:
> 
> > On May 24, 2007, at 7:55 PM, Hak...@gmail.com wrote:
