teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

11

修正

2017/07/11 13:36

投稿

退会済みユーザー
answer CHANGED
File without changes

10

修正

2017/07/11 13:36

投稿

退会済みユーザー
answer CHANGED
@@ -63,7 +63,7 @@
63
63
  'numina' => 'numen',
64
64
  'octopuses' => 'octopus',
65
65
  'opuses' => 'opus',
66
- 'penises' => 'penis',
66
+ 'penises' => 'penis',
67
67
  'equipment' => 'equipment',
68
68
  'information' => 'information',
69
69
  'rice' => 'rice',

9

修正

2017/07/11 13:34

投稿

退会済みユーザー
answer CHANGED
@@ -98,7 +98,7 @@
98
98
  '(hive)s$' => '\1',
99
99
  '([^f])ves$' => '\1fe',
100
100
  '(^analy)ses$' => '\1sis',
101
- '(analy|ba|diagno|parenthe|progno|synop|the)ses$' => '\1sis',
101
+ '(analy|ba|diagno|parenthe|progno|synop|the)ses$' => '\1sis',
102
102
  '([ti])a$' => '\1um',
103
103
  '(n)ews$' => '\1ews',
104
104
  '(.)s$' => '\1'

8

修正

2017/07/11 13:29

投稿

退会済みユーザー
answer CHANGED
@@ -29,14 +29,14 @@
29
29
 
30
30
  class String
31
31
  def singularize
32
- P2S.new.singularByPlural(self)
32
+ P2S.singular_by_plural(self)
33
33
  end
34
34
  end
35
35
 
36
36
  class P2S
37
37
  attr_reader :word
38
38
 
