Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:38:47+00:00 2026-05-27T02:38:47+00:00

In Ruby 1.9 the File and IO libraries were changed — they now seem

  • 0

In Ruby 1.9 the File and IO libraries were changed — they now seem to always interpret the data as encoded strings (e.g. UTF-8), and the returned values seem to be always strings.

I need to read a file in Ruby 1.9 byte by byte, without any modification or interpretation of the data.
I want to read byte sequences, not encoded strings.

Any tips on how to best do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:38:47+00:00Added an answer on May 27, 2026 at 2:38 am

    I had a similar problem in a gem I wrote. Here’s the relevant code:
    (you don’t need the require statements)

    # ==============================================================================
    # Loading Libraries and Stuff needed for Ruby 1.9 vs 1.8 Compatibility
    # ==============================================================================
    # the idea here is to define a couple of go-between methods for different classes
    # which are differently defined depending on which Ruby version it is -- thereby
    # abstracting from the particular Ruby version's API of those classes
    
    if RUBY_VERSION >= "1.9.0"
      require "digest/md5"
      require "digest/sha1"
      include Digest
    
      require 'fileutils'        # replaces ftools
      include FileUtils::Verbose
    
      class File
        def read_bytes(n)  # returns a string containing bytes
              self.bytes.take(n)
        end
        def write_bytes(bytes)
          self.syswrite(bytes)
        end
        def get_byte
          self.getbyte     # returns a number 0..255
        end
      end
    
      ZEROBYTE = "\x00".force_encoding(Encoding::BINARY) unless defined? ZEROBYTE
    
    else # older Ruby versions:
      require 'rubygems'
    
      require "md5"
      require "sha1"
    
      require 'ftools'
      def move(a,b)
        File.move(a,b)
      end
    
      class String
        def getbyte(x)   # when accessing a string and selecting x-th byte to do calculations , as defined in Ruby 1.9
          self[x]  # returns an integer
        end
      end
    
      class File
        def read_bytes(n)
          self.read(n)   # should use sysread here as well?
        end
        def write_bytes(bytes)
          self.write(bytes)   # should use syswrite here as well?
        end
        def get_byte     # in older Ruby versions <1.9 getc returned a byte, e.g. a number 0..255
          self.getc   # returns a number 0..255
        end
      end
    
      ZEROBYTE = "\0" unless defined? ZEROBYTE
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to parse a tab-delimited text file with Ruby to extract some data
I would like to write some data to a file in Ruby. What is
I have a ruby file as follows: module Example class Myclass def t_st Hello
I see this all the time in Ruby: require File.dirname(__FILE__) + /../../config/environment What does
I want to move a file with Ruby. How do I do that?
I tried following the README file in Ruby 1.9.1 but I can't compile it
Is it possible to use single XML file for Ruby on Rails as an
Looking for something similar to xerces for parsing an xml file in ruby. I
Ruby tempfile instances automatically delete their corresponding file when the references are released. However,
Are there any gems / libraries which can be used with Ruby to convert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.