Skip to content

Commit

Permalink
Implement Hash.push, and add S32-hash/push.t to spectest.data
Browse files Browse the repository at this point in the history
All of these new tests pass except one, which trips over a Null PMC access in
type() from is_deeply in Test.pm
  • Loading branch information
moritz committed Apr 10, 2009
1 parent c44a05b commit 7b2c47c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/setting/Hash.pm
Expand Up @@ -4,6 +4,38 @@ class Hash is also {
(%result).{%hash.values} = %hash.keys;
%result;
}

multi method push (*@values) {
my $previous;
my $has_previous;
for @values -> $e {
if $has_previous {
self!push_construct($previous, $e);
$has_previous = 0;
} elsif $e ~~ Pair {
self!push_construct($e.key, $e.value);
} else {
$previous = $e;
$has_previous = 1;
}
}
if $has_previous {
warn "Trailing item in Hash.push";
}
}

# push a value onto a hash item, constructing an array if necessary
method !push_construct (Object $key, Object $value) {
if self.exists($key) {
if self.{$key} ~~ Array {
self.{$key}.push: $value;
} else {
self.{$key} = [ self.{$key}, $value];
}
} else {
self.{$key} = $value;
}
}
}

multi reverse(%hash) {
Expand Down
1 change: 1 addition & 0 deletions t/spectest.data
Expand Up @@ -321,6 +321,7 @@ S32-container/zip.t
S32-hash/exists.t
S32-hash/keys_values.t
S32-hash/pairs.t
S32-hash/push.t
S32-hash/slice.t
S32-list/end.t
S32-list/first.t
Expand Down

0 comments on commit 7b2c47c

Please sign in to comment.