Skip to content

Commit 1502275

Browse files
committed
[Truffle] Adding Kernel#open to kernel.rb
1 parent b1cab0a commit 1502275

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
fails:Kernel#open is a private method
2-
fails:Kernel#open opens a file when given a valid filename
3-
fails:Kernel#open opens a file when called with a block
41
fails:Kernel#open opens an io when path starts with a pipe
52
fails:Kernel#open opens an io when called with a block
63
fails:Kernel#open opens an io for writing
7-
fails:Kernel#open raises an ArgumentError if not passed one argument
8-
fails:Kernel#open raises a TypeError if passed a non-String that does not respond to #to_open
9-
fails:Kernel#open accepts nil for mode and permission
10-
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
11-
fails:Kernel#open when given an object that responds to to_open calls #to_str to convert the argument to a String
12-
fails:Kernel#open when given an object that responds to to_open calls #to_open on argument
13-
fails:Kernel#open when given an object that responds to to_open returns the value from #to_open
14-
fails:Kernel#open when given an object that responds to to_open passes its arguments onto #to_open
15-
fails:Kernel#open when given an object that responds to to_open passes the return value from #to_open to a block

truffle/src/main/ruby/core/rubinius/common/kernel.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,27 @@ def tap
228228
self
229229
end
230230

231+
def open(obj, *rest, &block)
232+
if obj.respond_to?(:to_open)
233+
obj = obj.to_open(*rest)
234+
235+
if block_given?
236+
return yield(obj)
237+
else
238+
return obj
239+
end
240+
end
241+
242+
path = Rubinius::Type.coerce_to_path obj
243+
244+
if path.kind_of? String and path.prefix? '|'
245+
return IO.popen(path[1..-1], *rest, &block)
246+
end
247+
248+
File.open(path, *rest, &block)
249+
end
250+
module_function :open
251+
231252
def p(*a)
232253
return nil if a.empty?
233254
a.each { |obj| $stdout.puts obj.inspect }

0 commit comments

Comments
 (0)