summaryrefslogtreecommitdiff
path: root/struct.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-03 11:02:53 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-03 11:02:53 +0000
commit9d22a06ea06e9bec5030fd6e2c8481958fcd30d9 (patch)
tree58b15b4dc85acca1c806388ff3848a3c85fd0ebf /struct.c
parenta11ab83884c47f729a8630ab5ef21c7522b48386 (diff)
* array.c (rb_values_at): extract common procedure from
rb_ary_values_at. follow DRY principle. * re.c (match_values_at): values_at should understand ranges. * struct.c (rb_struct_values_at): ditto. * struct.c (inspect_struct): inspect format changed; add "struct " at the top. * sprintf.c (rb_f_sprintf): "%p" specifier for inspect output. (RCR#68) * eval.c (rb_mod_undef_method): allow "undef_method" to accept multiple arguments. (RCR#146) * lib/timeout.rb: put timeout in Timeout module. (RCR#121) [ruby-talk:61028] * re.c (match_groups): new method added. (RCR#139) * variable.c (rb_mod_const_of): should exclude constant defined in Object, unless retrieving constants of Object. * string.c (rb_str_new4): do not allocate new string if original is frozen or already have copy-on-write entry. [ruby-talk:74940] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4031 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c37
1 files changed, 13 insertions, 24 deletions
diff --git a/struct.c b/struct.c
index 6d9c8113a9..32ea8f525c 100644
--- a/struct.c
+++ b/struct.c
@@ -352,18 +352,6 @@ rb_struct_each_pair(s)
}
static VALUE
-rb_struct_to_s(s)
- VALUE s;
-{
- char *cname = rb_class2name(rb_obj_class(s));
- VALUE str = rb_str_new(0, strlen(cname) + 4);
-
- sprintf(RSTRING(str)->ptr, "#<%s>", cname);
- RSTRING(str)->len = strlen(RSTRING(str)->ptr);
- return str;
-}
-
-static VALUE
inspect_struct(s)
VALUE s;
{
@@ -376,7 +364,7 @@ inspect_struct(s)
rb_bug("non-initialized struct");
}
- str = rb_str_buf_new2("#<");
+ str = rb_str_buf_new2("#<struct ");
rb_str_cat2(str, cname);
rb_str_cat2(str, " ");
for (i=0; i<RSTRUCT(s)->len; i++) {
@@ -405,9 +393,9 @@ rb_struct_inspect(s)
{
if (rb_inspecting_p(s)) {
char *cname = rb_class2name(rb_obj_class(s));
- VALUE str = rb_str_new(0, strlen(cname) + 8);
+ VALUE str = rb_str_new(0, strlen(cname) + 15);
- sprintf(RSTRING(str)->ptr, "#<%s:...>", cname);
+ sprintf(RSTRING(str)->ptr, "#<struct %s:...>", cname);
RSTRING(str)->len = strlen(RSTRING(str)->ptr);
return str;
}
@@ -530,19 +518,20 @@ rb_struct_aset(s, idx, val)
}
static VALUE
+struct_entry(s, n)
+ VALUE s;
+ long n;
+{
+ return rb_struct_aref(s, LONG2NUM(n));
+}
+
+static VALUE
rb_struct_values_at(argc, argv, s)
int argc;
VALUE *argv;
VALUE s;
{
- VALUE result = rb_ary_new();
- long i;
-
- for (i=0; i<argc; i++) {
- rb_ary_push(result, rb_struct_aref(s, argv[i]));
- }
-
- return result;
+ return rb_values_at(s, RSTRUCT(s)->len, argc, argv, struct_entry);
}
static VALUE
@@ -648,7 +637,7 @@ Init_Struct()
rb_define_method(rb_cStruct, "eql?", rb_struct_eql, 1);
rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);
- rb_define_method(rb_cStruct, "to_s", rb_struct_to_s, 0);
+ rb_define_method(rb_cStruct, "to_s", rb_struct_inspect, 0);
rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0);
rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0);