Code translation, from Python to Ruby -


I would like to convert some dragon lines on Ruby to this excellent article signed by Thomas Guest:

(Note: I am actually a big python knot)

Here is a copy of the original Python version:

  def expand_blanks (fen): '' 'in the empty space Extend points in a FEN string & gt; & Gt; & Gt; ("RK4q3") 'RQQ' '' DEF Expansion (match): Return '' * Ent (MatchGroup (0)) Reactor Package (R 'D'). Sub (extension, fan) def external junction (sep, ss): '' 'like string, but joins the result with an external separator. Example: & gt; & Gt; & Gt; External_joint ('|', ['1', '2', '3']) '| 1 | 2 | 3 | ' Returns'% s% s% s'% (sep, sep.join (ss), sep) def ascii_draw_chess_position (fen): '' 'Returns an ASCII image of pieces on a chess. '' Fragments = broad_bank (fen) .replace ('/', '') divider = '+ + + + + + + + + + + + + + + \ n' rows = ((External 'zone' 'External' join (divider, rows)  

Usage example for pieces R [R: R + 8]) R in range (0, 8 * 8, 8) :

  & gt; Fen = "r2q1rk1 / pp2ppbp / 1np2np1 / 2Q3B1 / 3PP1b1 / 2N2N2 / pp3ppp/3kkb1r"> gt; & gt; & Gt; print ascii_draw_chess_position (fen) + - + - + - + - + + + + + + + | R | | | | | R | K | + + + + + + + + + + + + + + + + + + + + | P | P | | | P | P | B | P | + - + - + - + - + + + + + + + + | | N | P | | | N | P | + + - + - + - + - + + + + + + + + + | Q | | | | B | + + + + + + + + + + + + + + + | | | P | P | | B | + + + + + + + + - + + + + + + + + | | N | | N | | + + + + + + + + + + + + + + + | P | P | P | P | P | P | P | + - + - + - + - + + + + + + + | || | | R | K | B | R | + + + + + + +  
< P> I have tried to make some modifications to convert every row in Ruby ... and I wonder if this is bad: start but I publish it in any way:

Def def_blanks (fen) def extension (match) returns '' * int (match.group (0)) end # bugged: re.compile ( R '\ d'). Sub (Extension, Feng) End DEF External Association (SEP, SS) SEP + SEP.JOND (SS) + SEP & DEF ASCI_DRA_CHES_POSITION (FEN) Pieces = Extension_bank (Fen). Places place ('/', '') . Square divider = "+ + + + + + + + + + + + + + + \ n" #Bug Lines: Rows = ((External 'Zone' ('' ', Pieces [R, R + 8] R '"Ascii_draw_chess_position (fen) for reservation reserved for 8, 8/8 / PPPPPP / RNBQBNR (RTE) (0, 8 * 8, 8))

Let's see the lines that I can fix, I would be good for you, thank you all.

Looking at each method in return, start with external_joy .

In Python, there is a method on the joining string, which takes a sequence and gets in the elements delimited by the string such as '|' In the Ruby, there is a method on the join arrays, as a logic, the delimiter takes ,

, 'p', 'p', 'p'])

['P', 'p', 'p']. Join ('|')

Then there will be a Ruby version of outer_join :

< Ex> def external junction (sep, ss): sep + ss.join (sep) + sep end

expand_blanks , here The objective is to replace one digit with an equal number of spaces, in ruby ​​it can be achieved by using gsub to change all the occurrences of a pattern in a string. The gsub can be called with a block which passes each match substrings and returns the string which should change the match. So the Ruby Code will be:

  def expand_blanks (fen) fen.gsub (/ \ d /) {| Match | '* * Match.to_i} End  

Finally ascii_draw_chess_position we get the

gsub again in the form of the dragon's Use the map function of the ruby ​​obtained by using the method and a list comprehension in Python:

  def Ascii_draw_chess_position (fen) pieces = expand_blanks (fen) .gsub ('/', '') divider = "+ - + - + - + + + + + + + + + + \ n" lines = (0 ... 8 ). MAP | I | Line = pieces [i * 8 ... (i + 1) * 8] .split ('') outer_join ("" "," row) + "\ n" end outer_join (divider, rows) Ends  

Let me know if you need any more details given above.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -