Ruby C extension rb_str_new2 seems to return false

I cannot post my actual code due to work copyright, so I will try to show my problem with simple example code. I have a C extension whose simplified version looks like: #include #include #include #include #include #include #include VALUE test(VALUE self, VALUE string); void Init_module_name() { module_name = rb_define_module("Modulename"); c_modulename = rb_define_class_under(modulename, "Class", rb_cObject); rb_define_method(c_modulename, "test", test, 1); e_ModuleNameError = rb_define_class_under(modulename, "Error", rb_eStandardError); } VALUE test(VALUE self, VALUE string) { char *c_string = StringValueCStr(string); int fd = open(c_string, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd == -1) { rb_raise(e_ModuleNameError, "Failed to open file"); } if (!isatty(fd)) { rb_raise(e_ModuleNameError, "File is not a tty"); } struct termios config; int termios_ret = init_termios(config, fd) if (termios_ret != OK) { // OK defined by enum in modulename's header close(fd); rb_raise(e_ModuleNameError, "Termios init failed."); } int success = write(fd, "I'm a string", str_length); if (success < str_length) { close(fd); rb_raise(e_ModuleNameError, "Failed to write to file."); } close(fd); return rb_str_new2("Success"); } Then, the ruby code that requires this looks like: require 'modulename' class ModuleName attr_acessor :file def initialize(file) @file = file @object = end def test @object.test @file end end Which is then called in my production project like: require "modulename_ruby_file" x = ModuleName "/dev/pts/1" x.test Here is the interesting thing. When I run this code in production, the return value from x.test above is false (As in literally the value false, not a string). Also, the write to the file never happens. However, if I do it in some simplified test code it returns the string "Success" just like expected and the write is indeed completed. Does anyone know of any situation that would cause this function not to execute the write, and return false? I already tried putting a rescue around it in case it was throwing one of the rb_raises, but it doesn't appear to be. Me and 3 other members of my team have looked at this all afternoon and have not found an answer.
I think the example code needs some edits. What is @console for example? Is it in fact @object?

以上就是Ruby C extension rb_str_new2 seems to return false的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准