[Groonga-commit] groonga/groonga at 3c17c5e [master] example: use Ruby 2.0 or later API

Back to archive index

Kouhei Sutou null+****@clear*****
Thu May 14 12:19:20 JST 2015


Kouhei Sutou	2015-05-14 12:19:20 +0900 (Thu, 14 May 2015)

  New Revision: 3c17c5ed94b33bd2915d9355517fabcb4d46ba36
  https://github.com/groonga/groonga/commit/3c17c5ed94b33bd2915d9355517fabcb4d46ba36

  Message:
    example: use Ruby 2.0 or later API

  Modified files:
    examples/dictionary/gene95/gene2grn.rb

  Modified: examples/dictionary/gene95/gene2grn.rb (+25 -38)
===================================================================
--- examples/dictionary/gene95/gene2grn.rb    2015-05-14 01:50:34 +0900 (0d10cfd)
+++ examples/dictionary/gene95/gene2grn.rb    2015-05-14 12:19:20 +0900 (c9d9a59)
@@ -1,46 +1,33 @@
 #!/usr/bin/env ruby
-# -*- coding: utf-8 -*-
 
-$KCODE = 'u'
+require "json"
 
-require 'kconv'
-
-class String
-  def to_json
-    a = split(//).map {|char|
-      case char
-      when '"' then '\\"'
-      when '\\' then '\\\\'
-      when "\b" then '\b'
-      when "\f" then '\f'
-      when "\n" then '\n'
-      when "\r" then ''
-      when "\t" then '\t'
-      else char
-      end
-    }
-    "\"#{a.join('')}\""
-  end
-end
-
-class Array
-  def to_json
-    '[' + map {|element|
-      element.to_json
-    }.join(',') + ']'
-  end
-end
-
-puts <<END
+print(<<HEADER.chomp)
 column_create item_dictionary gene95_desc COLUMN_SCALAR ShortText
 column_create bigram item_dictionary_gene95_desc COLUMN_INDEX|WITH_POSITION item_dictionary gene95_desc
 load --table item_dictionary
-[["_key","gene95_desc"],
-END
+[
+["_key","gene95_desc"]
+HEADER
 
-while !STDIN.eof?
-  key = Kconv.toutf8(gets.strip)
-  body = Kconv.toutf8(gets.strip)
-  puts [key, body].to_json
+loop do
+  raw_key = gets
+  break if raw_key.nil?
+  raw_body = gets
+
+  key = nil
+  body = nil
+  begin
+    key = raw_key.encode("UTF-8", "Windows-31J").strip
+    body = raw_body.encode("UTF-8", "Windows-31J").strip
+  rescue EncodingError
+    $stderr.puts("Ignore:")
+    $stderr.puts("   key: <#{raw_key}>")
+    $stderr.puts("  body: <#{raw_body}>")
+    next
+  end
+  puts(",")
+  print([key, body].to_json)
 end
-puts ']'
+puts
+puts("]")
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index