Class: Phuby::Array
- Inherits:
-
Object
- Object
- Phuby::Array
- Defined in:
- lib/phuby/array.rb,
ext/phuby/phuby_array.c
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each(&block) ⇒ Object
- #get(key) ⇒ Object
- #key?(key) ⇒ Boolean
- #length ⇒ Object
- #set(key, value) ⇒ Object
- #to_a ⇒ Object
Instance Method Details
#[](key) ⇒ Object
3 4 5 |
# File 'lib/phuby/array.rb', line 3 def [] key get key end |
#[]=(key, value) ⇒ Object
7 8 9 |
# File 'lib/phuby/array.rb', line 7 def []= key, value set key, value end |
#each(&block) ⇒ Object
11 12 13 14 15 |
# File 'lib/phuby/array.rb', line 11 def each &block 0.upto(length - 1) do |i| block.call get(i) end end |
#get(key) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'ext/phuby/phuby_array.c', line 21
static VALUE get(VALUE self, VALUE key)
{
zval * array;
zval **value;
Data_Get_Struct(self, zval, array);
switch(TYPE(key))
{
case T_FIXNUM:
if(SUCCESS == zend_hash_index_find(
Z_ARRVAL_P(array),
NUM2INT(key),
(void **)&value
)) {
return ZVAL2VALUE(rb_iv_get(self, "@runtime"), *value);
}
break;
default:
if(SUCCESS == zend_hash_find(
Z_ARRVAL_P(array),
StringValuePtr(key),
RSTRING_LEN(key) + 1, // Add one for the NULL byte
(void **)&value
)) {
return ZVAL2VALUE(rb_iv_get(self, "@runtime"), *value);
}
}
return Qnil;
}
|
#key?(key) ⇒ Boolean
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'ext/phuby/phuby_array.c', line 69
static VALUE key_eh(VALUE self, VALUE key)
{
zval * array;
zval **value;
Data_Get_Struct(self, zval, array);
if(zend_hash_exists(Z_ARRVAL_P(array),
StringValuePtr(key), RSTRING_LEN(key) + 1)) {
return Qtrue;
}
return Qfalse;
}
|
#length ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'ext/phuby/phuby_array.c', line 12
static VALUE length(VALUE self)
{
zval * array;
Data_Get_Struct(self, zval, array);
return INT2NUM(zend_hash_num_elements(Z_ARRVAL_P(array)));
}
|
#set(key, value) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'ext/phuby/phuby_array.c', line 53
static VALUE set(VALUE self, VALUE key, VALUE value)
{
zval * array;
Data_Get_Struct(self, zval, array);
VALUE s_key = rb_funcall(key, rb_intern("to_s"), 0);
add_assoc_zval(array,
StringValuePtr(s_key),
VALUE2ZVAL(rb_iv_get(self, "@runtime"), value)
);
return self;
}
|
#to_a ⇒ Object
17 18 19 20 21 22 |
# File 'lib/phuby/array.rb', line 17 def to_a tmp = [] each { |x| tmp << x } tmp end |