Skip to content
Permalink
Browse files

Merge pull request #648 from kostya/construct

Hash#constructor
  • Loading branch information
alex committed Apr 21, 2013
2 parents 58e722c + 56f3964 commit ec164369f05f0303ab9524a4f339562738cbb711
Showing with 28 additions and 24 deletions.
  1. +28 −0 lib-topaz/hash.rb
  2. +0 −12 spec/tags/core/hash/constructor_tags.txt
  3. +0 −12 topaz/objects/hashobject.py
@@ -13,6 +13,34 @@ def each(&block)
end
alias each_pair each

def self.[](*args)
if args.size == 1
arg = args[0]
if hash = Hash.try_convert(arg)
return allocate.replace(hash)
elsif array = Array.try_convert(arg)
h = new
array.each do |a|
next unless a = Array.try_convert(a)
next if a.size < 1 || a.size > 2
h[a[0]] = a[1]
end
return h
end
end

return new if args.empty?
raise ArgumentError.new("odd number of arguments for Hash") if args.size.odd?

h = new
i = 0
while i < args.size
h[args[i]] = args[i + 1]
i += 2
end
h
end

def each_key(&block)
return self.enum_for(:each_key) if !block
each { |k, v| yield k }

This file was deleted.

@@ -18,18 +18,6 @@ def __init__(self, space, klass=None):
def method_allocate(self, space, args_w):
return W_HashObject(space, self)

@classdef.singleton_method("[]")
def singleton_method_subscript(self, space, w_obj=None):
if w_obj is None:
return W_HashObject(space)
w_res = space.convert_type(w_obj, space.w_hash, "to_hash", raise_error=False)
if w_res is space.w_nil:
raise NotImplementedError
assert isinstance(w_res, W_HashObject)
result = W_HashObject(space)
result.contents.update(w_res.contents)
return result

@classdef.singleton_method("try_convert")
def method_try_convert(self, space, w_obj):
if not space.is_kind_of(w_obj, space.w_hash):

0 comments on commit ec16436

Please sign in to comment.
You can’t perform that action at this time.