Modified: MacRuby/branches/experimental/roxor.cpp (1479 => 1480)
--- MacRuby/branches/experimental/roxor.cpp 2009-04-23 23:30:07 UTC (rev 1479)
+++ MacRuby/branches/experimental/roxor.cpp 2009-04-23 23:43:48 UTC (rev 1480)
@@ -8567,6 +8567,27 @@
return Qtrue;
}
+static VALUE
+rb_vm_struct_inspect(VALUE rcv, SEL sel)
+{
+ VALUE str = rb_str_new2("#<");
+ rb_str_cat2(str, rb_obj_classname(rcv));
+
+ VALUE *rcv_data;
+ Data_Get_Struct(rcv, VALUE, rcv_data);
+ rb_vm_bs_boxed_t *bs_boxed = locate_bs_boxed(CLASS_OF(rcv));
+ for (unsigned i = 0; i < bs_boxed->as.s->fields_count; i++) {
+ rb_str_cat2(str, " ");
+ rb_str_cat2(str, bs_boxed->as.s->fields[i].name);
+ rb_str_cat2(str, "=");
+ rb_str_append(str, rb_inspect(rcv_data[i]));
+ }
+
+ rb_str_cat2(str, ">");
+
+ return str;
+}
+
static bool
register_bs_boxed(bs_element_type_t type, void *value)
{
@@ -8611,6 +8632,8 @@
// Define other utility methods.
rb_objc_define_method(boxed->klass, "==",
(void *)rb_vm_struct_equal, 1);
+ rb_objc_define_method(boxed->klass, "inspect",
+ (void *)rb_vm_struct_inspect, 0);
}
GET_VM()->bs_boxed[octype] = boxed;
Modified: MacRuby/branches/experimental/spec/macruby/struct_spec.rb (1479 => 1480)
--- MacRuby/branches/experimental/spec/macruby/struct_spec.rb 2009-04-23 23:30:07 UTC (rev 1479)
+++ MacRuby/branches/experimental/spec/macruby/struct_spec.rb 2009-04-23 23:43:48 UTC (rev 1480)
@@ -144,4 +144,22 @@
NSPoint.new.should_not == [0.0, 0.0]
NSPoint.new.should_not == NSSize.new
end
+
+ it "has a nice #inspect message that lists the fields" do
+ p = NSPoint.new
+ p.inspect.should == "#<NSPoint x=0.0 y=0.0>"
+ p.x = 1
+ p.y = 2
+ p.inspect.should == "#<NSPoint x=1.0 y=2.0>"
+
+ s = NSSize.new(3, 4)
+ s.inspect.should == "#<NSSize width=3.0 height=4.0>"
+
+ r = NSRect.new
+ r.inspect.should == "#<NSRect origin=#<NSPoint x=0.0 y=0.0> size=#<NSSize width=0.0 height=0.0>>"
+ r.origin = p
+ r.inspect.should == "#<NSRect origin=#<NSPoint x=1.0 y=2.0> size=#<NSSize width=0.0 height=0.0>>"
+ r.size = s
+ r.inspect.should == "#<NSRect origin=#<NSPoint x=1.0 y=2.0> size=#<NSSize width=3.0 height=4.0>>"
+ end
end