Skip to content

Commit b7fb7b9

Browse files
committed
Make some changes suggested by Flay
1 parent 87a1555 commit b7fb7b9

File tree

9 files changed

+137
-226
lines changed

9 files changed

+137
-226
lines changed

lib/truffle/bigdecimal.rb

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,36 @@ def coerce(other)
101101
[BigDecimal(other, 20), self]
102102
end
103103

104-
# TODO (pitr 28-may-2015): compare with pure Java versions
105104
def >(other)
106-
unless comp = (self <=> other)
107-
return false if nan? || (BigDecimal === other && other.nan?)
108-
raise ArgumentError, "comparison of #{self.class} with #{other.class}"
109-
end
110-
111-
comp > 0
105+
comp_helper(other, :>)
112106
end
113107

114108
def >=(other)
115-
unless comp = (self <=> other)
116-
return false if nan? || (BigDecimal === other && other.nan?)
117-
raise ArgumentError, "comparison of #{self.class} with #{other.class}"
118-
end
119-
120-
comp >= 0
109+
comp_helper(other, :>=)
121110
end
122111

123112
def <(other)
124-
unless comp = (self <=> other)
125-
return false if nan? || (BigDecimal === other && other.nan?)
126-
raise ArgumentError, "comparison of #{self.class} with #{other.class}"
127-
end
128-
129-
comp < 0
113+
comp_helper(other, :<)
130114
end
131115

132116
def <=(other)
133-
unless comp = (self <=> other)
134-
return false if nan? || (BigDecimal === other && other.nan?)
135-
raise ArgumentError, "comparison of #{self.class} with #{other.class}"
136-
end
117+
comp_helper(other, :<=)
118+
end
137119

138-
comp <= 0
120+
def comp_helper(other, op)
121+
comp = (self <=> other)
122+
if comp
123+
# cmp >= 0, comp < 0, etc
124+
comp.send(op, 0)
125+
else
126+
if nan? || (BigDecimal === other && other.nan?)
127+
return false
128+
else
129+
raise ArgumentError, "comparison of #{self.class} with #{other.class}"
130+
end
131+
end
139132
end
133+
private :comp_helper
140134

141135
def nonzero?
142136
zero? ? nil : self

lib/truffle/socket/addrinfo.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -338,34 +338,31 @@ def ipv6_sitelocal?
338338
end
339339

340340
def ipv6_mc_global?
341-
return false unless ipv6?
342-
bytes = Truffle::Socket::Foreign.ip_to_bytes(afamily, ip_address)
343-
bytes[0] == 0xff && bytes[1] & 0xf == 0xe
341+
ipv6_mc_flag?(0xe)
344342
end
345343

346344
def ipv6_mc_linklocal?
347-
return false unless ipv6?
348-
bytes = Truffle::Socket::Foreign.ip_to_bytes(afamily, ip_address)
349-
bytes[0] == 0xff && bytes[1] & 0xf == 0x2
345+
ipv6_mc_flag?(0x2)
350346
end
351347

352348
def ipv6_mc_nodelocal?
353-
return false unless ipv6?
354-
bytes = Truffle::Socket::Foreign.ip_to_bytes(afamily, ip_address)
355-
bytes[0] == 0xff && bytes[1] & 0xf == 0x1
349+
ipv6_mc_flag?(0x1)
356350
end
357351

358352
def ipv6_mc_orglocal?
359-
return false unless ipv6?
360-
bytes = Truffle::Socket::Foreign.ip_to_bytes(afamily, ip_address)
361-
bytes[0] == 0xff && bytes[1] & 0xf == 0x8
353+
ipv6_mc_flag?(0x8)
362354
end
363355

364356
def ipv6_mc_sitelocal?
357+
ipv6_mc_flag?(0x5)
358+
end
359+
360+
def ipv6_mc_flag?(value)
365361
return false unless ipv6?
366362
bytes = Truffle::Socket::Foreign.ip_to_bytes(afamily, ip_address)
367-
bytes[0] == 0xff && bytes[1] & 0xf == 0x5
363+
bytes[0] == 0xff && bytes[1] & 0xf == value
368364
end
365+
private :ipv6_mc_flag?
369366

370367
def ipv6_to_ipv4
371368
return unless ipv6?

src/main/ruby/core/enumerable.rb

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -672,24 +672,7 @@ def first(n=undefined)
672672

