Skip to content

Commit

Permalink
Implement IO#pid
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Jul 10, 2024
1 parent 2f1a977 commit 1188773
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/natalie/io_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class IoObject : public Object {
bool is_nonblock(Env *) const;
bool isatty(Env *) const;
int lineno(Env *) const;
Value pid(Env *) const;
static Value pipe(Env *, Value, Value, Block *, ClassObject *);
static Value popen(Env *, Args, Block *, ClassObject *);
int pos(Env *);
Expand Down
1 change: 1 addition & 0 deletions lib/natalie/compiler/binding_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ def generate_name
gen.binding('IO', 'lineno', 'IoObject', 'lineno', argc: 0, pass_env: true, pass_block: false, return_type: :int)
gen.binding('IO', 'lineno=', 'IoObject', 'set_lineno', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('IO', 'path', 'IoObject', 'get_path', argc: 0, pass_env: false, pass_block: false, aliases: ['to_path'], return_type: :Object)
gen.binding('IO', 'pid', 'IoObject', 'pid', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('IO', 'pos=', 'IoObject', 'set_pos', argc: 1, pass_env: true, pass_block: false, return_type: :int)
gen.binding('IO', 'pread', 'IoObject', 'pread', argc: 2..3, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('IO', 'print', 'IoObject', 'print', argc: :any, pass_env: true, pass_block: false, return_type: :Object)
Expand Down
8 changes: 2 additions & 6 deletions spec/core/io/close_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@
it "clears #pid" do
io = IO.popen ruby_cmd('r = loop{puts "y"; 0} rescue 1; exit r'), 'r'

NATFIXME 'Implement IO#pid', exception: NoMethodError, message: "undefined method `pid' for an instance of IO" do
io.pid.should_not == 0
end
io.pid.should_not == 0

io.close

NATFIXME 'Implement IO#pid', exception: SpecFailedException, message: "undefined method `pid' for an instance of IO" do
-> { io.pid }.should raise_error(IOError)
end
-> { io.pid }.should raise_error(IOError)
end

it "sets $?" do
Expand Down
7 changes: 7 additions & 0 deletions src/io_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,13 @@ Value IoObject::get_path() const {
return new StringObject { *m_path };
}

Value IoObject::pid(Env *env) const {
if (m_pid == -1)
return NilObject::the();
raise_if_closed(env);
return Value::integer(m_pid);
}

Value IoObject::pread(Env *env, Value count, Value offset, Value out_string) {
raise_if_closed(env);
if (!is_readable(m_fileno))
Expand Down

0 comments on commit 1188773

Please sign in to comment.