rdoba 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rdoba/io.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/ruby -KU
2
- #coding:utf-8
2
+ # frozen_string_literal: true
3
3
 
4
4
  require 'strscan'
5
5
  require 'rdoba/re'
@@ -7,7 +7,7 @@ require 'rdoba/roman'
7
7
  require 'rdoba/numeric'
8
8
 
9
9
  module Kernel
10
- alias :__sprintf__ :sprintf
10
+ alias __sprintf__ sprintf
11
11
  def sprintf(format, *args)
12
12
  nargs = []
13
13
  nformat = ''
@@ -15,26 +15,31 @@ module Kernel
15
15
  fmt = format.split('%')
16
16
  nformat = fmt.shift
17
17
 
18
- while (not fmt.empty?)
18
+ until fmt.empty?
19
19
  part = fmt.shift
20
- part = '%' + fmt.shift unless part
21
- if part =~ /([0-9 #+\-*.]*)([bcdEefGgiopsuXxP])(.*)/ and $2 == 'P'
22
- keys = $1 || ''
23
- str = $3 || ''
24
- if keys =~ /(-)?([0-9*]*)\.?([0-9\*]*)(\+?)/
20
+ part ||= '%' + fmt.shift
21
+ if part =~ /([0-9 #+\-*.]*)([bcdEefGgiopsuXxP])(.*)/ and Regexp.last_match(2) == 'P'
22
+ keys = Regexp.last_match(1) || ''
23
+ str = Regexp.last_match(3) || ''
24
+ if keys =~ /(-)?([0-9*]*)\.?([0-9*]*)(\+?)/
25
25
  value = args.shift
26
- indent = ' ' * ($2 == '*' ? args.shift : $2).to_i
27
- plain = value && value.to_p(
28
- :padding => ($3 == '*' ? args.shift : $3.empty? ? 1 : $3).to_i,
29
- :be => $4.empty? ? nil : true) || ''
30
- nformat += ($1 ? plain + indent : indent + plain) + str
26
+ indent = ' ' * (Regexp.last_match(2) == '*' ? args.shift : Regexp.last_match(2)).to_i
27
+ plain =
28
+ value &&
29
+ value.to_p(
30
+ padding:
31
+ (Regexp.last_match(3) == '*' ? args.shift : Regexp.last_match(3).empty? ? 1 : Regexp.last_match(3))
32
+ .to_i,
33
+ be: Regexp.last_match(4).empty? ? nil : true
34
+ ) || ''
35
+ nformat += (Regexp.last_match(1) ? plain + indent : indent + plain) + str
31
36
  else
32
37
  nformat += '%' + keys + 'c' + str
33
38
  nargs.push args.shift
34
39
  end
35
40
  else
36
41
  nformat += '%' + part
37
- l = $1 =~ /\*/ ? 2 : 1
42
+ l = Regexp.last_match(1) =~ /\*/ ? 2 : 1
38
43
  while l > 0
39
44
  nargs.push args.shift
40
45
  l -= 1
@@ -43,57 +48,59 @@ module Kernel
43
48
  end
44
49
  __sprintf__(nformat, *nargs).to_p
45
50
  end
46
-
47
51
  end
48
52
 
49
53
  class String
50
54
  def scanf_re(format)
51
- fss = StringScanner.new(format) # TODO remove scanner in favor of match
55
+ fss = StringScanner.new(format) # TODO: remove scanner in favor of match
52
56
  nformat = ''
53
57
 
54
58
  pos = 0
55
59
  argfs = []
56
60
  while fss.scan_until(/%([0-9 #+\-*]*)(?:\.([0-9]+)(\+)?)?([bcdefgiosuxr])/)
57
- argfs << [ fss[1], fss[2], fss[3], fss[4] ]
58
- # TODO add performing the special case in fss[1]
59
- nformat += fss.pre_match[pos..-1].to_res + case fss[4]
60
- when 'x'
61
- '(?:0[xX])?([a-fA-F0-9]+)'
62
- when 'i'
63
- '([+\-]?[0-9]+)'
64
- when 'u'
65
- '([0-9]+)'
66
- when 'e'
67
- '([+\-]?[0-9]+[eE][+\-]?[0-9]+)'
68
- when 'f'
69
- '([+\-]?[0-9]+\.[0-9]*)'
70
- when 'g'
71
- '([+\-]?[0-9]+(?:[eE][+\-]?[0-9]+|\.[0-9]*))'
72
- when 'c'
73
- fss[2] ? "(.{1,#{fss[2]}})" : "(.)"
74
- when 'b'
75
- '([01]+)b?'
76
- when 'o'
77
- '0([0-9]+)'
78
- when 'd'
79
- '([+\-]?(?:0X)?[A-F0-9.+]+)'
80
- when 's'
81
- '(.+)'
82
- when 'r'
83
- '([IVXLCDMivxlcdm]+)'
84
- end
61
+ argfs << [fss[1], fss[2], fss[3], fss[4]]
62
+
63
+ #  TODO add performing the special case in fss[1]
64
+ nformat +=
65
+ fss.pre_match[pos..-1].to_res +
66
+ case fss[4]
67
+ when 'x'
68
+ '(?:0[xX])?([a-fA-F0-9]+)'
69
+ when 'i'
70
+ '([+\-]?[0-9]+)'
71
+ when 'u'
72
+ '([0-9]+)'
73
+ when 'e'
74
+ '([+\-]?[0-9]+[eE][+\-]?[0-9]+)'
75
+ when 'f'
76
+ '([+\-]?[0-9]+\.[0-9]*)'
77
+ when 'g'
78
+ '([+\-]?[0-9]+(?:[eE][+\-]?[0-9]+|\.[0-9]*))'
79
+ when 'c'
80
+ fss[2] ? "(.{1,#{fss[2]}})" : '(.)'
81
+ when 'b'
82
+ '([01]+)b?'
83
+ when 'o'
84
+ '0([0-9]+)'
85
+ when 'd'
86
+ '([+\-]?(?:0X)?[A-F0-9.+]+)'
87
+ when 's'
88
+ '(.+)'
89
+ when 'r'
90
+ '([IVXLCDMivxlcdm]+)'
91
+ end
85
92
 
86
93
  pos = fss.pos
87
94
  end
88
95
 
89
96
  nformat += fss.rest
90
97
 
91
- [ /#{nformat}/, argfs ]
98
+ [/#{nformat}/, argfs]
92
99
  end
93
100
 
94
- (alias :__scanf__ :scanf) if self.instance_methods(false).include?(:scanf)
101
+ (alias __scanf__ scanf) if instance_methods(false).include?(:scanf)
95
102
  def scanf(format, &block)
96
- (re, argfs) = scanf_re(format)
103
+ re, argfs = scanf_re(format)
97
104
 
98
105
  ss = StringScanner.new(self)
99
106
  res = []
@@ -102,29 +109,32 @@ class String
102
109
  argfs.each_index do |i|
103
110
  argf = argfs[i]
104
111
  value = ss[i + 1]
105
- rline << case argf[3]
106
- when 'x'
107
- value.to_i(16)
108
- when /[diu]/
109
- value.to_i
110
- when /[efg]/
111
- value.to_f
112
- when 'c'
113
- argf[2] ? value.to_i(:be) : value.to_i
114
- when 'b'
115
- value.to_i(2)
116
- when 'o'
117
- value.to_i(8)
118
- when 's'
119
- value
120
- when 'r'
121
- value.rom
122
- end
112
+ rline <<
113
+ case argf[3]
114
+ when 'x'
115
+ value.to_i(16)
116
+ when /[diu]/
117
+ value.to_i
118
+ when /[efg]/
119
+ value.to_f
120
+ when 'c'
121
+ argf[2] ? value.to_i(:be) : value.to_i
122
+ when 'b'
123
+ value.to_i(2)
124
+ when 'o'
125
+ value.to_i(8)
126
+ when 's'
127
+ value
128
+ when 'r'
129
+ value.rom
130
+ end
123
131
  end
124
132
 
125
- if block_given?
133
+ if block
126
134
  pass = []
127
- (1..block.arity).each do |i| pass << "rline[#{i}]" end
135
+ (1..block.arity).each do |i|
136
+ pass << "rline[#{i}]"
137
+ end
128
138
  eval "yield(#{pass.join(', ')})"
129
139
  end
130
140
 
@@ -144,17 +154,14 @@ class String
144
154
  ostr[0...ss.pre_match.size - pos] = ss.pre_match[pos..-1]
145
155
  pos = ss.pos
146
156
 
147
- if ss.post_match[0] == "\n"[0]
148
- res = ostr
149
- pos += 1
150
- ostr = ''
151
- end
157
+ next unless ss.post_match[0] == "\n"[0]
152
158
 
159
+ res = ostr
160
+ pos += 1
161
+ ostr = ''
153
162
  end
154
163
 
155
164
  ostr[0...ss.rest.size] = ss.rest
156
165
  res + ostr
157
166
  end
158
167
  end
159
-
160
-
data/lib/rdoba/merge.rb CHANGED
@@ -1,21 +1,21 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Rdoba::Merge
4
- TARGET = :Hash
4
+ TARGET = :Hash
5
5
 
6
- def deep_merge source, dest
7
- dup = dest.dup
8
- source.each do |key, value|
9
- newvalue = dup.delete key
10
- case newvalue
11
- when Hash
12
- value.deep_merge newvalue
13
- when Array
14
- value |= newvalue
15
- when NilClass
16
- else
17
- raise
18
- end
6
+ def deep_merge(source, dest)
7
+ dup = dest.dup
8
+ source.each do |key, value|
9
+ newvalue = dup.delete key
10
+ case newvalue
11
+ when Hash
12
+ value.deep_merge newvalue
13
+ when Array
14
+ value |= newvalue
15
+ when NilClass
16
+ else
17
+ raise
19
18
  end
20
- end
19
+ end
20
+ end
21
21
  end
@@ -1,11 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rdoba::Mixin::Time
2
- require 'ffi/stat'
4
+ require 'ffi/stat'
3
5
 
4
- def mtime file
5
- FFI::Stat.stat( file )[:st_mtimespec].to_time ; end
6
+ def mtime(file)
7
+ FFI::Stat.stat(file)[:st_mtimespec].to_time
8
+ end
6
9
 
7
- def atime file
8
- FFI::Stat.stat( file )[:st_atimespec].to_time ; end
10
+ def atime(file)
11
+ FFI::Stat.stat(file)[:st_atimespec].to_time
12
+ end
9
13
 
10
- def ctime file
11
- FFI::Stat.stat( file )[:st_ctimespec].to_time ; end ; end
14
+ def ctime(file)
15
+ FFI::Stat.stat(file)[:st_ctimespec].to_time
16
+ end
17
+ end
@@ -1,6 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rdoba::Mixin::TryObject
2
- def try method, *args, default: nil
3
- if self.respond_to?( method )
4
- self.send( method, *args )
5
- else
6
- default ; end ; end ; end
4
+ def try(method, *args, default: nil)
5
+ if respond_to?(method)
6
+ send(method, *args)
7
+ else
8
+ default
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rdoba::Mixin::TryObject
2
- def try method, *args
3
- if self.respond_to?( method )
4
- self.send( method, *args ) ; end ; end ; end
4
+ def try(method, *args)
5
+ if respond_to?(method)
6
+ send(method, *args)
7
+ end
8
+ end
9
+ end
@@ -1,21 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'timeout'
2
4
 
3
5
  module Rdoba::Mixin::Wait_ifKernel
4
-
5
- ##
6
- # +wait_if+ waits for +timeout+ second to the condition passed via block,
7
- # and in case if it failed, returns false, otherwise true. +timeout+ can
8
- # be a float or integer number of seconds, but if passed 0 or nil it waits
9
- # forever. Default value is 6 seconds. Example:
10
- #
11
- # wait_if(5) { sleep 2; true } # => true
12
- # wait_if(5) { sleep 10; true } # => false
13
- #
14
- def wait_if timeout = 6
15
- begin
16
- Timeout.timeout( timeout ) do
17
- while yield() do
18
- sleep( 0.1 ) ; end ; end
19
- true
20
- rescue Timeout::Error
21
- false ; end ; end ; end
6
+ ##
7
+ # +wait_if+ waits for +timeout+ second to the condition passed via block,
8
+ # and in case if it failed, returns false, otherwise true. +timeout+ can
9
+ # be a float or integer number of seconds, but if passed 0 or nil it waits
10
+ # forever. Default value is 6 seconds. Example:
11
+ #
12
+ # wait_if(5) { sleep 2; true } # => true
13
+ # wait_if(5) { sleep 10; true } # => false
14
+ #
15
+ def wait_if(timeout = 6)
16
+ begin
17
+ Timeout.timeout(timeout) do
18
+ while yield()
19
+ sleep(0.1)
20
+ end
21
+ end
22
+ true
23
+ rescue Timeout::Error
24
+ false
25
+ end
26
+ end
27
+ end