673673
def min(n = undefined, &block)
674674
return min_n(n, &block) if !undefined.equal?(n) && !n.nil?
675-
min = undefined
676-
each do
677-
o = Truffle.single_block_arg
678-
if undefined.equal? min
679-
min = o
680-
else
681-
comp = block_given? ? yield(o, min) : o <=> min
682-
unless comp
683-
raise ArgumentError, "comparison of #{o.class} with #{min} failed"
684-
end
685-
686-
if Comparable.compare_int(comp) < 0
687-
min = o
688-
end
689-
end
690-
end
691-
692-
undefined.equal?(min) ? nil : min
675+
min_max(-1, &block)
693676
end
694677

695678
def min_n(n, &block)
@@ -702,33 +685,38 @@ def min_n(n, &block)
702685

703686
def max(n = undefined, &block)
704687
return max_n(n, &block) if !undefined.equal?(n) && !n.nil?
705-
max = undefined
688+
min_max(+1, &block)
689+
end
690+
691+
def max_n(n, &block)
692+
raise ArgumentError, "negative size #{n}" if n < 0
693+
return [] if n == 0
694+
695+
self.sort(&block).last(n).reverse
696+
end
697+
private :max_n
698+
699+
def min_max(relative)
700+
chosen = undefined
706701
each do
707702
o = Truffle.single_block_arg
708-
if undefined.equal? max
709-
max = o
703+
if undefined.equal? chosen
704+
chosen = o
710705
else
711-
comp = block_given? ? yield(o, max) : o <=> max
706+
comp = block_given? ? yield(o, chosen) : o <=> chosen
712707
unless comp
713-
raise ArgumentError, "comparison of #{o.class} with #{max} failed"
708+
raise ArgumentError, "comparison of #{o.class} with #{chosen} failed"
714709
end
715-
716-
if Comparable.compare_int(comp) > 0
717-
max = o
710+
711+
if (Comparable.compare_int(comp) <=> 0) == relative
712+
chosen = o
718713
end
719714
end
720715
end
721716

722-
undefined.equal?(max) ? nil : max
723-
end
724-
725-
def max_n(n, &block)
726-
raise ArgumentError, "negative size #{n}" if n < 0
727-
return [] if n == 0
728-
729-
self.sort(&block).last(n).reverse
717+
undefined.equal?(chosen) ? nil : chosen
730718
end
731-
private :max_n
719+
private :min_max
732720

733721
private def max_by_n(n, &block)
734722
n = Truffle::Type.rb_num2long(n)

src/main/ruby/core/file.rb

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,23 @@ def self.basename(path, ext=undefined)
203203
path
204204
end
205205

206+
def self.query_stat_mode(path)
207+
path = Truffle::Type.coerce_to_path(path)
208+
Truffle::POSIX.truffleposix_stat_mode(path)
209+
end
210+
211+
private_class_method :query_stat_mode
212+
206213
##
207214
# Returns true if the named file is a block device.
208215
def self.blockdev?(path)
209-
path = Truffle::Type.coerce_to_path(path)
210-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
211-
Truffle::StatOperations.blockdev?(mode)
216+
Truffle::StatOperations.blockdev?(query_stat_mode(path))
212217
end
213218

214219
##
215220
# Returns true if the named file is a character device.
216221
def self.chardev?(path)
217-
path = Truffle::Type.coerce_to_path(path)
218-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
219-
Truffle::StatOperations.chardev?(mode)
222+
Truffle::StatOperations.chardev?(query_stat_mode(path))
220223
end
221224

222225
##
@@ -581,9 +584,7 @@ def self.extname(path)
581584
##
582585
# Returns true if the named file exists and is a regular file.
583586
def self.file?(path)
584-
path = Truffle::Type.coerce_to_path(path)
585-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
586-
Truffle::StatOperations.file?(mode)
587+
Truffle::StatOperations.file?(query_stat_mode(path))
587588
end
588589

589590
def self.braces(pattern, flags=0, patterns=[])
@@ -890,9 +891,7 @@ def self.path(obj)
890891
##
891892
# Returns true if the named file is a pipe.
892893
def self.pipe?(path)
893-
path = Truffle::Type.coerce_to_path(path)
894-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
895-
Truffle::StatOperations.pipe?(mode)
894+
Truffle::StatOperations.pipe?(query_stat_mode(path))
896895
end
897896