39
- @@irregular_rules = {
39
+ @irregular_rules = {
40
40
  'men' => 'man',
41
41
  'seamen' => 'seaman',
42
42
  'snowmen' => 'snowman',
@@ -77,7 +77,7 @@
77
77
  'nonsense' => 'nonsense'
78
78
  }.freeze
79
79
 
80
- @@singular_rules = {
80
+ @singular_rules = {
81
81
  '(quiz)zes$' => '\1',
82
82
  '(matr)ices$' => '\1ix',
83
83
  '(vert|ind)ices$' => '\1ex',
@@ -104,13 +104,11 @@
104
104
  '(.)s$' => '\1'
105
105
  }.freeze
106
106
 
107
- def singularByPlural(prural)
107
+ def self.singular_by_plural(prural)
108
- return @@irregular_rules[prural] if @@irregular_rules.key?(prural)
108
+ return @irregular_rules[prural] if @irregular_rules.key?(prural)
109
-
109
+
110
- @@singular_rules.each do |key, value|
110
+ @singular_rules.each do |key, value|
111
- if prural =~ /#{key}/
112
- return prural.sub(/#{key}/, value)
111
+ return prural.sub(/#{key}/, value) if prural =~ /#{key}/
113
- end
114
112
  end
115
113
 
116
114
  nil

7

修正

2017/07/11 13:27

投稿

退会済みユーザー
answer CHANGED
@@ -113,7 +113,7 @@
113
113
  end
114
114
  end
115
115
 
116
- 'Not found'
116
+ nil
117
117
  end
118
118
  end
119
119
 

6

修正

2017/07/11 13:15

投稿

退会済みユーザー
answer CHANGED
@@ -117,7 +117,7 @@
117
117
  end
118
118
  end
119
119
 
120
- puts 'computer'.singularize # => computer
120
+ puts 'computers'.singularize # => computer
121
121
 
122
122
  ```
123
123
  参考にしたページ

5

修正

2017/07/11 11:24

投稿

退会済みユーザー
answer CHANGED
@@ -117,7 +117,7 @@
117
117
  end
118
118
  end
119
119
 
120
- puts 'progress'.singularize # => computer
120
+ puts 'computer'.singularize # => computer
121
121
 
122
122
  ```
123
123
  参考にしたページ

4

修正

2017/07/11 11:23

投稿

退会済みユーザー
answer CHANGED
@@ -38,70 +38,70 @@
38
38
 
39
39
  @@irregular_rules = {
40
40
  'men' => 'man',
41
- 'seamen' => 'seaman',
41
+ 'seamen' => 'seaman',
42
- 'snowmen' => 'snowman',
42
+ 'snowmen' => 'snowman',
43
- 'women' => 'woman',
43
+ 'women' => 'woman',
44
- 'people' => 'person',
44
+ 'people' => 'person',
45
- 'children' => 'child',
45
+ 'children' => 'child',
46
- 'sexes' => 'sex',
46
+ 'sexes' => 'sex',
47
- 'moves' => 'move',
47
+ 'moves' => 'move',
48
- 'databases' => 'database',
48
+ 'databases' => 'database',
49
- 'feet' => 'foot',
49
+ 'feet' => 'foot',
50
- 'cruces' => 'crux',
50
+ 'cruces' => 'crux',
51
- 'oases' => 'oasis',
51
+ 'oases' => 'oasis',
52
- 'phenomena' => 'phenomenon',
52
+ 'phenomena' => 'phenomenon',
53
- 'teeth' => 'tooth',
53
+ 'teeth' => 'tooth',
54
- 'geese' => 'goose',
54
+ 'geese' => 'goose',
55
- 'atlases' => 'atlas',
55
+ 'atlases' => 'atlas',
56
- 'corpuses' => 'corpus',
56
+ 'corpuses' => 'corpus',
57
- 'genies' => 'genie',
57
+ 'genies' => 'genie',
58
- 'genera' => 'genus',
58
+ 'genera' => 'genus',
59
- 'graffiti' => 'graffito',
59
+ 'graffiti' => 'graffito',
60
- 'loaves' => 'loaf',
60
+ 'loaves' => 'loaf',
61
- 'mythoi' => 'mythos',
61
+ 'mythoi' => 'mythos',
62
- 'niches' => 'niche',
62
+ 'niches' => 'niche',
63
- 'numina' => 'numen',
63
+ 'numina' => 'numen',
64
- 'octopuses' => 'octopus',
64
+ 'octopuses' => 'octopus',
65
- 'opuses' => 'opus',
65
+ 'opuses' => 'opus',
66
- 'penises' => 'penis',
66
+ 'penises' => 'penis',
67
- 'equipment' => 'equipment',
67
+ 'equipment' => 'equipment',
68
- 'information' => 'information',
68
+ 'information' => 'information',
69
- 'rice' => 'rice',
69
+ 'rice' => 'rice',
70
- 'money' => 'money',
70
+ 'money' => 'money',
71
- 'species' => 'species',
71
+ 'species' => 'species',
72
- 'series' => 'series',
72
+ 'series' => 'series',
73
- 'fish' => 'fish',
73
+ 'fish' => 'fish',
74
- 'sheep' => 'sheep',
74
+ 'sheep' => 'sheep',
75
- 'swiss' => 'swiss',
75
+ 'swiss' => 'swiss',
76
76
  'progress' => 'progress',
77
77
  'nonsense' => 'nonsense'
78
78
  }.freeze
79
79
 
80
80
  @@singular_rules = {
81
- '(quiz)zes$' => '\1',
81
+ '(quiz)zes$' => '\1',
82
- '(matr)ices$' => '\1ix',
82
+ '(matr)ices$' => '\1ix',
83
- '(vert|ind)ices$' => '\1ex',
83
+ '(vert|ind)ices$' => '\1ex',
84
- '^(ox)en' => '\1',
84
+ '^(ox)en' => '\1',
85
- '(alias|status)es$' => '\1',
85
+ '(alias|status)es$' => '\1',
86
- '(octop|vir)i$' => '\1us',
86
+ '(octop|vir)i$' => '\1us',
87
- '(cris|ax|test)es$' => '\1is',
87
+ '(cris|ax|test)es$' => '\1is',
88
- '(shoe)s$' => '\1',
88
+ '(shoe)s$' => '\1',
89
- '(o)es$' => '\1',
89
+ '(o)es$' => '\1',
90
- '(bus)es$' => '\1',
90
+ '(bus)es$' => '\1',
91
- '([m|l])ice$' => '\1ouse',
91
+ '([m|l])ice$' => '\1ouse',
92
- '(x|ch|ss|sh)es$' => '\1',
92
+ '(x|ch|ss|sh)es$' => '\1',
93
- 'movies$' => 'movie',
93
+ 'movies$' => 'movie',
94
- 'series$' => 'series',
94
+ 'series$' => 'series',
95
- '([^aeiouy]|qu)ies$' => '\1y',
95
+ '([^aeiouy]|qu)ies$' => '\1y',
96
- '([lr])ves$' => '\1f',
96
+ '([lr])ves$' => '\1f',
97
- '(tive)s$' => '\1',
97
+ '(tive)s$' => '\1',
98
- '(hive)s$' => '\1',
98
+ '(hive)s$' => '\1',
99
- '([^f])ves$' => '\1fe',
99
+ '([^f])ves$' => '\1fe',
100
- '(^analy)ses$' => '\1sis',
100
+ '(^analy)ses$' => '\1sis',
101
- '(analy|ba|diagno|parenthe|progno|synop|the)ses$' => '\1sis',
101
+ '(analy|ba|diagno|parenthe|progno|synop|the)ses$' => '\1sis',
102
- '([ti])a$' => '\1um',
102
+ '([ti])a$' => '\1um',
103
- '(n)ews$' => '\1ews',
103
+ '(n)ews$' => '\1ews',
104
- '(.)s$' => '\1'
104
+ '(.)s$' => '\1'
105
105
  }.freeze
106
106
 
107
107
  def singularByPlural(prural)
@@ -117,7 +117,8 @@
117
117
  end
118
118
  end
119
119
 
120
- puts 'computers'.singularize # => computer
120
+ puts 'progress'.singularize # => computer
121
+
121
122
  ```
122
123
  参考にしたページ
123
124
  [【まとめ】インスタンス変数、クラス変数、クラスインスタンス変数](http://qiita.com/mogulla3/items/cd4d6e188c34c6819709)

3

修正

2017/07/11 11:23

投稿

退会済みユーザー
answer CHANGED
@@ -72,7 +72,9 @@
72
72
  'series' => 'series',
73
73
  'fish' => 'fish',
74
74
  'sheep' => 'sheep',
75
- 'swiss' => 'swiss'
75
+ 'swiss' => 'swiss',
76
+ 'progress' => 'progress',
77
+ 'nonsense' => 'nonsense'
76
78
  }.freeze
77
79
 
78
80
  @@singular_rules = {
@@ -115,8 +117,10 @@
115
117
  end
116
118
  end
117
119
 
118
- p 'computers'.singularize # => "computer"
120
+ puts 'computers'.singularize # => computer
119
121
  ```
120
122
  参考にしたページ
121
123
  [【まとめ】インスタンス変数、クラス変数、クラスインスタンス変数](http://qiita.com/mogulla3/items/cd4d6e188c34c6819709)
122
- [Rubyで逆ポーランド記法の計算機と変換器を作った](http://qiita.com/siman/items/d72854c4c4aaaf452868)
124
+ [Rubyで逆ポーランド記法の計算機と変換器を作った](http://qiita.com/siman/items/d72854c4c4aaaf452868)
125
+
126
+ progressとnonsenseは追加しました。

2

修正

2017/07/11 11:10

投稿

退会済みユーザー
answer CHANGED
@@ -22,3 +22,101 @@
22
22
  私のRubyのバージョンなど
23
23
  Ruby 2.2.6 Windows32bit版
24
24
 
25
+ [PHP 複数形を単数形に変換 - サンプルコード](http://pb-times.jp/P_527b395742f17)
26
+ こちらのページに書いてあるコードをRubyに書き換えてみました。
27
+ ```Ruby
28
+ # encoding: utf-8
29
+
30
+ class String
31
+ def singularize
32
+ P2S.new.singularByPlural(self)
33
+ end
34
+ end
35
+
36
+ class P2S
37
+ attr_reader :word
38
+
39
+ @@irregular_rules = {
40
+ 'men' => 'man',
41
+ 'seamen' => 'seaman',
42
+ 'snowmen' => 'snowman',
43
+ 'women' => 'woman',
44
+ 'people' => 'person',
45
+ 'children' => 'child',
46
+ 'sexes' => 'sex',
47
+ 'moves' => 'move',
48
+ 'databases' => 'database',
49
+ 'feet' => 'foot',
50
+ 'cruces' => 'crux',
51
+ 'oases' => 'oasis',
52
+ 'phenomena' => 'phenomenon',
53
+ 'teeth' => 'tooth',
54
+ 'geese' => 'goose',
55
+ 'atlases' => 'atlas',
56
+ 'corpuses' => 'corpus',
57
+ 'genies' => 'genie',
58
+ 'genera' => 'genus',
59
+ 'graffiti' => 'graffito',
60
+ 'loaves' => 'loaf',
61
+ 'mythoi' => 'mythos',
62
+ 'niches' => 'niche',
63
+ 'numina' => 'numen',
64
+ 'octopuses' => 'octopus',
65
+ 'opuses' => 'opus',
66
+ 'penises' => 'penis',
67
+ 'equipment' => 'equipment',
68
+ 'information' => 'information',
69
+ 'rice' => 'rice',
70
+ 'money' => 'money',
71
+ 'species' => 'species',
72
+ 'series' => 'series',
73
+ 'fish' => 'fish',
74
+ 'sheep' => 'sheep',
75
+ 'swiss' => 'swiss'
76
+ }.freeze
77
+
78
+ @@singular_rules = {
79
+ '(quiz)zes$' => '\1',
80
+ '(matr)ices$' => '\1ix',
81
+ '(vert|ind)ices$' => '\1ex',
82
+ '^(ox)en' => '\1',
83
+ '(alias|status)es$' => '\1',
84
+ '(octop|vir)i$' => '\1us',
85
+ '(cris|ax|test)es$' => '\1is',
86
+ '(shoe)s$' => '\1',
87
+ '(o)es$' => '\1',
88
+ '(bus)es$' => '\1',
89
+ '([m|l])ice$' => '\1ouse',
90
+ '(x|ch|ss|sh)es$' => '\1',
91
+ 'movies$' => 'movie',
92
+ 'series$' => 'series',
93
+ '([^aeiouy]|qu)ies$' => '\1y',
94
+ '([lr])ves$' => '\1f',
95
+ '(tive)s$' => '\1',
96
+ '(hive)s$' => '\1',
97
+ '([^f])ves$' => '\1fe',
98
+ '(^analy)ses$' => '\1sis',
99
+ '(analy|ba|diagno|parenthe|progno|synop|the)ses$' => '\1sis',
100
+ '([ti])a$' => '\1um',
101
+ '(n)ews$' => '\1ews',
102
+ '(.)s$' => '\1'
103
+ }.freeze
104
+
105
+ def singularByPlural(prural)
106
+ return @@irregular_rules[prural] if @@irregular_rules.key?(prural)
107
+
108
+ @@singular_rules.each do |key, value|
109
+ if prural =~ /#{key}/
110
+ return prural.sub(/#{key}/, value)
111
+ end
112
+ end
113
+
114
+ 'Not found'
115
+ end
116
+ end
117
+
118
+ p 'computers'.singularize # => "computer"
119
+ ```
120
+ 参考にしたページ
121
+ [【まとめ】インスタンス変数、クラス変数、クラスインスタンス変数](http://qiita.com/mogulla3/items/cd4d6e188c34c6819709)
122
+ [Rubyで逆ポーランド記法の計算機と変換器を作った](http://qiita.com/siman/items/d72854c4c4aaaf452868)

1

修正

2017/07/11 11:01

投稿

退会済みユーザー
answer CHANGED
@@ -17,4 +17,8 @@
17
17
 
18
18
  Railsに同梱されている機能というのが存在するのか
19
19
  どうかわかりませんがRuby単体には存在しない
20
- 機能なのではないかなぁと私は予想しています。
20
+ 機能なのではないかなぁと私は予想しています。
21
+
22
+ 私のRubyのバージョンなど
23
+ Ruby 2.2.6 Windows32bit版
24
+