Skip to content

Commit

Permalink
[Truffle] Adding Kernel#open to kernel.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Apr 24, 2015
1 parent b1cab0a commit 1502275
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
12 changes: 0 additions & 12 deletions spec/truffle/tags/core/kernel/open_tags.txt
@@ -1,15 +1,3 @@
fails:Kernel#open is a private method
fails:Kernel#open opens a file when given a valid filename
fails:Kernel#open opens a file when called with a block
fails:Kernel#open opens an io when path starts with a pipe
fails:Kernel#open opens an io when called with a block
fails:Kernel#open opens an io for writing
fails:Kernel#open raises an ArgumentError if not passed one argument
fails:Kernel#open raises a TypeError if passed a non-String that does not respond to #to_open
fails:Kernel#open accepts nil for mode and permission
fails:Kernel#open when given an object that responds to to_open calls #to_path to covert the argument to a String before calling #to_str
fails:Kernel#open when given an object that responds to to_open calls #to_str to convert the argument to a String
fails:Kernel#open when given an object that responds to to_open calls #to_open on argument
fails:Kernel#open when given an object that responds to to_open returns the value from #to_open
fails:Kernel#open when given an object that responds to to_open passes its arguments onto #to_open
fails:Kernel#open when given an object that responds to to_open passes the return value from #to_open to a block
21 changes: 21 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/kernel.rb
Expand Up @@ -228,6 +228,27 @@ def tap
self
end

def open(obj, *rest, &block)
if obj.respond_to?(:to_open)
obj = obj.to_open(*rest)

if block_given?
return yield(obj)
else
return obj
end
end

path = Rubinius::Type.coerce_to_path obj

if path.kind_of? String and path.prefix? '|'
return IO.popen(path[1..-1], *rest, &block)
end

File.open(path, *rest, &block)
end
module_function :open

def p(*a)
return nil if a.empty?
a.each { |obj| $stdout.puts obj.inspect }
Expand Down

0 comments on commit 1502275

Please sign in to comment.