Skip to content

Instantly share code, notes, and snippets.

@bchase
Created July 22, 2015 19:32
Show Gist options
  • Select an option

  • Save bchase/d578d7e66363954a1e5f to your computer and use it in GitHub Desktop.

Select an option

Save bchase/d578d7e66363954a1e5f to your computer and use it in GitHub Desktop.
#!/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