Skip to content

Instantly share code, notes, and snippets.

@shyouhei
Last active July 24, 2019 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyouhei/fd29912f7349ed75770b95b6b03c8654 to your computer and use it in GitHub Desktop.
Save shyouhei/fd29912f7349ed75770b95b6b03c8654 to your computer and use it in GitHub Desktop.
From 7b6bbba78cb2fcfb2a9def7d2c31aad64c03b5c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
<shyouhei@ruby-lang.org>
Date: Wed, 24 Jul 2019 16:29:59 +0900
Subject: [PATCH] greetings.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: 卜部昌平 <shyouhei@ruby-lang.org>
---
main.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/main.c b/main.c
index 6e8ab7b9fe..a56066e0c3 100644
--- a/main.c
+++ b/main.c
@@ -35,6 +35,7 @@ main(int argc, char **argv)
setlocale(LC_CTYPE, "");
#endif
+ fprintf(stdout, "%s\n", "Hello");
ruby_sysinit(&argc, &argv);
{
RUBY_INIT_STACK;
--
2.17.1
From 8f324331bc7840a1419ee0f96a69cdcdf5cb2e4b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
<shyouhei@ruby-lang.org>
Date: Wed, 24 Jul 2019 16:28:23 +0900
Subject: [PATCH] add Kernel#hello and its test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: 卜部昌平 <shyouhei@ruby-lang.org>
---
object.c | 9 +++++++++
test/ruby/test_io.rb | 4 ++++
2 files changed, 13 insertions(+)
diff --git a/object.c b/object.c
index 6b7a8f2952..bf21b1d4eb 100644
--- a/object.c
+++ b/object.c
@@ -4044,6 +4044,13 @@ rb_obj_dig(int argc, VALUE *argv, VALUE obj, VALUE notfound)
return obj;
}
+static VALUE
+rb_f_hello(VALUE self)
+{
+ fprintf(stdout, "%s\n", "Hello");
+ return self;
+}
+
/*
* Document-class: Class
*
@@ -4305,6 +4312,8 @@ InitVM_Object(void)
rb_define_global_function("Array", rb_f_array, 1);
rb_define_global_function("Hash", rb_f_hash, 1);
+ rb_define_global_function("hello", rb_f_hello, 0);
+
rb_cNilClass = rb_define_class("NilClass", rb_cObject);
rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 3c5dc7671a..f98c1f3788 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -3865,4 +3865,8 @@ def test_external_encoding_index
assert_raise(TypeError) {Marshal.dump(w)}
}
end
+
+ def test_hello
+ assert_in_out_err([], "hello", ["Hello"])
+ end
end
--
2.17.1
From 5c500c6eecadbd7389ef21ba21810fa9e5498926 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
<shyouhei@ruby-lang.org>
Date: Wed, 24 Jul 2019 17:29:51 +0900
Subject: [PATCH] add Process.exist?
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: 卜部昌平 <shyouhei@ruby-lang.org>
---
process.c | 48 +++++++++++++++++++++++++++++++++++++++
test/ruby/test_process.rb | 8 +++++++
2 files changed, 56 insertions(+)
diff --git a/process.c b/process.c
index f1cd802f95..fd1165e6db 100644
--- a/process.c
+++ b/process.c
@@ -8038,11 +8038,58 @@ rb_clock_getres(int argc, VALUE *argv)
}
}
+
VALUE rb_mProcess;
static VALUE rb_mProcUID;
static VALUE rb_mProcGID;
static VALUE rb_mProcID_Syscall;
+#if true /* simple */
+
+static VALUE
+proc_exist(VALUE self, VALUE pid)
+{
+ pid_t p = NUM2PIDT(pid);
+ int e = kill(p, 0);
+ if (e == 0) {
+ return Qtrue;
+ }
+ else if (errno == ESRCH) {
+ return Qfalse;
+ }
+ else {
+ return Qtrue;
+ }
+}
+
+#else /* complicated */
+
+
+static VALUE
+proc_exist_begin(VALUE arg)
+{
+ rb_funcall(rb_mProcess, rb_intern("kill"), 2, INT2FIX(0), arg);
+ return Qtrue;
+}
+
+static VALUE
+proc_exist_rescue(VALUE self, VALUE exc)
+{
+ if (rb_funcall(exc, rb_intern("errno"), 0) == ESRCH) {
+ return Qfalse;
+ }
+ else {
+ return Qtrue;
+ }
+}
+
+static VALUE
+proc_exist(VALUE self, VALUE pid)
+{
+ return rb_rescue(proc_exist_begin, pid, proc_exist_rescue, self);
+}
+
+#endif
/*
* The Process module is a collection of methods used to
@@ -8089,6 +8136,7 @@ InitVM_process(void)
rb_define_singleton_method(rb_mProcess, "exit", rb_f_exit, -1);
rb_define_singleton_method(rb_mProcess, "abort", rb_f_abort, -1);
rb_define_singleton_method(rb_mProcess, "last_status", proc_s_last_status, 0);
+ rb_define_singleton_method(rb_mProcess, "exist?", proc_exist, 1);
rb_define_module_function(rb_mProcess, "kill", rb_f_kill, -1); /* in signal.c */
rb_define_module_function(rb_mProcess, "wait", proc_wait, -1);
diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb
index 02efc168c2..e3b908d2fb 100644
--- a/test/ruby/test_process.rb
+++ b/test/ruby/test_process.rb
@@ -2436,4 +2436,12 @@ def test_last_status
Process.wait spawn(RUBY, "-e", "exit 13")
assert_same(Process.last_status, $?)
end
+
+ def test_process_exist
+ pid = spawn(RUBY, in: :close) # create a nonexistent PID
+ Process.wait pid
+
+ assert_equal(Process.exist?(pid), false)
+ assert_equal(Process.exist?(Process.pid), true)
+ end
end
--
2.17.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment