1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Prefer rb_check_arity when 0 or 1 arguments

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66205 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-12-05 01:09:44 +00:00
parent cfa6c55347
commit 09ef29a78f
5 changed files with 40 additions and 61 deletions

57
array.c
View file

@ -2351,8 +2351,12 @@ rb_ary_join_m(int argc, VALUE *argv, VALUE ary)
{
VALUE sep;
rb_scan_args(argc, argv, "01", &sep);
if (NIL_P(sep)) sep = rb_output_fs;
if (rb_check_arity(argc, 0, 1) == 0) {
sep = rb_output_fs;
}
else if (NIL_P(sep = argv[0])) {
sep = rb_output_fs;
}
return rb_ary_join(ary, sep);
}
@ -3448,10 +3452,7 @@ rb_ary_slice_bang(int argc, VALUE *argv, VALUE ary)
return arg2;
}
if (argc != 1) {
/* error report */
rb_scan_args(argc, argv, "11", NULL, NULL);
}
rb_check_arity(argc, 1, 2);
arg1 = argv[0];
if (!FIXNUM_P(arg1)) {
@ -4735,9 +4736,7 @@ rb_ary_max(int argc, VALUE *argv, VALUE ary)
VALUE num;
long i;
rb_scan_args(argc, argv, "01", &num);
if (!NIL_P(num))
if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0]))
return rb_nmin_run(ary, num, 0, 1, 1);
if (rb_block_given_p()) {
@ -4790,9 +4789,7 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary)
VALUE num;
long i;
rb_scan_args(argc, argv, "01", &num);
if (!NIL_P(num))
if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0]))
return rb_nmin_run(ary, num, 0, 0, 1);
if (rb_block_given_p()) {
@ -5001,7 +4998,7 @@ rb_ary_count(int argc, VALUE *argv, VALUE ary)
{
long i, n = 0;
if (argc == 0) {
if (rb_check_arity(argc, 0, 1) == 0) {
VALUE v;
if (!rb_block_given_p())
@ -5013,9 +5010,8 @@ rb_ary_count(int argc, VALUE *argv, VALUE ary)
}
}
else {
VALUE obj;
VALUE obj = argv[0];
rb_scan_args(argc, argv, "1", &obj);
if (rb_block_given_p()) {
rb_warn("given block not used");
}
@ -5111,7 +5107,7 @@ rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary)
int mod = 0, level = -1;
VALUE result, lv;
rb_scan_args(argc, argv, "01", &lv);
lv = (rb_check_arity(argc, 0, 1) ? argv[0] : Qnil);
rb_ary_modify_check(ary);
if (!NIL_P(lv)) level = NUM2INT(lv);
if (level == 0) return Qnil;
@ -5154,11 +5150,12 @@ static VALUE
rb_ary_flatten(int argc, VALUE *argv, VALUE ary)
{
int mod = 0, level = -1;
VALUE result, lv;
VALUE result;
rb_scan_args(argc, argv, "01", &lv);
if (!NIL_P(lv)) level = NUM2INT(lv);
if (level == 0) return ary_make_shared_copy(ary);
if (rb_check_arity(argc, 0, 1) && !NIL_P(argv[0])) {
level = NUM2INT(argv[0]);
if (level == 0) return ary_make_shared_copy(ary);
}
result = flatten(ary, level, &mod);
OBJ_INFECT(result, ary);
@ -5295,7 +5292,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
}
}
len = RARRAY_LEN(ary);
if (argc == 0) {
if (rb_check_arity(argc, 0, 1) == 0) {
if (len < 2)
i = 0;
else
@ -5303,7 +5300,7 @@ rb_ary_sample(int argc, VALUE *argv, VALUE ary)
return rb_ary_elt(ary, i);
}
rb_scan_args(argc, argv, "1", &nv);
nv = argv[0];
n = NUM2LONG(nv);
if (n < 0) rb_raise(rb_eArgError, "negative sample number");
if (n > len) n = len;
@ -5457,16 +5454,15 @@ static VALUE
rb_ary_cycle(int argc, VALUE *argv, VALUE ary)
{
long n, i;
VALUE nv = Qnil;
rb_scan_args(argc, argv, "01", &nv);
rb_check_arity(argc, 0, 1);
RETURN_SIZED_ENUMERATOR(ary, argc, argv, rb_ary_cycle_size);
if (NIL_P(nv)) {
if (argc == 0 || NIL_P(argv[0])) {
n = -1;
}
else {
n = NUM2LONG(nv);
n = NUM2LONG(argv[0]);
if (n <= 0) return Qnil;
}
@ -5628,13 +5624,13 @@ rb_ary_permutation_size(VALUE ary, VALUE args, VALUE eobj)
static VALUE
rb_ary_permutation(int argc, VALUE *argv, VALUE ary)
{
VALUE num;
long r, n, i;
n = RARRAY_LEN(ary); /* Array length */
RETURN_SIZED_ENUMERATOR(ary, argc, argv, rb_ary_permutation_size); /* Return enumerator if no block */
rb_scan_args(argc, argv, "01", &num);
r = NIL_P(num) ? n : NUM2LONG(num); /* Permutation size from argument */
r = n;
if (rb_check_arity(argc, 0, 1) && !NIL_P(argv[0]))
r = NUM2LONG(argv[0]); /* Permutation size from argument */
if (r < 0 || n < r) {
/* no permutations: yield nothing */
@ -6306,8 +6302,7 @@ rb_ary_sum(int argc, VALUE *argv, VALUE ary)
long i, n;
int block_given;
if (rb_scan_args(argc, argv, "01", &v) == 0)
v = LONG2FIX(0);
v = (rb_check_arity(argc, 0, 1) ? argv[0] : LONG2FIX(0));
block_given = rb_block_given_p();

25
class.c
View file

@ -1178,17 +1178,10 @@ static VALUE
class_instance_method_list(int argc, const VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
{
VALUE ary;
int recur, prepended = 0;
int recur = TRUE, prepended = 0;
struct method_entry_arg me_arg;
if (argc == 0) {
recur = TRUE;
}
else {
VALUE r;
rb_scan_args(argc, argv, "01", &r);
recur = RTEST(r);
}
if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
if (!recur && RCLASS_ORIGIN(mod) != mod) {
mod = RCLASS_ORIGIN(mod);
@ -1417,25 +1410,21 @@ rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
VALUE
rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
{
VALUE recur, ary, klass, origin;
VALUE ary, klass, origin;
struct method_entry_arg me_arg;
struct rb_id_table *mtbl;
int recur = TRUE;
if (argc == 0) {
recur = Qtrue;
}
else {
rb_scan_args(argc, argv, "01", &recur);
}
if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
klass = CLASS_OF(obj);
origin = RCLASS_ORIGIN(klass);
me_arg.list = st_init_numtable();
me_arg.recur = RTEST(recur);
me_arg.recur = recur;
if (klass && FL_TEST(klass, FL_SINGLETON)) {
if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
klass = RCLASS_SUPER(klass);
}
if (RTEST(recur)) {
if (recur) {
while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
klass = RCLASS_SUPER(klass);

5
dir.c
View file

@ -1080,9 +1080,8 @@ dir_s_chdir(int argc, VALUE *argv, VALUE obj)
{
VALUE path = Qnil;
if (rb_scan_args(argc, argv, "01", &path) == 1) {
FilePathValue(path);
path = rb_str_encode_ospath(path);
if (rb_check_arity(argc, 0, 1) == 1) {
path = rb_str_encode_ospath(rb_get_path(argv[0]));
}
else {
const char *dist = getenv("HOME");

10
enum.c
View file

@ -294,7 +294,7 @@ enum_find(int argc, VALUE *argv, VALUE obj)
struct MEMO *memo;
VALUE if_none;
rb_scan_args(argc, argv, "01", &if_none);
if_none = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
RETURN_ENUMERATOR(obj, argc, argv);
memo = MEMO_NEW(Qundef, 0, 0);
rb_block_call(obj, id_each, 0, 0, find_i, (VALUE)memo);
@ -1699,9 +1699,7 @@ enum_min(int argc, VALUE *argv, VALUE obj)
VALUE result;
VALUE num;
rb_scan_args(argc, argv, "01", &num);
if (!NIL_P(num))
if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0]))
return rb_nmin_run(obj, num, 0, 0, 0);
m->min = Qundef;
@ -1793,9 +1791,7 @@ enum_max(int argc, VALUE *argv, VALUE obj)
VALUE result;
VALUE num;
rb_scan_args(argc, argv, "01", &num);
if (!NIL_P(num))
if (rb_check_arity(argc, 0, 1) && !NIL_P(num = argv[0]))
return rb_nmin_run(obj, num, 0, 1, 0);
m->max = Qundef;

View file

@ -2061,11 +2061,11 @@ rb_class_initialize(int argc, VALUE *argv, VALUE klass)
if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
rb_raise(rb_eTypeError, "already initialized class");
}
if (argc == 0) {
if (rb_check_arity(argc, 0, 1) == 0) {
super = rb_cObject;
}
else {
rb_scan_args(argc, argv, "01", &super);
super = argv[0];
rb_check_inheritable(super);
if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
rb_raise(rb_eTypeError, "can't inherit uninitialized class");