fallenrogue.com

Fun little ruby time...

So, Scott Hanselman posted a cool little brain teaser over at his site. It wasn’t a teaser so much as an exercise in scripting. I figured when he asked for samples in other scripting languages that we’d see a lot of extremely terse multidimensional array looping examples in OCaml and F# and even Python, Perl and Ruby. Well, no one (at that time) had tossed their Ruby hat in, so I though, oh what the hell. But rather than show off “fewest lines of code” (FTW!) I decided to approach the task like I would any application. I thought it’d be more fun that way and give me a few minutes in my precious love… ruby. <3

Check out Scott’s setup here and read the official rules here and then feel free to tear my example to shreds OR better still, provide us with links to your own. (you can try to reply in comments but I can’t promise it will render very well. :) Without further adieu…


class Team
  attr_accessor :name
  def initialize(name)
    @name=name
  end
end

class Game
  attr_accessor :home_team, :away_team
  def initialize(home,away)
    @home_team = home
    @away_team = away
  end
end

class Schedule
  attr_accessor :games
  def initialize()
    @games=[]
  end

  def scheduled_to_play?(team1, team2)
    @games.each do |game|
      if (game.home_team == team1 || game.away_team == team1) 
            && (game.home_team == team2 || game.away_team == team2) 
        return true
      end
    end
    return false
  end 

  def add_match(team1,team2)
    @games << Game.new(team1,team2) unless scheduled_to_play?(team1,team2)
  end

  def to_s
    @games.sort_by{rand}.each{|game| puts 
      "#{game.home_team.name} vs. #{game.away_team.name}\n"}
  end
end

schedule = Schedule.new
teams= []

("A".."F").to_a.each{|letter| teams << Team.new(letter)}
teams.each do |home|
  teams.each do |away|
    schedule.add_match(home,away) unless home == away
  end
end
schedule.to_s

It’s a “warts and all” first pass (looking after I paste, I wanted to make some changes) but that’s not the point. the point was, task easily solved with Ruby and my night just a little brighter.

articleStats

Here are some silly little facts about this Fun little ruby time......

It was written by Leon 4 months ago.
It has 2420 letters in it.
It has 316 words in it.
It has a total of 2 comments in all.
So far fallenrogue has the last word!

article Links

These are the links that appear in this article. They probably don't make sense out of context... but just in case. :)

Check out Scott&#8217;s setup here
read the official rules here
 

The other stuff...

What the kids are saying...

4 months ago Eric said...

Here's a small "FTW-example"

###########################################################
puts (1..N=6).map{|i| (i...N).map{|j| "#{(i+64).chr} vs. #{(j+65).chr}"}}.sort_by{rand}
###########################################################

4 months ago fallenrogue said...

@Eric YES! fantastic! I knew it was only a matter of time before we got the single line version. :)

Leave a comment
*name:
*email: (never sold or published.)
url :

©2000-2008 fallenrogue.com | Some Rights reserved.