Skip to content

Instantly share code, notes, and snippets.

@shoyan
Created April 16, 2014 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoyan/10804126 to your computer and use it in GitHub Desktop.
Save shoyan/10804126 to your computer and use it in GitHub Desktop.
ymlから単語リストを取得して、ランダムでだす(サンプル)
class WordList
class << self
def find(option = {})
default_option = {limit: 10}.merge(option)
option = default_option.merge(option)
word_list_path = Rails.root.join('lib/word_list/config/')
@word_list = []
@file ||= YAML.load_file(word_list_path.to_s + file)
Dir::foreach(word_list_path) do |file|
@word_list.concat(YAML.load_file(word_list_path.to_s + file)) if File::extn
ame(file) == '.yml'
end
if option[:type] == 'rand'
find_rand(option)
elsif option[:type] == 'ahead'
# 未実装
elsif option[:type] == 'behind'
# 未実装
elsif option[:type] == 'partial'
# 未実装
else
find_rand(option)
end
end
def find_rand(option)
@word_list.sample(option[:limit])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment