Skip to content

Commit

Permalink
Add #empty? to Tempfile, StringIO, File::Stat
Browse files Browse the repository at this point in the history
Rubocop prefers `empty?` over `length == 0` and `size == 0`, which is great for String, Array, Hash, etc. It would be nice if more classes implemented `#empty?` for consistancy.

See related discussion at rubocop/rubocop#2841
  • Loading branch information
mikegee authored and hsbt committed Oct 25, 2023
1 parent a6a67b0 commit 28fbcde
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,23 @@ strio_size(VALUE self)
return ULONG2NUM(RSTRING_LEN(string));
}

/*
* call-seq:
* strio.empty? -> true or false
*
* Returns the true when there is no content.
*/
static VALUE
strio_empty(VALUE self)
{
VALUE string = StringIO(self)->string;
if (NIL_P(string)) {
rb_raise(rb_eIOError, "not opened");
}
if (RSTRING_LEN(string) == 0) { return Qtrue; }
return Qfalse;
}

/*
* call-seq:
* strio.truncate(integer) -> 0
Expand Down Expand Up @@ -1913,6 +1930,7 @@ Init_stringio(void)
rb_define_method(StringIO, "fileno", strio_fileno, 0);
rb_define_method(StringIO, "size", strio_size, 0);
rb_define_method(StringIO, "length", strio_size, 0);
rb_define_method(StringIO, "empty?", strio_empty, 0);
rb_define_method(StringIO, "truncate", strio_truncate, 1);

rb_define_method(StringIO, "external_encoding", strio_external_encoding, 0);
Expand Down
1 change: 1 addition & 0 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -7845,6 +7845,7 @@ Init_File(void)
rb_define_method(rb_cStat, "executable_real?", rb_stat_X, 0);
rb_define_method(rb_cStat, "file?", rb_stat_f, 0);
rb_define_method(rb_cStat, "zero?", rb_stat_z, 0);
rb_define_method(rb_cStat, "empty?", rb_stat_z, 0);
rb_define_method(rb_cStat, "size?", rb_stat_s, 0);
rb_define_method(rb_cStat, "owned?", rb_stat_owned, 0);
rb_define_method(rb_cStat, "grpowned?", rb_stat_grpowned, 0);
Expand Down
5 changes: 5 additions & 0 deletions lib/tempfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ def size
end
alias length size

# Returns true if the file has no content.
def empty?
size.zero?
end

# :stopdoc:
def inspect
if @tmpfile.closed?
Expand Down
10 changes: 10 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,16 @@ def test_size
assert_equal(4, f.size)
end

def test_empty_with_some_content
f = StringIO.new("1234")
assert !f.empty?
end

def test_empty_with_no_content
f = StringIO.new("")
assert f.empty?
end

# This test is should in ruby/test_method.rb
# However this test depends on stringio library,
# we write it here.
Expand Down
14 changes: 14 additions & 0 deletions test/test_tempfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ def test_size_on_empty_file
assert_equal 0, t.size
end

def test_empty_on_empty_file
t = tempfile("foo")
t.write("")
t.close
assert t.empty?
end

def test_empty_on_file_with_some_content
t = tempfile("foo")
t.write("bar")
t.close
assert !t.empty?
end

def test_concurrency
threads = []
tempfiles = []
Expand Down

0 comments on commit 28fbcde

Please sign in to comment.