Ruby において、複数形を単数系に変換するメソッドsingularizeを使いたいのですが、以下のエラーになります。
どうすれば解決できるでしょうか?
<main>': undefined method
pluralize' for main:Object (NoMethodError)
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答3件
0
ベストアンサー
私の場合はrequire 'active_support/core_ext'と書くと
cannot load such file -- active_support/core_ext (LoadError)
と表示されます。
これを書かないで
'posts'.singularize
とだけ書くと
undefined method `singularize` for "posts":String
というエラーが発生します。
Railsに同梱されている機能というのが存在するのか
どうかわかりませんがRuby単体には存在しない
機能なのではないかなぁと私は予想しています。
私のRubyのバージョンなど
Ruby 2.2.6 Windows32bit版
PHP 複数形を単数形に変換 - サンプルコード
こちらのページに書いてあるコードをRubyに書き換えてみました。
Ruby
1# encoding: utf-8 2 3class String 4 def singularize 5 P2S.singular_by_plural(self) 6 end 7end 8 9class P2S 10 attr_reader :word 11 12 @irregular_rules = { 13 'men' => 'man', 14 'seamen' => 'seaman', 15 'snowmen' => 'snowman', 16 'women' => 'woman', 17 'people' => 'person', 18 'children' => 'child', 19 'sexes' => 'sex', 20 'moves' => 'move', 21 'databases' => 'database', 22 'feet' => 'foot', 23 'cruces' => 'crux', 24 'oases' => 'oasis', 25 'phenomena' => 'phenomenon', 26 'teeth' => 'tooth', 27 'geese' => 'goose', 28 'atlases' => 'atlas', 29 'corpuses' => 'corpus', 30 'genies' => 'genie', 31 'genera' => 'genus', 32 'graffiti' => 'graffito', 33 'loaves' => 'loaf', 34 'mythoi' => 'mythos', 35 'niches' => 'niche', 36 'numina' => 'numen', 37 'octopuses' => 'octopus', 38 'opuses' => 'opus', 39 'penises' => 'penis', 40 'equipment' => 'equipment', 41 'information' => 'information', 42 'rice' => 'rice', 43 'money' => 'money', 44 'species' => 'species', 45 'series' => 'series', 46 'fish' => 'fish', 47 'sheep' => 'sheep', 48 'swiss' => 'swiss', 49 'progress' => 'progress', 50 'nonsense' => 'nonsense' 51 }.freeze 52 53 @singular_rules = { 54 '(quiz)zes$' => '\1', 55 '(matr)ices$' => '\1ix', 56 '(vert|ind)ices$' => '\1ex', 57 '^(ox)en' => '\1', 58 '(alias|status)es$' => '\1', 59 '(octop|vir)i$' => '\1us', 60 '(cris|ax|test)es$' => '\1is', 61 '(shoe)s$' => '\1', 62 '(o)es$' => '\1', 63 '(bus)es$' => '\1', 64 '([m|l])ice$' => '\1ouse', 65 '(x|ch|ss|sh)es$' => '\1', 66 'movies$' => 'movie', 67 'series$' => 'series', 68 '([^aeiouy]|qu)ies$' => '\1y', 69 '([lr])ves$' => '\1f', 70 '(tive)s$' => '\1', 71 '(hive)s$' => '\1', 72 '([^f])ves$' => '\1fe', 73 '(^analy)ses$' => '\1sis', 74 '(analy|ba|diagno|parenthe|progno|synop|the)ses$' => '\1sis', 75 '([ti])a$' => '\1um', 76 '(n)ews$' => '\1ews', 77 '(.)s$' => '\1' 78 }.freeze 79 80 def self.singular_by_plural(prural) 81 return @irregular_rules[prural] if @irregular_rules.key?(prural) 82 83 @singular_rules.each do |key, value| 84 return prural.sub(/#{key}/, value) if prural =~ /#{key}/ 85 end 86 87 nil 88 end 89end 90 91puts 'computers'.singularize # => computer 92
参考にしたページ
【まとめ】インスタンス変数、クラス変数、クラスインスタンス変数
Rubyで逆ポーランド記法の計算機と変換器を作った
progressとnonsenseは追加しました。
投稿2017/07/11 09:24
編集2017/07/11 13:36退会済みユーザー
総合スコア0
0
.singularize
はString
のめそっどなので、'posts'.singularize
のように使います(参考)。
投稿2017/07/11 05:01
総合スコア145123
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/11 05:08
2017/07/11 05:10
2017/07/11 05:11
2017/07/11 05:17
2017/07/11 08:46
2017/07/12 07:16
2017/07/12 07:20
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/11 10:52
退会済みユーザー
2017/07/11 11:13
2017/07/12 07:18
退会済みユーザー
2017/07/12 09:45