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 6230719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:48:11+00:00 2026-05-24T09:48:11+00:00

Recently I stumbled over this code snippet in Ruby: @data = 3.chr * 5

  • 0

Recently I stumbled over this code snippet in Ruby:

@data = 3.chr * 5

which results in “\003\003\003\003\003”

later in the code for example

flag = @data[2] & 2

is used,
I know that it has something todo with bitwise-flags. It seems the values 1,2 and 3 are used as state flags, but because ruby 1.9, which is the version I am familar with, changed the Integer.chr method the code does no longer work and I would really like to know whats going on.
Furthermore, what is the purpose of the “\00x” escaped-thing?

Thanks for your answers

  • 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-24T09:48:12+00:00Added an answer on May 24, 2026 at 9:48 am

    To make the code work in Ruby 1.9, try changing that line to:

    flag = @data[2].ord & 2
    

    Prior to Ruby 1.9, str[n] would return an integer between 0 and 255, but in Ruby 1.9 with its new unicode support, str[n] returns a character (string of length 1). To get the integer instead of character, you can call .ord on the character.

    The & operator is just the standard bitwise AND operator common to C, Ruby, and many other languages.

    Byte number three (0x03) is not a printable ASCII character, so when you have that byte in a string and call inspect ruby denotes that byte as \003. Just make sure you understand that “\003” is a single-byte string while ‘\003’ is a four-byte string.

    In Ruby, strings are really sequences of bytes. In Ruby 1.9, there is also encoding information, but they are still really just a sequence of bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I stumbled over code such as this: void foo(const Bar* b) { ...
I recently stumbled over a problem caused by some very old code I wrote
I recently stumbled across this entry in the google testing blog about guidelines for
I have recently stumbled over a seeming inconsistency in Python's way of dealing with
I recently stumbled upon Etherpad, it's a collaborative writing tool http://code.google.com/p/etherpad/ - main project
Recently I stumbled across this pretty slick JS library called nodeJS that acts like
I'm new to this site having stumbled across it recently (and am loving it!)
I'm currently using URL Rewriting, but I recently stumbled upon this link on Rerouting
Recently I've stumbled over surface. It's incredible and so easy to develop small applications
I recently stumbled into this this C++/Lua error int function_for_lua( lua_State* L ) {

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.