summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-09-18 15:43:06 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-09-18 15:43:06 +0000
commit27f97827b18c15b250313f33e4cf26bfc258f619 (patch)
treecc66ac3cbdcfa6749548747c71e37811369f67b5 /object.c
parent824f114051e59b16a22484539ee782d9abcdd5e3 (diff)
* 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
Diffstat (limited to 'object.c')
-rw-r--r--object.c61
1 files changed, 58 insertions, 3 deletions
diff --git a/object.c b/object.c
index d66e24e6de..cbadb9c295 100644
--- a/object.c
+++ b/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
@@ -2002,6 +2000,36 @@ rb_obj_ivar_set(obj, iv, 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
*
* Returns the value of the given class variable (or throws a
@@ -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);