summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog25
-rw-r--r--array.c13
-rw-r--r--eval.c28
-rw-r--r--io.c14
-rw-r--r--lib/mathn.rb2
-rw-r--r--lib/rational.rb8
-rw-r--r--object.c6
-rw-r--r--string.c124
8 files changed, 127 insertions, 93 deletions
diff --git a/ChangeLog b/ChangeLog
index 0388e35e66..f50f95aede 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,28 @@
+Fri May 2 09:38:06 2003 Warren Brown <wkb@airmail.net>
+
+ * string.c (rb_str_ljust): now takes optional argument to specify
+ pad string. [ruby-talk:70482]
+
+ * string.c (rb_str_rjust): ditto.
+
+ * string.c (rb_str_center): ditto.
+
+ * string.c (rb_str_justify): utility function.
+
+Fri May 2 04:10:59 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * eval.c (rb_add_method): call singleton_method_added or
+ method_added for every method definition (after ruby_running).
+ [ruby-talk:70471]
+
+ * array.c (rb_ary_reverse_bang): Array#reverse! should not return
+ nil even for arrays sized less than 2.
+
+Thu May 1 23:18:01 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * io.c (argf_eof): should not block after reading all argument
+ files. (ruby-bugs-ja PR#449)
+
Fri May 2 15:10:41 2003 Minero Aoki <aamine@loveruby.net>
* lib/fileutils.rb: use hashes to pass options.
diff --git a/array.c b/array.c
index cecf4278f1..8deeaf6ac7 100644
--- a/array.c
+++ b/array.c
@@ -344,9 +344,6 @@ rb_ary_push_m(argc, argv, ary)
VALUE *argv;
VALUE ary;
{
- if (argc <= 0) {
- rb_raise(rb_eArgError, "wrong number arguments (at least 1)");
- }
while (argc--) {
rb_ary_push(ary, *argv++);
}
@@ -431,10 +428,6 @@ rb_ary_unshift_m(argc, argv, ary)
{
long len = RARRAY(ary)->len;
- if (argc <= 0) {
- rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
- }
-
/* make rooms by setting the last item */
rb_ary_store(ary, len + argc - 1, Qnil);
@@ -759,8 +752,8 @@ rb_ary_insert(argc, argv, ary)
{
long pos;
- if (argc < 2) {
- rb_raise(rb_eArgError, "wrong number of arguments (at least 2)");
+ if (argc < 1) {
+ rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
}
pos = NUM2LONG(argv[0]);
if (pos == -1) {
@@ -770,6 +763,7 @@ rb_ary_insert(argc, argv, ary)
pos++;
}
+ if (argc == 1) return ary;
rb_ary_update(ary, pos, 0, rb_ary_new4(argc - 1, argv + 1));
return ary;
}
@@ -1057,7 +1051,6 @@ static VALUE
rb_ary_reverse_bang(ary)
VALUE ary;
{
- if (RARRAY(ary)->len <= 1) return Qnil;
return rb_ary_reverse(ary);
}
diff --git a/eval.c b/eval.c
index 9240f3850e..4a079203a7 100644
--- a/eval.c
+++ b/eval.c
@@ -279,6 +279,14 @@ rb_add_method(klass, mid, node, noex)
rb_clear_cache_by_id(mid);
body = NEW_METHOD(node, noex);
st_insert(RCLASS(klass)->m_tbl, mid, (st_data_t)body);
+ if (node && mid != ID_ALLOCATOR && ruby_running) {
+ if (FL_TEST(klass, FL_SINGLETON)) {
+ rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1, ID2SYM(mid));
+ }
+ else {
+ rb_funcall(klass, added, 1, ID2SYM(mid));
+ }
+ }
}
void
@@ -3353,14 +3361,6 @@ rb_eval(self, n)
if (scope_vmode == SCOPE_MODFUNC) {
rb_add_method(rb_singleton_class(ruby_class),
node->nd_mid, defn, NOEX_PUBLIC);
- rb_funcall(ruby_class, singleton_added, 1, ID2SYM(node->nd_mid));
- }
- if (FL_TEST(ruby_class, FL_SINGLETON)) {
- rb_funcall(rb_iv_get(ruby_class, "__attached__"),
- singleton_added, 1, ID2SYM(node->nd_mid));
- }
- else {
- rb_funcall(ruby_class, added, 1, ID2SYM(node->nd_mid));
}
result = Qnil;
}
@@ -3396,7 +3396,6 @@ rb_eval(self, n)
defn->nd_rval = (VALUE)ruby_cref;
rb_add_method(klass, node->nd_mid, defn,
NOEX_PUBLIC|(body?body->nd_noex&NOEX_UNDEF:0));
- rb_funcall(recv, singleton_added, 1, ID2SYM(node->nd_mid));
result = Qnil;
}
break;
@@ -6033,7 +6032,6 @@ rb_mod_modfunc(argc, argv, module)
m = RCLASS(m)->super;
}
rb_add_method(rb_singleton_class(module), id, body->nd_body, NOEX_PUBLIC);
- rb_funcall(module, singleton_added, 1, ID2SYM(id));
}
return module;
}
@@ -7418,16 +7416,6 @@ rb_mod_define_method(argc, argv, mod)
noex = NOEX_PUBLIC;
}
rb_add_method(mod, id, node, noex);
- if (scope_vmode == SCOPE_MODFUNC) {
- rb_add_method(rb_singleton_class(mod), id, node, NOEX_PUBLIC);
- rb_funcall(mod, singleton_added, 1, ID2SYM(id));
- }
- if (FL_TEST(mod, FL_SINGLETON)) {
- rb_funcall(rb_iv_get(mod, "__attached__"), singleton_added, 1, ID2SYM(id));
- }
- else {
- rb_funcall(mod, added, 1, ID2SYM(id));
- }
return body;
}
diff --git a/io.c b/io.c
index 75008a97f7..78b885517f 100644
--- a/io.c
+++ b/io.c
@@ -3756,13 +3756,12 @@ argf_readchar()
static VALUE
argf_eof()
{
- if (!next_argv()) return Qtrue;
- if (next_p == 1) {
- return Qtrue;
- }
- if (rb_io_eof(current_file)) {
- next_p = 1;
- return Qtrue;
+ if (current_file) {
+ if (init_p == 0) return Qtrue;
+ if (rb_io_eof(current_file)) {
+ next_p = 1;
+ return Qtrue;
+ }
}
return Qfalse;
}
@@ -4052,7 +4051,6 @@ Init_IO()
rb_define_singleton_method(argf, "lineno", argf_lineno, 0);
rb_define_singleton_method(argf, "lineno=", argf_set_lineno, 1);
- current_file = rb_stdin;
rb_global_variable(&current_file);
filename = rb_str_new2("-");
rb_define_readonly_variable("$FILENAME", &filename);
diff --git a/lib/mathn.rb b/lib/mathn.rb
index cd4c6ef9e9..bab9083c78 100644
--- a/lib/mathn.rb
+++ b/lib/mathn.rb
@@ -10,8 +10,8 @@
#
#
-require "rational.rb"
require "complex.rb"
+require "rational.rb"
require "matrix.rb"
class Integer
diff --git a/lib/rational.rb b/lib/rational.rb
index 0c784fb94b..a0f0b06f8b 100644
--- a/lib/rational.rb
+++ b/lib/rational.rb
@@ -329,7 +329,7 @@ class Integer
end
class Fixnum
- if not defined? Complex
+ unless defined? Complex
alias power! **;
end
@@ -347,13 +347,13 @@ class Fixnum
end
end
- if not defined? Complex
+ unless defined? Complex
alias ** rpower
end
end
class Bignum
- if not defined? power!
+ unless defined? Complex
alias power! **
end
@@ -371,7 +371,7 @@ class Bignum
end
end
- if not defined? Complex
+ unless defined? Complex
alias ** rpower
end
end
diff --git a/object.c b/object.c
index afd2e197a7..c9661a226c 100644
--- a/object.c
+++ b/object.c
@@ -1398,9 +1398,9 @@ Init_Object()
rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
- rb_define_global_function("singleton_method_added", rb_obj_dummy, 1);
- rb_define_global_function("singleton_method_removed", rb_obj_dummy, 1);
- rb_define_global_function("singleton_method_undefined", rb_obj_dummy, 1);
+ rb_define_private_method(rb_mKernel, "singleton_method_added", rb_obj_dummy, 1);
+ rb_define_private_method(rb_mKernel, "singleton_method_removed", rb_obj_dummy, 1);
+ rb_define_private_method(rb_mKernel, "singleton_method_undefined", rb_obj_dummy, 1);
rb_define_global_function("sprintf", rb_f_sprintf, -1);
rb_define_global_function("format", rb_f_sprintf, -1);
diff --git a/string.c b/string.c
index 42177887a8..ee3dc6b774 100644
--- a/string.c
+++ b/string.c
@@ -3121,69 +3121,99 @@ rb_str_sum(argc, argv, str)
}
static VALUE
-rb_str_ljust(str, w)
+rb_str_justify(argc, argv, str, jflag)
+ int argc;
+ VALUE *argv;
VALUE str;
- VALUE w;
+ char jflag;
{
- long width = NUM2LONG(w);
+ VALUE w;
+ long width, flen = 0;
VALUE res;
- char *p, *pend;
-
+ char *p, *pend, *f = " ";
+ long n;
+ VALUE pad;
+
+ if (rb_scan_args(argc, argv, "11", &w, &pad) == 2) {
+ if (!NIL_P(pad)) {
+ StringValue(pad);
+ if (RSTRING(pad)->len > 0) {
+ f = RSTRING(pad)->ptr;
+ flen = RSTRING(pad)->len;
+ }
+ }
+ }
+ width = NUM2LONG(w);
if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
res = rb_str_new5(str, 0, width);
- memcpy(RSTRING(res)->ptr, RSTRING(str)->ptr, RSTRING(str)->len);
- p = RSTRING(res)->ptr + RSTRING(str)->len; pend = RSTRING(res)->ptr + width;
- while (p < pend) {
- *p++ = ' ';
+ p = RSTRING(res)->ptr;
+ if (jflag != 'l') {
+ n = width - RSTRING(str)->len;
+ pend = p + ((jflag == 'r') ? n : n/2);
+ if (flen <= 1) {
+ while (p < pend) {
+ *p++ = *f;
+ }
+ }
+ else {
+ char *q = f;
+ while (p + flen <= pend) {
+ memcpy(p,f,flen);
+ p += flen;
+ }
+ while (p < pend) {
+ *p++ = *q++;
+ }
+ }
+ }
+ memcpy(p, RSTRING(str)->ptr, RSTRING(str)->len);
+ if (jflag != 'r') {
+ p += RSTRING(str)->len; pend = RSTRING(res)->ptr + width;
+ if (flen <= 1) {
+ while (p < pend) {
+ *p++ = *f;
+ }
+ }
+ else {
+ while (p + flen <= pend) {
+ memcpy(p,f,flen);
+ p += flen;
+ }
+ while (p < pend) {
+ *p++ = *f++;
+ }
+ }
}
OBJ_INFECT(res, str);
+ if (flen > 0) OBJ_INFECT(res, pad);
return res;
}
static VALUE
-rb_str_rjust(str, w)
+rb_str_ljust(argc, argv, str)
+ int argc;
+ VALUE *argv;
VALUE str;
- VALUE w;
{
- long width = NUM2LONG(w);
- VALUE res;
- char *p, *pend;
-
- if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
- res = rb_str_new5(str, 0, width);
- p = RSTRING(res)->ptr; pend = p + width - RSTRING(str)->len;
- while (p < pend) {
- *p++ = ' ';
- }
- memcpy(pend, RSTRING(str)->ptr, RSTRING(str)->len);
- OBJ_INFECT(res, str);
- return res;
+ return rb_str_justify(argc, argv, str, 'l');
}
static VALUE
-rb_str_center(str, w)
+rb_str_rjust(argc, argv, str)
+ int argc;
+ VALUE *argv;
VALUE str;
- VALUE w;
{
- long width = NUM2LONG(w);
- VALUE res;
- char *p, *pend;
- long n;
+ return rb_str_justify(argc, argv, str, 'r');
+}
- if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
- res = rb_str_new5(str, 0, width);
- n = (width - RSTRING(str)->len)/2;
- p = RSTRING(res)->ptr; pend = p + n;
- while (p < pend) {
- *p++ = ' ';
- }
- memcpy(pend, RSTRING(str)->ptr, RSTRING(str)->len);
- p = pend + RSTRING(str)->len; pend = RSTRING(res)->ptr + width;
- while (p < pend) {
- *p++ = ' ';
- }
- OBJ_INFECT(res, str);
- return res;
+static VALUE
+rb_str_center(argc, argv, str)
+ int argc;
+ VALUE *argv;
+ VALUE str;
+{
+ return rb_str_justify(argc, argv, str, 'c');
}
void
@@ -3265,9 +3295,9 @@ Init_String()
rb_define_method(rb_cString, "scan", rb_str_scan, 1);
- rb_define_method(rb_cString, "ljust", rb_str_ljust, 1);
- rb_define_method(rb_cString, "rjust", rb_str_rjust, 1);
- rb_define_method(rb_cString, "center", rb_str_center, 1);
+ rb_define_method(rb_cString, "ljust", rb_str_ljust, -1);
+ rb_define_method(rb_cString, "rjust", rb_str_rjust, -1);
+ rb_define_method(rb_cString, "center", rb_str_center, -1);
rb_define_method(rb_cString, "sub", rb_str_sub, -1);
rb_define_method(rb_cString, "gsub", rb_str_gsub, -1);