Skip to content

Cory Foy

Organizational agility through intersecting business and technology

Menu
  • FASTER Fridays
  • Mapping Mondays
  • Player Embed
  • Search Videos
  • User Dashboard
  • User Videos
  • Video Category
  • Video Form
  • Video Tag
Menu

Follow up: Binary Clock in Ruby in under 55 lines of code using Shoes

Posted on September 24, 2008 by Cory Foy

After some great comments in my last post and some judicious refactoring, I now present an entire binary clock in Ruby in less than 55 lines of code using Shoes:


Shoes.app {
@width = 40
@left = 10
@margin = 10
@top = 180
@offset = @width + @margin
@fill = red
@images = [[],[0],[1],[0,1],[2],[0,2],[1,2],[0,1,2],[3],[0,3]]

def gen_recs(num)
  return stack {
    fill @fill
    num.times do |i|
      rect :left => @left, :top => (@top - (i*@offset)), :width => @width
    end
    @left += @offset
  }
end

def turn_on_images(num, stack1, stack2)
  time = num.to_s.split(//)
  t1 = []
  t1 = @images[time[0].to_i] if time.length > 1
  t2 = @images[time[time.length-1].to_i]
  activate_images(stack1, t1)
  activate_images(stack2, t2)
end

def activate_images(stack, image_pos_array)
  stack.contents.each do |item|
    item.style :fill => @fill
  end
  image_pos_array.each do |pos|
    stack.contents[pos].style :fill => green
  end
end

@hours1 = gen_recs(2)
@hours2 = gen_recs(4)

@minutes1 = gen_recs(3)
@minutes2 = gen_recs(4)

@seconds1 = gen_recs(3)
@seconds2 = gen_recs(4)

every(1) do
  now = Time.new

  turn_on_images(now.sec, @seconds1, @seconds2)
  turn_on_images(now.min, @minutes1, @minutes2)
  turn_on_images(now.hour, @hours1, @hours2)
  end
}

2 thoughts on “Follow up: Binary Clock in Ruby in under 55 lines of code using Shoes”

  1. Joshua Choi says:
    September 25, 2008 at 11:59 am

    Sweet. You should put this on The Shoebox. If you’re too busy, I volunteer to put it up for you, because I already love it so much. :P

  2. Cory Foy says:
    September 25, 2008 at 8:16 pm

    Thanks! It has been added. Glad you like it!

Comments are closed.

© 2025 Cory Foy | Powered by Superbs Personal Blog theme