Skip to content

Commit 359acdc

Browse files
committedOct 29, 2013
Small comsmetic cleanup in hash.rb
1 parent baceb57 commit 359acdc

File tree

1 file changed

+63
-80
lines changed

1 file changed

+63
-80
lines changed
 

‎corelib/hash.rb

+63-80
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def self.[](*objs)
6464

6565
def self.allocate
6666
%x{
67-
var hash = new #{self}._alloc;
67+
var hash = new self._alloc;
6868
hash.map = {};
6969
hash.keys = [];
7070
return hash;
@@ -75,44 +75,44 @@ def initialize(defaults = undefined, &block)
7575
%x{
7676
if (defaults != null) {
7777
if (defaults.constructor == Object) {
78-
var map = #{self}.map, keys = #{self}.keys;
78+
var map = self.map, keys = self.keys;
7979
8080
for (var key in defaults) {
8181
keys.push(key);
8282
map[key] = defaults[key];
8383
}
8484
}
8585
else {
86-
#{self}.none = defaults;
86+
self.none = defaults;
8787
}
8888
}
8989
else if (block !== nil) {
90-
#{self}.proc = block;
90+
self.proc = block;
9191
}
9292
93-
return #{self};
93+
return self;
9494
}
9595
end
9696

9797
def ==(other)
9898
%x{
99-
if (#{self} === other) {
99+
if (self === other) {
100100
return true;
101101
}
102102
103103
if (!other.map || !other.keys) {
104104
return false;
105105
}
106106
107-
if (#{self}.keys.length !== other.keys.length) {
107+
if (self.keys.length !== other.keys.length) {
108108
return false;
109109
}
110110
111-
var map = #{self}.map,
111+
var map = self.map,
112112
map2 = other.map;
113113
114-
for (var i = 0, length = #{self}.keys.length; i < length; i++) {
115-
var key = #{self}.keys[i], obj = map[key], obj2 = map2[key];
114+
for (var i = 0, length = self.keys.length; i < length; i++) {
115+
var key = self.keys[i], obj = map[key], obj2 = map2[key];
116116
117117
if (#{`obj` != `obj2`}) {
118118
return false;
@@ -125,28 +125,28 @@ def ==(other)
125125

126126
def [](key)
127127
%x{
128-
var map = #{self}.map;
128+
var map = self.map;
129129
130130
if ($hasOwn.call(map, key)) {
131131
return map[key];
132132
}
133133
134-
var proc = #{@proc};
134+
var proc = #@proc;
135135
136136
if (proc !== nil) {
137137
return #{ `proc`.call self, key };
138138
}
139139
140-
return #{@none};
140+
return #@none;
141141
}
142142
end
143143

144144
def []=(key, value)
145145
%x{
146-
var map = #{self}.map;
146+
var map = self.map;
147147
148148
if (!$hasOwn.call(map, key)) {
149-
#{self}.keys.push(key);
149+
self.keys.push(key);
150150
}
151151
152152
map[key] = value;
@@ -157,13 +157,13 @@ def []=(key, value)
157157

158158
def assoc(object)
159159
%x{
160-
var keys = #{self}.keys, key;
160+
var keys = self.keys, key;
161161
162162
for (var i = 0, length = keys.length; i < length; i++) {
163163
key = keys[i];
164164
165165
if (#{`key` == object}) {
166-
return [key, #{self}.map[key]];
166+
return [key, self.map[key]];
167167
}
168168
}
169169
@@ -173,9 +173,9 @@ def assoc(object)
173173

174174
def clear
175175
%x{
176-
#{self}.map = {};
177-
#{self}.keys = [];
178-
return #{self};
176+
self.map = {};
177+
self.keys = [];
178+
return self;
179179
}
180180
end
181181

@@ -185,13 +185,13 @@ def clone
185185
186186
result.map = {}; result.keys = [];
187187
188-
var map = #{self}.map,
188+
var map = self.map,
189189
map2 = result.map,
190190
keys2 = result.keys;
191191
192-
for (var i = 0, length = #{self}.keys.length; i < length; i++) {
193-
keys2.push(#{self}.keys[i]);
194-
map2[#{self}.keys[i]] = map[#{self}.keys[i]];
192+
for (var i = 0, length = self.keys.length; i < length; i++) {
193+
keys2.push(self.keys[i]);
194+
map2[self.keys[i]] = map[self.keys[i]];
195195
}
196196
197197
return result;
@@ -216,11 +216,11 @@ def default_proc=(proc)
216216

217217
def delete(key)
218218
%x{
219-
var map = #{self}.map, result = map[key];
219+
var map = self.map, result = map[key];
220220
221221
if (result != null) {
222222
delete map[key];
223-
#{self}.keys.$delete(key);
223+
self.keys.$delete(key);
224224
225225
return result;
226226
}
@@ -233,7 +233,7 @@ def delete_if(&block)
233233
return enum_for :delete_if unless block
234234

235235
%x{
236-
var map = #{self}.map, keys = #{self}.keys, value;
236+
var map = self.map, keys = self.keys, value;
237237
238238
for (var i = 0, length = keys.length; i < length; i++) {
239239
var key = keys[i], obj = map[key];
@@ -251,7 +251,7 @@ def delete_if(&block)
251251
}
252252
}
253253
254-
return #{self};
254+
return self;
255255
}
256256
end
257257

@@ -281,7 +281,7 @@ def each_key(&block)
281281
return enum_for :each_key unless block
282282

283283
%x{
284-
var keys = #{self}.keys;
284+
var keys = self.keys;
285285
286286
for (var i = 0, length = keys.length; i < length; i++) {
287287
var key = keys[i];
@@ -291,7 +291,7 @@ def each_key(&block)
291291
}
292292
}
293293
294-
return #{self};
294+
return self;
295295
}
296296
end
297297

@@ -301,27 +301,27 @@ def each_value(&block)
301301
return enum_for :each_value unless block
302302

303303
%x{
304-
var map = #{self}.map, keys = #{self}.keys;
304+
var map = self.map, keys = self.keys;
305305
306306
for (var i = 0, length = keys.length; i < length; i++) {
307307
if (block(map[keys[i]]) === $breaker) {
308308
return $breaker.$v;
309309
}
310310
}
311311
312-
return #{self};
312+
return self;
313313
}
314314
end
315315

316316
def empty?
317-
`#{self}.keys.length === 0`
317+
`self.keys.length === 0`
318318
end
319319

320320
alias eql? ==
321321

322322
def fetch(key, defaults = undefined, &block)
323323
%x{
324-
var value = #{self}.map[key];
324+
var value = self.map[key];
325325
326326
if (value != null) {
327327
return value;
@@ -347,7 +347,7 @@ def fetch(key, defaults = undefined, &block)
347347

348348
def flatten(level=undefined)
349349
%x{
350-
var map = #{self}.map, keys = #{self}.keys, result = [];
350+
var map = self.map, keys = self.keys, result = [];
351351
352352
for (var i = 0, length = keys.length; i < length; i++) {
353353
var key = keys[i], value = map[key];
@@ -372,13 +372,13 @@ def flatten(level=undefined)
372372
end
373373

374374
def has_key?(key)
375-
`$hasOwn.call(#{self}.map, key)`
375+
`$hasOwn.call(self.map, key)`
376376
end
377377

378378
def has_value?(value)
379379
%x{
380-
for (var assoc in #{self}.map) {
381-
if (#{`#{self}.map[assoc]` == value}) {
380+
for (var assoc in self.map) {
381+
if (#{`self.map[assoc]` == value}) {
382382
return true;
383383
}
384384
}
@@ -388,14 +388,14 @@ def has_value?(value)
388388
end
389389

390390
def hash
391-
`#{self}._id`
391+
`self._id`
392392
end
393393

394394
alias include? has_key?
395395

396396
def index(object)
397397
%x{
398-
var map = #{self}.map, keys = #{self}.keys;
398+
var map = self.map, keys = self.keys;
399399
400400
for (var i = 0, length = keys.length; i < length; i++) {
401401
var key = keys[i];
@@ -411,7 +411,7 @@ def index(object)
411411

412412
def indexes(*keys)
413413
%x{
414-
var result = [], map = #{self}.map, val;
414+
var result = [], map = self.map, val;
415415
416416
for (var i = 0, length = keys.length; i < length; i++) {
417417
var key = keys[i], val = map[key];
@@ -420,7 +420,7 @@ def indexes(*keys)
420420
result.push(val);
421421
}
422422
else {
423-
result.push(#{self}.none);
423+
result.push(self.none);
424424
}
425425
}
426426
@@ -432,12 +432,12 @@ def indexes(*keys)
432432

433433
def inspect
434434
%x{
435-
var inspect = [], keys = #{self}.keys, map = #{self}.map;
435+
var inspect = [], keys = self.keys, map = self.map;
436436
437437
for (var i = 0, length = keys.length; i < length; i++) {
438438
var key = keys[i], val = map[key];
439439
440-
if (val === #{self}) {
440+
if (val === self) {
441441
inspect.push(#{`key`.inspect} + '=>' + '{...}');
442442
} else {
443443
inspect.push(#{`key`.inspect} + '=>' + #{`map[key]`.inspect});
@@ -450,7 +450,7 @@ def inspect
450450

451451
def invert
452452
%x{
453-
var result = $hash(), keys = #{self}.keys, map = #{self}.map,
453+
var result = $hash(), keys = self.keys, map = self.map,
454454
keys2 = result.keys, map2 = result.map;
455455
456456
for (var i = 0, length = keys.length; i < length; i++) {
@@ -468,7 +468,7 @@ def keep_if(&block)
468468
return enum_for :keep_if unless block
469469

470470
%x{
471-
var map = #{self}.map, keys = #{self}.keys, value;
471+
var map = self.map, keys = self.keys, value;
472472
473473
for (var i = 0, length = keys.length; i < length; i++) {
474474
var key = keys[i], obj = map[key];
@@ -486,7 +486,7 @@ def keep_if(&block)
486486
}
487487
}
488488
489-
return #{self};
489+
return self;
490490
}
491491
end
492492

@@ -495,22 +495,18 @@ def keep_if(&block)
495495
alias key? has_key?
496496

497497
def keys
498-
%x{
499-
return #{self}.keys.slice(0);
500-
}
498+
`self.keys.slice(0)`
501499
end
502500

503501
def length
504-
%x{
505-
return #{self}.keys.length;
506-
}
502+
`self.keys.length`
507503
end
508504

509505
alias member? has_key?
510506

511507
def merge(other, &block)
512508
%x{
513-
var keys = #{self}.keys, map = #{self}.map,
509+
var keys = self.keys, map = self.map,
514510
result = $hash(), keys2 = result.keys, map2 = result.map;
515511
516512
for (var i = 0, length = keys.length; i < length; i++) {
@@ -553,7 +549,7 @@ def merge(other, &block)
553549

554550
def merge!(other, &block)
555551
%x{
556-
var keys = #{self}.keys, map = #{self}.map,
552+
var keys = self.keys, map = self.map,
557553
keys2 = other.keys, map2 = other.map;
558554
559555
if (block === nil) {
@@ -581,13 +577,13 @@ def merge!(other, &block)
581577
}
582578
}
583579
584-
return #{self};
580+
return self;
585581
}
586582
end
587583

588584
def rassoc(object)
589585
%x{
590-
var keys = #{self}.keys, map = #{self}.map;
586+
var keys = self.keys, map = self.map;
591587
592588
for (var i = 0, length = keys.length; i < length; i++) {
593589
var key = keys[i], obj = map[key];
@@ -605,7 +601,7 @@ def reject(&block)
605601
return enum_for :reject unless block
606602

607603
%x{
608-
var keys = #{self}.keys, map = #{self}.map,
604+
var keys = self.keys, map = self.map,
609605
result = $hash(), map2 = result.map, keys2 = result.keys;
610606
611607
for (var i = 0, length = keys.length; i < length; i++) {
@@ -627,23 +623,23 @@ def reject(&block)
627623

628624
def replace(other)
629625
%x{
630-
var map = #{self}.map = {}, keys = #{self}.keys = [];
626+
var map = self.map = {}, keys = self.keys = [];
631627
632628
for (var i = 0, length = other.keys.length; i < length; i++) {
633629
var key = other.keys[i];
634630
keys.push(key);
635631
map[key] = other.map[key];
636632
}
637633
638-
return #{self};
634+
return self;
639635
}
640636
end
641637

642638
def select(&block)
643639
return enum_for :select unless block
644640

645641
%x{
646-
var keys = #{self}.keys, map = #{self}.map,
642+
var keys = self.keys, map = self.map,
647643
result = $hash(), map2 = result.map, keys2 = result.keys;
648644
649645
for (var i = 0, length = keys.length; i < length; i++) {
@@ -667,7 +663,7 @@ def select!(&block)
667663
return enum_for :select! unless block
668664

669665
%x{
670-
var map = #{self}.map, keys = #{self}.keys, value, result = nil;
666+
var map = self.map, keys = self.keys, value, result = nil;
671667
672668
for (var i = 0, length = keys.length; i < length; i++) {
673669
var key = keys[i], obj = map[key];
@@ -682,7 +678,7 @@ def select!(&block)
682678
683679
length--;
684680
i--;
685-
result = #{self}
681+
result = self
686682
}
687683
}
688684
@@ -692,7 +688,7 @@ def select!(&block)
692688

693689
def shift
694690
%x{
695-
var keys = #{self}.keys, map = #{self}.map;
691+
var keys = self.keys, map = self.map;
696692
697693
if (keys.length) {
698694
var key = keys[0], obj = map[key];
@@ -713,7 +709,7 @@ def shift
713709

714710
def to_a
715711
%x{
716-
var keys = #{self}.keys, map = #{self}.map, result = [];
712+
var keys = self.keys, map = self.map, result = [];
717713
718714
for (var i = 0, length = keys.length; i < length; i++) {
719715
var key = keys[i];
@@ -756,26 +752,13 @@ def to_n
756752

757753
alias update merge!
758754

759-
def value?(value)
760-
%x{
761-
var map = #{self}.map;
762-
763-
for (var assoc in map) {
764-
var v = map[assoc];
765-
if (#{`v` == value}) {
766-
return true;
767-
}
768-
}
769-
770-
return false;
771-
}
772-
end
755+
alias value? has_value?
773756

774757
alias values_at indexes
775758

776759
def values
777760
%x{
778-
var map = #{self}.map,
761+
var map = self.map,
779762
result = [];
780763
781764
for (var key in map) {

0 commit comments

Comments
 (0)
Please sign in to comment.