shiritori 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15306fc6b5e758835b251847186d047d17f5a097
4
- data.tar.gz: 670e46f10cde90c3cf71be56db9754f702e7778d
3
+ metadata.gz: 7ca46091473a69da8cca24df97a965bfd65f6300
4
+ data.tar.gz: a50c380b8b86bfb402d2641da8d7477eda101b8e
5
5
  SHA512:
6
- metadata.gz: 7909844236da3afcf8f0b4e52fd46b3a43e5e1db968062b6ae76712e883fc36b11448ca9f247ef530e83da51812e08232cde009cfe9024b9e206c721830b5aad
7
- data.tar.gz: 87cc9b4e7551c9ffbd1895710a0a9c48474d26942b4e50fb137eb69e0232b147a8d4129ab40a4d862cd086e1dfcb2e24e4d7cfb4a1ef327fba52f97a273df1b6
6
+ metadata.gz: 90aae5e28b8c64edb6097fb5434f79716776f8cd86038876824028e10cc732f163cc5ee04dda157ff6a91e308a8dcf396b84f14a0a946e9b3e01f43b964e4cf1
7
+ data.tar.gz: 29c19a4710ee8a3ee4ac88aab3e86dc8ae4d464aa9b158f9a0a2b1660fac61aa5b71e4ec04dd3b759bba9d633955cfb58a76a62ddeddb3533355a0511eabd4f3
@@ -4,20 +4,16 @@ module Shiritori
4
4
 
5
5
  def get_all_method
6
6
  @check_list = {}
7
- @method_list = []
8
-
9
- scan_method
7
+ @method_list = scan_methods
10
8
 
11
9
  @method_list - UNUSE_METHOD
12
10
  end
13
11
 
14
- def scan_method(klass = BasicObject)
15
- @check_list[klass] = true
16
- @method_list |= klass.instance_methods
12
+ def scan_methods(klass = BasicObject)
13
+ klasses = ObjectSpace.each_object(Class).to_a
14
+ klasses |= klasses.map(&:singleton_class)
17
15
 
18
- ObjectSpace.each_object(Class) do |subclass|
19
- scan_method(subclass) if klass != subclass && @check_list[subclass].nil?
20
- end
16
+ klasses.map(&:instance_methods).inject(&:|)
21
17
  end
22
18
  end
23
19
  end
@@ -3,7 +3,7 @@ require 'readline'
3
3
 
4
4
  module Shiritori
5
5
  class Main
6
- attr_reader :current_object, :chain_count, :error_count, :mode, :use_method_list
6
+ attr_reader :current_object, :chain_count, :error_count, :mode, :used_method_list
7
7
  include SearchMethod
8
8
  include View
9
9
 
@@ -34,7 +34,7 @@ module Shiritori
34
34
  def update(action: nil, object: nil)
35
35
  if action
36
36
  @all_method.delete(action)
37
- @use_method_list << action
37
+ @used_method_list << action
38
38
  @current_object = object
39
39
  @chain_count += 1
40
40
  end
@@ -53,7 +53,7 @@ module Shiritori
53
53
  @all_method = get_all_method
54
54
  @current_class = Object
55
55
  @current_chain = []
56
- @use_method_list = []
56
+ @used_method_list = []
57
57
  @chain_count = 0
58
58
  @error_count = 0
59
59
  @success = false
@@ -68,7 +68,7 @@ module Shiritori
68
68
 
69
69
  begin
70
70
  Thread.new do
71
- timeout(TIME_LIMIT) do
71
+ Timeout.timeout(TIME_LIMIT) do
72
72
  @current_object = eval(command.chomp)
73
73
  end
74
74
  end.join
@@ -116,7 +116,13 @@ module Shiritori
116
116
  command = command.chomp.sub(/^\./, '')
117
117
 
118
118
  begin
119
- result = exec_method_chain(command, current_object)
119
+ begin
120
+ object = Marshal.load(Marshal.dump(current_object))
121
+ rescue Exception => ex
122
+ object = current_object
123
+ end
124
+
125
+ result = try_method_chain(command, object)
120
126
 
121
127
  redo unless result
122
128
 
@@ -132,7 +138,7 @@ module Shiritori
132
138
  puts "Exec command #{[@current_object.to_ss, command].join('.')}"
133
139
  @current_chain << command
134
140
  update(action: action, object: object)
135
- else
141
+ elsif used_method_list[action]
136
142
  $error_message = "#{action} is already used."
137
143
  raise Shiritori::UseSameMethodError
138
144
  end
@@ -145,7 +151,7 @@ module Shiritori
145
151
  end
146
152
  end
147
153
 
148
- def exec_method_chain(command, object)
154
+ def try_method_chain(command, object)
149
155
  method_name = command.scan(METHOD_PATTERN).first.to_sym
150
156
  result = [method_name]
151
157
 
@@ -159,7 +165,7 @@ module Shiritori
159
165
  Thread.new do
160
166
  raise NoMethodError unless object.respond_to?(method_name)
161
167
 
162
- timeout(TIME_LIMIT) do
168
+ Timeout.timeout(TIME_LIMIT) do
163
169
  result << eval('object.' + command)
164
170
  end
165
171
  end.join
@@ -177,7 +183,7 @@ module Shiritori
177
183
 
178
184
  private
179
185
  def help_me(current_object)
180
- can_use_methods = current_object.methods - use_method_list
186
+ can_use_methods = current_object.methods - used_method_list
181
187
 
182
188
  new_line
183
189
 
@@ -1,3 +1,3 @@
1
1
  module Shiritori
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
@@ -8,7 +8,4 @@ describe "Shiritori test" do
8
8
  describe "Random singleton methods" do
9
9
  it { instance_check(:new, %q(0), obj: __class__) }
10
10
  end
11
-
12
- describe "Random instance methods" do
13
- end
14
- end
11
+ end
data/spec/spec_helper.rb CHANGED
@@ -27,7 +27,7 @@ module Helpers
27
27
  end
28
28
 
29
29
  def check(method, obj = same_object)
30
- main.exec_method_chain(method, obj)
30
+ main.try_method_chain(method, obj)
31
31
  end
32
32
 
33
33
  def compare_range(a, b)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shiritori
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - siman-man