Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:31:07+00:00 2026-05-13T08:31:07+00:00

It would be helpful to pick up Ruby for my new gig so this

  • 0

It would be helpful to pick up Ruby for my new gig so this morning I wrote the following. It takes a PGN file of chess games I’ve played and indexes them by first move. I’d appreciate any suggestions as to how to make it more “idiomatic”.

Since it doesn’t take command line arguments (such as for the filename) and isn’t object oriented suggestions along those lines are certainly welcome.

Bear in mind, I’m creating an index of all of the moves (not just first moves) from all of the games, because I’d like to eventually index on more than just the first move.

Data follows code.

games = []
file = File.new("jemptymethod.pgn", "r")

is_header = false
is_score = false

Game = Struct::new(:header, :score)

while (line = file.gets)
  if !line.chomp.empty?
    if !is_score && !is_header
      game = Game::new('','')
    end
    if /^\[/.match(line)
      is_header = true
      game.header << line
    else
      is_score = true
      game.score << line
    end
  else
    if is_score
      is_score = false
      is_header = false
      games << game
    end
  end
end

file.close
puts "# Games: " + games.length.to_s
moves_index = {}
first_moves = {}

games.each { |gm|
  #the following output should essentially be lossless
  #with the possible exception of beginning or ending newlines
  #
  #puts gm.header + "\n"
  #puts gm.score + "\n"

  score_tokens = gm.score.split(/\s+/);
  game_moves = []

  score_tokens.each_index{|i|
    if i%3 != 0
      move_token = score_tokens[i]
      if !moves_index.has_key?(move_token)
        moves_index[move_token] = moves_index.keys.length
      end
      game_moves << moves_index[move_token]
    end
  }

  first_move = moves_index.index(game_moves[0])

  if !first_moves.has_key?(first_move)
    first_moves[first_move] = 1
  else
    first_moves[first_move] = 1 + first_moves[first_move]
  end
}

# sorting hashes by value: http://nhw.pl/wp/2007/06/11/sorting-hash-by-values
first_moves.sort{|a,b| -1*(a[1]<=>b[1])}.each{|k,v|
  puts "1. #{k} occurred #{v} times" 
}

Data (just 3 games, I’ve been working with 25):

[Event "Enough With the Draws Already ;)"]
[Site "http://www.queenalice.com/game.php?id=533406"]
[Date "2009.2.1"]
[Round "-"]
[White "Troy"]
[Black "jemptymethod"]
[Result "1/2-1/2"]
[WhiteElo "1300"]
[BlackElo "2076"]
[ECO "C36"]

1. e4 e5 2. f4 exf4 3. Nf3 Be7 4. Bc4 Nf6 5. Qe2 d5 6. exd5 Nxd5 7. O-O Be6 8.
d4 Nc6 9. Nc3 O-O 10. Nxd5 Bxd5 11. Bxd5 Qxd5 12. Bxf4 Bd6 13. Qd2 Rae8 14. Bxd6
Qxd6 15. Rae1 h6 16. c3 Qd5 17. b3 Qa5 18. h3 a6 19. Rf2 Re7 20. Rxe7 Nxe7 21.
Ne5 Nd5 22. c4 Qxd2 1/2-1/2

[Event "AUTO-MASTER-620"]
[Site "http://www.queenalice.com/game.php?id=545265"]
[Date "2009.2.23"]
[Round "2"]
[White "testouverture"]
[Black "jemptymethod"]
[Result "1/2-1/2"]
[WhiteElo "2240"]
[BlackElo "2179"]
[ECO "A52"]

1. d4 Nf6 2. c4 e5 3. dxe5 Ng4 4. Nf3 Bc5 5. e3 Nc6 6. Be2 O-O 7. O-O Re8 8. b3
Ngxe5 9. Bb2 Nxf3+ 10. Bxf3 Ne5 11. Nc3 a5 12. Ne4 Bf8 13. Bh5 Ra6 14. f4 Ng6
15. Ng5 d5 16. Nxf7 Kxf7 17. f5 Kg8 18. fxg6 hxg6 19. Qd4 Qe7 20. Bf3 dxc4 21.
Qxc4+ Be6 22. Qc3 c6 23. Be2 Raa8 24. Bd3 Bf5 25. Bxf5 gxf5 26. Rf3 Qc5 27. Re1
Qxc3 28. Bxc3 g6 29. g4 Bg7 30. Bxg7 fxg4 31. Rg3 Kxg7 32. Rxg4 Rad8 33. Kf2
1/2-1/2

[Event "AUTO-MASTER-620"]
[Site "http://www.queenalice.com/game.php?id=545266"]
[Date "2009.2.23"]
[Round "2"]
[White "jemptymethod"]
[Black "testouverture"]
[Result "0-1"]
[WhiteElo "2079"]
[BlackElo "2306"]
[ECO "B22"]

1. e4 c5 2. c3 d5 3. exd5 Qxd5 4. d4 Nc6 5. dxc5 Qxd1+ 6. Kxd1 e5 7. Be3 Nf6 8.
b4 a5 9. b5 Ne7 10. Nf3 Ng4 11. Bc4 Nf5 12. Ke2 Nfxe3 13. fxe3 Bxc5 14. h3 Nxe3
15. Nxe5 f6 0-1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:31:08+00:00Added an answer on May 13, 2026 at 8:31 am

    I haven’t done a complete refactoring because I want to keep enough of your original code intact that it’s not too confusing. The major change is introduction of a Game class that handles parsing. The implementation of this class can be much improved, but it works without changing your code too much. Also, some minor points:

    • Instead of File.new, read the file using File.open and give it a block that takes the file parameter. The file is automatically closed at the end of the block.

    • Use of a += 1 instead of a = a + 1.

    I made up a simple notation and wrote a parser for handling the play-by-play details of a tennis match. You might want to look at that code for an example of parsing game moves. It’s actually very similar to what you’re doing. The bulk of the code is in the /lib directory. Parsing logic is in parser.rb and game components are in the other files. I’d encourage you to break your chess games up in a similar way, by adding a Move class.

    Anyway, here’s my half-refactoring of your code:

    class Game
      attr_accessor :header, :score, :moves
    
      def initialize
        @header = ""
        @score  = ""
        @moves  = []
      end
    
      def first_move
        moves_index.index(moves[0])
      end
    
      def moves_index
        moves_index = {}
        score.split(/\s+/).each_with_index do |move,i|
          if i%3 != 0
            unless moves_index.has_key?(move)
              moves_index[move] = moves_index.keys.length
            end
            moves << moves_index[move]
          end
        end
        moves_index
      end
    end
    
    games     = []
    is_header = false
    is_score  = false
    
    File.open("jemptymethod.pgn") do |file|
      while (line = file.gets)
        if !line.chomp.empty?
          if !is_score && !is_header
            game = Game.new
          end
          if line[0,1] == '['
            is_header = true
            game.header << line
          else
            is_score = true
            game.score << line
          end
        elsif is_score
          is_score = false
          is_header = false
          games << game
        end
      end
    end
    
    puts "# Games: " + games.length.to_s
    first_moves = {}
    
    #the following output should essentially be lossless
    #with the possible exception of beginning or ending newlines
    #
    #puts gm.header + "\n"
    #puts gm.score + "\n"
    games.each do |gm|
      if !first_moves.has_key?(gm.first_move)
        first_moves[gm.first_move] = 1
      else
        first_moves[gm.first_move] += 1
      end
    end
    
    # sorting hashes by value: http://nhw.pl/wp/2007/06/11/sorting-hash-by-values
    first_moves.sort{|a,b| -1*(a[1]<=>b[1])}.each{|k,v|
      puts "1. #{k} occurred #{v} times" 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 291k
  • Answers 291k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can simulate a button click for your button within… May 13, 2026 at 5:54 pm
  • Editorial Team
    Editorial Team added an answer Sounds like the graphics aren't being loaded properly. By default… May 13, 2026 at 5:54 pm
  • Editorial Team
    Editorial Team added an answer Depending on the configuration of your DB server, there is… May 13, 2026 at 5:54 pm

Related Questions

So, a long long time ago I used to program procedurally initially with Basic
Just got a request from my boss for an application I'm working on. Basically
Python, through it's readline bindings allows for great command-line autocompletion (as described in here
The idea here is to get better programmers right out of college. I think
At work we use a couple of custom columns in the Outlook contacts to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.