Created
July 22, 2015 19:32
-
-
Save bchase/d578d7e66363954a1e5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'ostruct' | |
| require 'pry' | |
| CEDict = Struct.new :trad, :simp, :pinyin, :glosses | |
| JMDict = Struct.new :kanji, :kana, :glosses | |
| module DictResourceable | |
| %i[word pronunciation meanings].each do |msg| | |
| define_method msg do | |
| self.send FIELD_TYPES[self.class][__method__] | |
| end | |
| end | |
| private | |
| FIELD_TYPES = { | |
| JMDict => { | |
| word: :kanji, | |
| pronunciation: :kana, | |
| meanings: :glosses, | |
| # part_of_speech: :pos, | |
| }, | |
| CEDict => { | |
| word: :trad, | |
| pronunciation: :pinyin, | |
| meanings: :glosses, | |
| } | |
| } | |
| end | |
| JMDict.include DictResourceable | |
| de = JMDict.new '漢字', 'かんじ', [ 'Kanji', 'Chinese characters' ] | |
| de.meanings # => [ 'Kanji', 'Chinese characters' ] | |
| binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment