mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* object.c (rb_obj_ivar_defined, rb_mod_cvar_defined): new methods,
Kernel#instance_variable_defined? and Module#class_variable_defined?. [ruby-dev:29587] * lib/date/format.rb (Date::Bag#method_missing): use new method, instance_variable_defined? to check if an instance variable is defined. fixed: [ruby-dev:29554] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10962 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
824f114051
commit
27f97827b1
3 changed files with 2553 additions and 2490 deletions
|
@ -98,10 +98,8 @@ class Date
|
|||
t = '@' + t
|
||||
if set
|
||||
instance_variable_set(t, *args)
|
||||
else
|
||||
if instance_variables.include?(t)
|
||||
instance_variable_get(t)
|
||||
end
|
||||
elsif instance_variable_defined?(t)
|
||||
instance_variable_get(t)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
61
object.c
61
object.c
|
@ -1839,7 +1839,6 @@ rb_mod_const_defined(mod, name)
|
|||
* k.methods.length #=> 42
|
||||
*/
|
||||
|
||||
|
||||
static VALUE
|
||||
rb_obj_methods(argc, argv, obj)
|
||||
int argc;
|
||||
|
@ -1967,7 +1966,6 @@ rb_obj_ivar_get(obj, iv)
|
|||
return rb_ivar_get(obj, id);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* obj.instance_variable_set(symbol, obj) => obj
|
||||
|
@ -2000,6 +1998,36 @@ rb_obj_ivar_set(obj, iv, val)
|
|||
return rb_ivar_set(obj, id, val);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* obj.instance_variable_defined?(symbol) => true or false
|
||||
*
|
||||
* Returns <code>true</code> if the given instance variable is
|
||||
* defined in <i>obj</i>.
|
||||
*
|
||||
* class Fred
|
||||
* def initialize(p1, p2)
|
||||
* @a, @b = p1, p2
|
||||
* end
|
||||
* end
|
||||
* fred = Fred.new('cat', 99)
|
||||
* fred.instance_variable_defined?(:@a) #=> true
|
||||
* fred.instance_variable_defined?("@b") #=> true
|
||||
* fred.instance_variable_defined?("@c") #=> false
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
rb_obj_ivar_defined(obj, iv)
|
||||
VALUE obj, iv;
|
||||
{
|
||||
ID id = rb_to_id(iv);
|
||||
|
||||
if (!rb_is_instance_id(id)) {
|
||||
rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
|
||||
}
|
||||
return rb_ivar_defined(obj, id);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* mod.class_variable_get(symbol) => obj
|
||||
|
@ -2029,7 +2057,6 @@ rb_mod_cvar_get(obj, iv)
|
|||
return rb_cvar_get(obj, id);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* obj.class_variable_set(symbol, obj) => obj
|
||||
|
@ -2064,6 +2091,32 @@ rb_mod_cvar_set(obj, iv, val)
|
|||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* obj.class_variable_defined?(symbol) => true or false
|
||||
*
|
||||
* Returns <code>true</code> if the given class variable is defined
|
||||
* in <i>obj</i>.
|
||||
*
|
||||
* class Fred
|
||||
* @@foo = 99
|
||||
* end
|
||||
* Fred.class_variable_defined?(:@@foo) #=> true
|
||||
* Fred.class_variable_defined?(:@@bar) #=> false
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
rb_mod_cvar_defined(obj, iv)
|
||||
VALUE obj, iv;
|
||||
{
|
||||
ID id = rb_to_id(iv);
|
||||
|
||||
if (!rb_is_instance_id(id)) {
|
||||
rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
|
||||
}
|
||||
return rb_cvar_defined(obj, id);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
convert_type(val, tname, method, raise)
|
||||
VALUE val;
|
||||
|
@ -2604,6 +2657,7 @@ Init_Object()
|
|||
rb_obj_instance_variables, 0); /* in variable.c */
|
||||
rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
|
||||
rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
|
||||
rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
|
||||
rb_define_private_method(rb_mKernel, "remove_instance_variable",
|
||||
rb_obj_remove_instance_variable, 1); /* in variable.c */
|
||||
|
||||
|
@ -2685,6 +2739,7 @@ Init_Object()
|
|||
rb_define_method(rb_cModule, "private_instance_methods",
|
||||
rb_class_private_instance_methods, -1); /* in class.c */
|
||||
|
||||
rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
|
||||
rb_define_method(rb_cModule, "constants", rb_mod_constants, 0); /* in variable.c */
|
||||
rb_define_method(rb_cModule, "const_get", rb_mod_const_get, 1);
|
||||
rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue