value_t Hash::to_s(Hash *self) { RecursionDetector<RecursionType::Hash_to_s, false> rd(self); if(rd.recursion()) return String::get("{...}"); CharArray result = "{"; OnStack<1> os1(self); OnStackString<1> os2(result); HashAccess::each_pair(self, [&](value_t key, value_t value) -> bool { OnStack<1> os(value); result += inspect(key); result += "=>"; result += inspect(value); result += ", "; return true; }); if(result.size() > 1) result.shrink(result.size() - 2); result += "}"; return result.to_string(); }
value_t IO::rb_readlines(String *path) { Stream *file; Platform::wrap([&] { file = Platform::open(path->string, Platform::Read, Platform::Open); }); Finally finally([&] { delete file; }); auto result = Array::allocate(); while(true) { CharArray line = file->gets(); if(line.size()) result->vector.push(line.to_string()); else break; } return result; }
value_t rb_each_line(IO *io, value_t block) { OnStack<2> os(io, block); while(true) { io->assert_stream(); CharArray line = io->stream->gets(); if(line.size()) yield(block, line.to_string()); else break; } return io; }