How to check ARGF is empty or not in Ruby -
i want argf this.
# file.rb if argf.??? puts argf.read else puts "no redirect." end $ echo "hello world" | ruby file.rb hello world $ ruby file.rb no redirect. i need without waiting user input. tried eof? or closed? doesn't help. ideas?
note misunderstood argf. please see comments below.
basically you'd examine #filename. 1 way is:
if argf.filename != "-" puts argf.read else puts "no redirect." end and more complete form:
#!/usr/bin/env ruby if argf.filename != "-" or (not stdin.tty? , not stdin.closed?) puts argf.read else puts "no redirect." end another:
#!/usr/bin/env ruby if not stdin.tty? , not stdin.closed? puts stdin.read else puts "no redirect." end
Comments
Post a Comment