Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,14 +854,21 @@ rb_obj_inspect(VALUE obj)
{
VALUE ivars = rb_check_funcall(obj, id_instance_variables_to_inspect, 0, 0);
st_index_t n = 0;
if (UNDEF_P(ivars)) {
if (UNDEF_P(ivars) || NIL_P(ivars)) {
n = rb_ivar_count(obj);
ivars = Qnil;
}
else if (!NIL_P(ivars)) {
Check_Type(ivars, T_ARRAY);
else if (RB_TYPE_P(ivars, T_ARRAY)) {
n = RARRAY_LEN(ivars);
}
else {
rb_raise(
rb_eTypeError,
"Expected #instance_variables_to_inspect to return an Array or nil, but it returned %"PRIsVALUE,
rb_obj_class(ivars)
);
}

if (n > 0) {
VALUE c = rb_class_name(CLASS_OF(obj));
VALUE args[2] = {
Expand All @@ -875,6 +882,13 @@ rb_obj_inspect(VALUE obj)
}
}

/* :nodoc: */
static VALUE
rb_obj_instance_variables_to_inspect(VALUE obj)
{
return Qnil;
}

static VALUE
class_or_module_required(VALUE c)
{
Expand Down Expand Up @@ -4535,6 +4549,7 @@ InitVM_Object(void)

rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
rb_define_private_method(rb_mKernel, "instance_variables_to_inspect", rb_obj_instance_variables_to_inspect, 0);
rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1); /* in class.c */
rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); /* in class.c */
rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1); /* in class.c */
Expand Down
12 changes: 6 additions & 6 deletions prism/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ nodes:
comment: |
Represents the location of the `class` keyword.

class Foo end
class Foo; end
^^^^^
- name: constant_path
type: node
Expand Down Expand Up @@ -1899,19 +1899,19 @@ nodes:
comment: |
Represents the location of the `end` keyword.

class Foo end
^^^
class Foo; end
^^^
- name: name
type: constant
comment: |
The name of the class.

class Foo end # name `:Foo`
class Foo; end # name `:Foo`
comment: |
Represents a class declaration involving the `class` keyword.

class Foo end
^^^^^^^^^^^^^
class Foo; end
^^^^^^^^^^^^^^
- name: ClassVariableAndWriteNode
fields:
- name: name
Expand Down
29 changes: 29 additions & 0 deletions spec/ruby/core/kernel/inspect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,34 @@ class << obj
inspected = obj.inspect.sub(/^#<Object:0x[0-9a-f]+/, '#<Object:0x00')
inspected.should == "#<Object:0x00>"
end

it "displays all instance variables if #instance_variables_to_inspect returns nil" do
obj = Object.new
obj.instance_eval do
@host = "localhost"
@user = "root"
@password = "hunter2"
end
obj.singleton_class.class_eval do
private def instance_variables_to_inspect = nil
end

inspected = obj.inspect.sub(/^#<Object:0x[0-9a-f]+/, '#<Object:0x00')
inspected.should == %{#<Object:0x00 @host="localhost", @user="root", @password="hunter2">}
end

it "raises an error if #instance_variables_to_inspect returns an invalid value" do
obj = Object.new
obj.instance_eval do
@host = "localhost"
@user = "root"
@password = "hunter2"
end
obj.singleton_class.class_eval do
private def instance_variables_to_inspect = {}
end

->{ obj.inspect }.should raise_error(TypeError, "Expected #instance_variables_to_inspect to return an Array or nil, but it returned Hash")
end
end
end
1 change: 1 addition & 0 deletions vm_insnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,7 @@ rb_vm_setclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *cfp, ID
vm_setclassvariable(iseq, cfp, id, val, ic);
}

ALWAYS_INLINE(static VALUE vm_getinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, IVC ic));
static inline VALUE
vm_getinstancevariable(const rb_iseq_t *iseq, VALUE obj, ID id, IVC ic)
{
Expand Down