Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Kernel.print spec compliant #2199

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/core/kernel/print_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Kernel#print" do
it "is a private method" do
Kernel.should have_private_instance_method(:print)
end

it "delegates to $stdout" do
-> { print :arg }.should output("arg")
end

it "prints $_ when no arguments are given" do
orig_value = $_
$_ = 'foo'
-> { print }.should output("foo")
ensure
$_ = orig_value
end
end

describe "Kernel.print" do
it "needs to be reviewed for spec completeness"
end
12 changes: 2 additions & 10 deletions src/kernel_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,8 @@ Value KernelModule::p(Env *env, Args args) {
Value KernelModule::print(Env *env, Args args) {
auto _stdout = env->global_get("$stdout"_s);
assert(_stdout);
// NATFIXME: This prevents crashes when $stdout is set to an object that
// doesn't have a write method. Technically this should be done when
// setting the global, but we dont have a hook for that yet, so this will
// do for now.
if (!_stdout->respond_to(env, "write"_s)) {
env->raise("TypeError", "$stdout must have write method, {} given", _stdout->klass()->inspect_str());
}
if (!_stdout->respond_to(env, "print"_s)) {
env->raise("TypeError", "$stdout must have print method, {} given", _stdout->klass()->inspect_str());
}
if (args.size() == 0)
args = Args { env->global_get("$_"_s) };
// NATFIXME: Kernel.print should actually call IO.print and not
// IO.write, but for now using IO.print causes crashes.
// return _stdout->send(env, "print"_s, args);
Expand Down
Loading