Read binary file as string in Ruby

后端 未结 8 1656
无人共我
无人共我 2020-11-30 16:40

I need an easy way to take a tar file and convert it into a string (and vice versa). Is there a way to do this in Ruby? My best attempt was this:

file = File         


        
8条回答
  •  悲&欢浪女
    2020-11-30 17:24

    If you need binary mode, you'll need to do it the hard way:

    s = File.open(filename, 'rb') { |f| f.read }
    

    If not, shorter and sweeter is:

    s = IO.read(filename)
    

提交回复
热议问题