898897
##
@@ -1033,9 +1032,7 @@ def self.size?(io_or_path)
10331032
##
10341033
# Returns true if the named file is a socket.
10351034
def self.socket?(path)
1036-
path = Truffle::Type.coerce_to_path(path)
1037-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
1038-
Truffle::StatOperations.socket?(mode)
1035+
Truffle::StatOperations.socket?(query_stat_mode(path))
10391036
end
10401037

10411038
##
@@ -1155,17 +1152,11 @@ def self.utime(atime, mtime, *paths)
11551152
end
11561153

11571154
def self.world_readable?(path)
1158-
path = Truffle::Type.coerce_to_path path
1159-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
1160-
1161-
Truffle::StatOperations.world_readable?(mode)
1155+
Truffle::StatOperations.world_readable?(query_stat_mode(path))
11621156
end
11631157

11641158
def self.world_writable?(path)
1165-
path = Truffle::Type.coerce_to_path path
1166-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
1167-
1168-
Truffle::StatOperations.world_writable?(mode)
1159+
Truffle::StatOperations.world_writable?(query_stat_mode(path))
11691160
end
11701161

11711162
##
@@ -1208,25 +1199,19 @@ def self.owned?(file_name)
12081199
##
12091200
# Returns true if the named file has the setgid bit set.
12101201
def self.setgid?(path)
1211-
path = Truffle::Type.coerce_to_path(path)
1212-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
1213-
Truffle::StatOperations.setgid?(mode)
1202+
Truffle::StatOperations.setgid?(query_stat_mode(path))
12141203
end
12151204

12161205
##
12171206
# Returns true if the named file has the setuid bit set.
12181207
def self.setuid?(path)
1219-
path = Truffle::Type.coerce_to_path(path)
1220-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
1221-
Truffle::StatOperations.setuid?(mode)
1208+
Truffle::StatOperations.setuid?(query_stat_mode(path))
12221209
end
12231210

12241211
##
12251212
# Returns true if the named file has the sticky bit set.
12261213
def self.sticky?(path)
1227-
path = Truffle::Type.coerce_to_path(path)
1228-
mode = Truffle::POSIX.truffleposix_stat_mode(path)
1229-
Truffle::StatOperations.sticky?(mode)
1214+
Truffle::StatOperations.sticky?(query_stat_mode(path))
12301215
end
12311216

12321217
class << self

src/main/ruby/core/hash.rb

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,39 +115,19 @@ def <=(other)
115115
self.class.contains_all_internal(self, other)
116116
end
117117

118-
119118
def ==(other)
120-
return true if self.equal? other
121-
unless other.kind_of? Hash
122-
return false unless other.respond_to? :to_hash
123-
return other == self
124-
end
125-
126-
return false unless other.size == size
127-
128-
Thread.detect_recursion self, other do
129-
each_pair do |key, value|
130-
other_value = other._get_or_undefined(key)
131-
132-
# Other doesn't even have this key
133-
return false if undefined.equal?(other_value)
134-
135-
# Order of the comparison matters! We must compare our value with
136-
# the other Hash's value and not the other way around.
137-
unless Truffle::Type.object_equal(value, other_value) or value == other_value
138-
return false
139-
end
140-
end
141-
end
142-
true
119+
eql_op(:==, other)
143120
end
144121

145122
def eql?(other)
146-
# Just like ==, but uses eql? to compare values.
123+
eql_op(:eql?, other)
124+
end
125+
126+
def eql_op(op, other)
147127
return true if self.equal? other
148128
unless other.kind_of? Hash
149129
return false unless other.respond_to? :to_hash
150-
return other.eql?(self)
130+
return other.send(op, self)
151131
end
152132

153133
return false unless other.size == size
@@ -161,13 +141,14 @@ def eql?(other)
161141

162142
# Order of the comparison matters! We must compare our value with
163143
# the other Hash's value and not the other way around.
164-
unless Truffle::Type.object_equal(value, other_value) or value.eql?(other_value)
144+
unless Truffle::Type.object_equal(value, other_value) or value.send(op, other_value)
165145
return false
166146
end
167147
end
168148
end
169149
true
170150
end
151+
private :eql_op
171152

172153
def >(other)
173154
other = Truffle::Type.coerce_to(other, Hash, :to_hash)

0 commit comments

Comments
 (0)