[QUIZ] Dreaming of a Ruby Christmas (#187)

Discussion in 'Ruby' started by Matthew Moss, Dec 19, 2008.

  1. Matthew Moss

    Matthew Moss Guest

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

    The three rules of Ruby Quiz 2:

    ..... no rules this week! .....

    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.

    Note: I will be out and away for approximately two weeks, so don't
    expect to see this quiz summarized and a new quiz available until
    either Jan 2nd or 9th.

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

    ## Dreaming of a Ruby Christmas (#187)

    It's six more days of Advent, and then... Christmas! Your task is to
    create a Ruby script that celebrates Christmas. Create a virtual,
    ASCII Christmas tree completely with blinking lights. Or a countdown
    calendar 'til December 25th. Or a script that generates the lyrics to
    _The Twelve Days of Christmas_. Or whatever you like: some Christmas
    creativity.

    And... Merry Christmas and a Happy New Year!!!

    ...

    P.S. Yes, I'm politically-incorrect. That said, if you'd rather
    celebrate Hanukkah, Kwanzaa, New Year's or another holiday using Ruby
    script, please do!
     
    Matthew Moss, Dec 19, 2008
    #1
    1. Advertising

  2. Matthew Moss wrote:
    >
    > ## Dreaming of a Ruby Christmas (#187)
    >
    > It's six more days of Advent, and then... Christmas! Your task is to
    > create a Ruby script that celebrates Christmas. Create a virtual, ASCII
    > Christmas tree completely with blinking lights. Or a countdown calendar
    > 'til December 25th. Or a script that generates the lyrics to _The Twelve
    > Days of Christmas_. Or whatever you like: some Christmas creativity.
    >
    > And... Merry Christmas and a Happy New Year!!!
    >


    Whenever I wrap Matzmas presents the wrapping turns out extremely
    lumpy, crinkly, and stuck with gobs of tape everywhere, and the
    contents are always disappointing. This script is no exception.

    Take the stuff between the '----- cut here -----' lines and save it in
    a file, e.g. 'matzmas.rb'. Feed it to ruby, e.g., 'ruby matzmas.rb'.

    Please don't try to learn anything from this code!

    - Glen

    ----- cut here -----

    ### M E R R Y M A T Z M A S ###

    "/|"\
    "/wl)e";eval(\
    "A | A"\
    "A | A"\
    \
    "InHhL"\
    ",Z,%w{-"\
    ",Y&/LH,L*"\
    "cCnd,Hh&/d,"\
    "YCB/Hh,Y&YHh,"\
    "L&xHh,L*v*nHh,*"\
    "&ghHh,n&nHh,H*nHh"\
    ",*q*v*nHh,Hw*qv*Hh}"\
    "Iq&n*L,Z,[,n&qK,n&qK,"\
    "@TwC,LH/&ng,gLBbL@K,@Th"\
    "/**,YC/k*d,Hh/*QdL@K,@FCB"\
    "/,cQqq&ng,=/CcL@K,@F&v*,RBb"\
    "y,g*mL!@K,@S&x,mQ/LhQqL,dBm=&"\
    "ng@K,@S*v*n,dBckL,Q-Hy=&ng@K,@E"\
    "&ghH,d&/L,Q-gqCbb&ng@K,@N&n*,/Qng"\
    "*L,LH*==&ng@K,@T*n,H/yL,Q-cQHch&ng@"\
    "K,@Eq*v*n,&CL,=&=&ng@K,@Tw*qv*,mCnk*y"\
    "L,=QHch&ng@,]IDAJJA2)J*Qch,dC,U&UI=/&nH"\
    ",@\nOn,Hh*,\#{nHhL[&]},dQy,CY,MQHzmQL,my,"\
    "H/B*,qCv*,gQv*,HC,m*:\n@I&JdCwnHCD2),dC,UjU"\
    "I=/&nH,@,,,\#{q&n*L[j]}\n@I*ndI=/&nHDD&,ZZ,A)"\
    "?,@,,"\
    ",G@,:"\
    ",@,,,"\
    "Gnd,Q"\
    "@)I=/"\
    "&nH,@,n*w,v*/L&Cn,CY,RBby!\n@I*nd"\
    "I".tr(' ID/VHLYGBqCA&|*UQJ=Z@K,',
    '+;(r/tsfAulo1i8e|a.p=", '))

    ----- cut here -----
     
    Glen F. Pankow, Dec 20, 2008
    #2
    1. Advertising

  3. I feel compelled to express my sheer amazement. Thank you, Glen, you
    have made my holiday!

    - Josh

    On Dec 20, 2008, at 2:33 AM, Glen F. Pankow wrote:

    > Matthew Moss wrote:
    >> ## Dreaming of a Ruby Christmas (#187)
    >> It's six more days of Advent, and then... Christmas! Your task is
    >> to create a Ruby script that celebrates Christmas. Create a
    >> virtual, ASCII Christmas tree completely with blinking lights. Or a
    >> countdown calendar 'til December 25th. Or a script that generates
    >> the lyrics to _The Twelve Days of Christmas_. Or whatever you like:
    >> some Christmas creativity.
    >> And... Merry Christmas and a Happy New Year!!!

    >
    > Whenever I wrap Matzmas presents the wrapping turns out extremely
    > lumpy, crinkly, and stuck with gobs of tape everywhere, and the
    > contents are always disappointing. This script is no exception.
    >
    > Take the stuff between the '----- cut here -----' lines and save it in
    > a file, e.g. 'matzmas.rb'. Feed it to ruby, e.g., 'ruby matzmas.rb'.
    >
    > Please don't try to learn anything from this code!
    >
    > - Glen
    >
    > ----- cut here -----
    >
    > ### M E R R Y M A T Z M A S ###
    >
    > "/|"\
    > "/wl)e";eval(\
    > "A | A"\
    > "A | A"\
    > \
    > "InHhL"\
    > ",Z,%w{-"\
    > ",Y&/LH,L*"\
    > "cCnd,Hh&/d,"\
    > "YCB/Hh,Y&YHh,"\
    > "L&xHh,L*v*nHh,*"\
    > "&ghHh,n&nHh,H*nHh"\
    > ",*q*v*nHh,Hw*qv*Hh}"\
    > "Iq&n*L,Z,[,n&qK,n&qK,"\
    > "@TwC,LH/&ng,gLBbL@K,@Th"\
    > "/**,YC/k*d,Hh/*QdL@K,@FCB"\
    > "/,cQqq&ng,=/CcL@K,@F&v*,RBb"\
    > "y,g*mL!@K,@S&x,mQ/LhQqL,dBm=&"\
    > "ng@K,@S*v*n,dBckL,Q-Hy=&ng@K,@E"\
    > "&ghH,d&/L,Q-gqCbb&ng@K,@N&n*,/Qng"\
    > "*L,LH*==&ng@K,@T*n,H/yL,Q-cQHch&ng@"\
    > "K,@Eq*v*n,&CL,=&=&ng@K,@Tw*qv*,mCnk*y"\
    > "L,=QHch&ng@,]IDAJJA2)J*Qch,dC,U&UI=/&nH"\
    > ",@\nOn,Hh*,\#{nHhL[&]},dQy,CY,MQHzmQL,my,"\
    > "H/B*,qCv*,gQv*,HC,m*:\n@I&JdCwnHCD2),dC,UjU"\
    > "I=/&nH,@,,,\#{q&n*L[j]}\n@I*ndI=/&nHDD&,ZZ,A)"\
    > "?,@,,"\
    > ",G@,:"\
    > ",@,,,"\
    > "Gnd,Q"\
    > "@)I=/"\
    > "&nH,@,n*w,v*/L&Cn,CY,RBby!\n@I*nd"\
    > "I".tr(' ID/VHLYGBqCA&|*UQJ=Z@K,',
    > '+;(r/tsfAulo1i8e|a.p=", '))
    >
    > ----- cut here -----
    >
     
    Joshua Ballanco, Dec 20, 2008
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    I agree, that's v cool :)

    On Sat, Dec 20, 2008 at 6:54 PM, Joshua Ballanco <> wrote:

    > I feel compelled to express my sheer amazement. Thank you, Glen, you have
    > made my holiday!
    >
    > - Josh
    >
    >
    > On Dec 20, 2008, at 2:33 AM, Glen F. Pankow wrote:
    >
    > Matthew Moss wrote:
    >>
    >>> ## Dreaming of a Ruby Christmas (#187)
    >>> It's six more days of Advent, and then... Christmas! Your task is to
    >>> create a Ruby script that celebrates Christmas. Create a virtual, ASCII
    >>> Christmas tree completely with blinking lights. Or a countdown calendar 'til
    >>> December 25th. Or a script that generates the lyrics to _The Twelve Days of
    >>> Christmas_. Or whatever you like: some Christmas creativity.
    >>> And... Merry Christmas and a Happy New Year!!!
    >>>

    >>
    >> Whenever I wrap Matzmas presents the wrapping turns out extremely
    >> lumpy, crinkly, and stuck with gobs of tape everywhere, and the
    >> contents are always disappointing. This script is no exception.
    >>
    >> Take the stuff between the '----- cut here -----' lines and save it in
    >> a file, e.g. 'matzmas.rb'. Feed it to ruby, e.g., 'ruby matzmas.rb'.
    >>
    >> Please don't try to learn anything from this code!
    >>
    >> - Glen
    >>
    >> ----- cut here -----
    >>
    >> ### M E R R Y M A T Z M A S ###
    >>
    >> "/|"\
    >> "/wl)e";eval(\
    >> "A | A"\
    >> "A | A"\
    >> \
    >> "InHhL"\
    >> ",Z,%w{-"\
    >> ",Y&/LH,L*"\
    >> "cCnd,Hh&/d,"\
    >> "YCB/Hh,Y&YHh,"\
    >> "L&xHh,L*v*nHh,*"\
    >> "&ghHh,n&nHh,H*nHh"\
    >> ",*q*v*nHh,Hw*qv*Hh}"\
    >> "Iq&n*L,Z,[,n&qK,n&qK,"\
    >> "@TwC,LH/&ng,gLBbL@K,@Th"\
    >> "/**,YC/k*d,Hh/*QdL@K,@FCB"\
    >> "/,cQqq&ng,=/CcL@K,@F&v*,RBb"\
    >> "y,g*mL!@K,@S&x,mQ/LhQqL,dBm=&"\
    >> "ng@K,@S*v*n,dBckL,Q-Hy=&ng@K,@E"\
    >> "&ghH,d&/L,Q-gqCbb&ng@K,@N&n*,/Qng"\
    >> "*L,LH*==&ng@K,@T*n,H/yL,Q-cQHch&ng@"\
    >> "K,@Eq*v*n,&CL,=&=&ng@K,@Tw*qv*,mCnk*y"\
    >> "L,=QHch&ng@,]IDAJJA2)J*Qch,dC,U&UI=/&nH"\
    >> ",@\nOn,Hh*,\#{nHhL[&]},dQy,CY,MQHzmQL,my,"\
    >> "H/B*,qCv*,gQv*,HC,m*:\n@I&JdCwnHCD2),dC,UjU"\
    >> "I=/&nH,@,,,\#{q&n*L[j]}\n@I*ndI=/&nHDD&,ZZ,A)"\
    >> "?,@,,"\
    >> ",G@,:"\
    >> ",@,,,"\
    >> "Gnd,Q"\
    >> "@)I=/"\
    >> "&nH,@,n*w,v*/L&Cn,CY,RBby!\n@I*nd"\
    >> "I".tr(' ID/VHLYGBqCA&|*UQJ=Z@K,',
    >> '+;(r/tsfAulo1i8e|a.p=", '))
    >>
    >> ----- cut here -----
    >>
    >>

    >
    >
     
    steven shingler, Dec 20, 2008
    #4
  5. Matthew Moss

    Guest

    On Fri, Dec 19, 2008 at 3:08 PM, Matthew Moss <> wrote:
    > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    > ## Dreaming of a Ruby Christmas (#187)
    >
    > It's six more days of Advent, and then... Christmas! Your task is to create
    > a Ruby script that celebrates Christmas. Create a virtual, ASCII Christmas
    > tree completely with blinking lights. Or a countdown calendar 'til December
    > 25th. Or a script that generates the lyrics to _The Twelve Days of
    > Christmas_. Or whatever you like: some Christmas creativity.
    >
    > And... Merry Christmas and a Happy New Year!!!


    I doubt I will find time to do more with this, so Merry Christmas:

    > cat RbMasTry.rb

    #!/usr/bin/env jruby
    require 'java'
    require 'mathn'

    JFrame = javax.swing.JFrame
    JPanel = javax.swing.JPanel
    Color = java.awt.Color

    frame = JFrame.new("Merry JRuby Christmas")
    frame.default_close_operation = JFrame::EXIT_ON_CLOSE
    frame.set_size(300, 400)

    frame.show

    class RbMasTry < JPanel
    def paintComponent(graphics)
    super(graphics)

    [
    [400, 150, 50, 125, 50, 400, 50, 50, 50],
    [300, 150, 200, 50, 200, 350, 0, 200, 0],
    [200, 150, 100, 75, 150, 250],
    [100, 150, 50, 100, 100, 150],
    ].each do |a|
    graphics.set_color(Color.new(*a[6..8])) if a[6]

    a[0].times do
    graphics.draw_line(a[1], a[2], a[3] + rand(a[4]), a[5])
    end
    end

    graphics.set_color(Color.new(255, 255, 0))

    360.times do
    graphics.draw_line(
    150, 50,
    150 + rand(25) * Math.sin(rand(2 * Math::pI)),
    50 + rand(25) * Math.cos(rand(2 * Math::pI))
    )
    end

    graphics.set_color(Color.new(255, 255, 255))

    rand(90).times do
    x, y = rand(300), rand(400)
    rand(180).times do
    graphics.draw_line(
    x, y,
    x + rand(5) * Math.sin(rand(2 * Math::pI)),
    y + rand(5) * Math.cos(rand(2 * Math::pI))
    )
    end
    end
    end
    end

    tree = RbMasTry.new
    frame.add(tree)
    tree.repaint
    tree.revalidate
     
    , Dec 22, 2008
    #5
  6. [Note: parts of this message were removed to make it a legal post.]

    Hi, I was wondering about this expression and its siblings:

    > Math.sin(rand(2 * Math::pI))


    The documentation
    <http://www.ruby-doc.org/core/classes/Kernel.html#M005977>states that
    rand(num) will pick a random integer between 0 and num.abs.to_i
    (unless this is different in JRuby). Therefore, the only values that can
    come out of this expression are sin(0), sin(1), ... sin(6) which are 0,
    841, .909, .141, -.757, -.960, and -.279 (of course these are different for
    cosine). Is it just the range -1<y<1? Would rand(2)-1 suffice? What is
    the significance of these numbers and doing rand(2pi) instead of rand(6)?

    I did enjoy seeing a JRuby solution, especially a GUI-based one. Kudos.

    Thanks,
    Dan


    On Mon, Dec 22, 2008 at 12:44 PM, <> wrote:

    > On Fri, Dec 19, 2008 at 3:08 PM, Matthew Moss <> wrote:
    > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    > > ## Dreaming of a Ruby Christmas (#187)
    > >
    > > It's six more days of Advent, and then... Christmas! Your task is to

    > create
    > > a Ruby script that celebrates Christmas. Create a virtual, ASCII

    > Christmas
    > > tree completely with blinking lights. Or a countdown calendar 'til

    > December
    > > 25th. Or a script that generates the lyrics to _The Twelve Days of
    > > Christmas_. Or whatever you like: some Christmas creativity.
    > >
    > > And... Merry Christmas and a Happy New Year!!!

    >
    > I doubt I will find time to do more with this, so Merry Christmas:
    >
    > > cat RbMasTry.rb

    > #!/usr/bin/env jruby
    > require 'java'
    > require 'mathn'
    >
    > JFrame = javax.swing.JFrame
    > JPanel = javax.swing.JPanel
    > Color = java.awt.Color
    >
    > frame = JFrame.new("Merry JRuby Christmas")
    > frame.default_close_operation = JFrame::EXIT_ON_CLOSE
    > frame.set_size(300, 400)
    >
    > frame.show
    >
    > class RbMasTry < JPanel
    > def paintComponent(graphics)
    > super(graphics)
    >
    > [
    > [400, 150, 50, 125, 50, 400, 50, 50, 50],
    > [300, 150, 200, 50, 200, 350, 0, 200, 0],
    > [200, 150, 100, 75, 150, 250],
    > [100, 150, 50, 100, 100, 150],
    > ].each do |a|
    > graphics.set_color(Color.new(*a[6..8])) if a[6]
    >
    > a[0].times do
    > graphics.draw_line(a[1], a[2], a[3] + rand(a[4]), a[5])
    > end
    > end
    >
    > graphics.set_color(Color.new(255, 255, 0))
    >
    > 360.times do
    > graphics.draw_line(
    > 150, 50,
    > 150 + rand(25) * Math.sin(rand(2 * Math::pI)),
    > 50 + rand(25) * Math.cos(rand(2 * Math::pI))
    > )
    > end
    >
    > graphics.set_color(Color.new(255, 255, 255))
    >
    > rand(90).times do
    > x, y = rand(300), rand(400)
    > rand(180).times do
    > graphics.draw_line(
    > x, y,
    > x + rand(5) * Math.sin(rand(2 * Math::pI)),
    > y + rand(5) * Math.cos(rand(2 * Math::pI))
    > )
    > end
    > end
    > end
    > end
    >
    > tree = RbMasTry.new
    > frame.add(tree)
    > tree.repaint
    > tree.revalidate
    >
    >
     
    Daniel Finnie, Dec 23, 2008
    #6
  7. Matthew Moss

    Guest

    On Mon, Dec 22, 2008 at 7:27 PM, Daniel Finnie <> wrote:
    > Hi, I was wondering about this expression and its siblings:
    >
    >> Math.sin(rand(2 * Math::pI))

    >
    > The documentation
    > <http://www.ruby-doc.org/core/classes/Kernel.html#M005977>states that
    > rand(num) will pick a random integer between 0 and num.abs.to_i
    > (unless this is different in JRuby). Therefore, the only values that can
    > come out of this expression are sin(0), sin(1), ... sin(6) which are 0,
    > .841, .909, .141, -.757, -.960, and -.279 (of course these are different for
    > cosine). Is it just the range -1<y<1? Would rand(2)-1 suffice? What is
    > the significance of these numbers and doing rand(2pi) instead of rand(6)?


    No significance other than trying to finish before 1:00. :) Maybe
    I was thinking: sin(rand * 2 * PI? But, yeah rand * 2 - 1 would have
    sufficed there.

    Thanks.
     
    , Dec 23, 2008
    #7
  8. Matthew Moss

    Guest

    On Mon, Dec 22, 2008 at 12:44 PM, <> wrote:
    > On Fri, Dec 19, 2008 at 3:08 PM, Matthew Moss <> wrote:
    >> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    >> ## Dreaming of a Ruby Christmas (#187)

    >
    > I doubt I will find time to do more with this, so Merry Christmas:


    I did make a few modifications:
    * removed unnecessary require
    * removed unnecessary trig functions
    * changed background color to black
    * "animated" the star :)
    * removed the snow :(
    I never did get around to:
    * decorating the tree
    * putting presents under the tree

    http://github.com/fjc/rubyquiz/tree/master/187

    #!/usr/bin/env jruby
    require 'java'

    JFrame = javax.swing.JFrame
    JPanel = javax.swing.JPanel
    Color = java.awt.Color

    frame = JFrame.new("Merry JRuby Christmas")
    frame.default_close_operation = JFrame::EXIT_ON_CLOSE
    frame.set_size(300, 400)

    frame.show

    class RbMasTry < JPanel
    def paintComponent(graphics)
    super(graphics)

    srand(@tree_seed ||= srand)
    [
    [400, 150, 50, 125, 50, 400, 50, 50, 50],
    [300, 150, 200, 50, 200, 350, 0, 200, 0],
    [200, 150, 100, 75, 150, 250],
    [100, 150, 50, 100, 100, 150],
    ].each do |a|
    graphics.set_color(Color.new(*a[6..8])) if a[6]

    a[0].times do
    graphics.draw_line(a[1], a[2], a[3] + rand(a[4]), a[5])
    end
    end
    srand

    graphics.set_color(Color.new(255, 255, 0))

    360.times do
    graphics.draw_line(
    150, 50,
    150 + rand(25) * (rand * 2 - 1),
    50 + rand(25) * (rand * 2 - 1)
    )
    end
    end
    end

    tree = RbMasTry.new
    tree.background = Color.new(0, 0, 0)
    frame.add(tree)

    loop do
    tree.repaint
    tree.revalidate
    sleep 0.1
    end
     
    , Dec 24, 2008
    #8
  9. Matthew Moss

    Robert Dober Guest

    > P.S. Yes, I'm politically-incorrect. That said, if you'd rather celebrate
    > Hanukkah, Kwanzaa, New Year's or another holiday using Ruby script, please
    > do!

    So I leave it to the user which message to display ;)
    I personally would chose

    Health, Love and Peace for All on this World

    But if you run my solution with this text you need some patience....

    http://pastie.org/346680

    Cheers
    Robert
     
    Robert Dober, Dec 25, 2008
    #9
  10. Matthew Moss

    Robert Dober Guest

    On Thu, Dec 25, 2008 at 5:35 PM, Robert Dober <> wrote:
    >> P.S. Yes, I'm politically-incorrect. That said, if you'd rather celebrate
    >> Hanukkah, Kwanzaa, New Year's or another holiday using Ruby script, please
    >> do!

    > So I leave it to the user which message to display ;)
    > I personally would chose
    >
    > Health, Love and Peace for All on this World
    >
    > But if you run my solution with this text you need some patience....

    Well as I had to fix a bug, I made the random choice converging, now
    we can have peace in less time :)

    http://pastie.org/346680

    R.
     
    Robert Dober, Dec 25, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Eternal Squire

    dreaming in Python

    The Eternal Squire, Oct 29, 2005, in forum: Python
    Replies:
    3
    Views:
    414
    =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=
    Oct 30, 2005
  2. Paul McGuire

    "Dreaming in Code"

    Paul McGuire, Apr 23, 2008, in forum: Python
    Replies:
    2
    Views:
    267
  3. Hr Ro
    Replies:
    0
    Views:
    120
    Hr Ro
    Aug 23, 2009
  4. Mitch Williams
    Replies:
    2
    Views:
    180
    Mitch Williams
    Feb 18, 2010
  5. Radhika
    Replies:
    0
    Views:
    252
    Radhika
    Sep 13, 2004
Loading...

Share This Page