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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:58:18+00:00 2026-05-26T16:58:18+00:00

I am testing two strings to see if they are equal. One string is

  • 0

I am testing two strings to see if they are equal.
One string is just a simple string: "\17"
The other is parsed to: "\17"

num = 7
num2 = "\17"

parsed_num = "\1#{num}"

puts parsed_num.class
puts num2.class

if parsed_num == num2
  puts 'Equal'
else
  puts 'Not equal'
end

It returns:

String

String

Not equal

My goal is to have parsed_num exactly the same as the literal num2

  • 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-26T16:58:19+00:00Added an answer on May 26, 2026 at 4:58 pm

    I am going to take the opposite answer and assume that “\17” is correct, then consider this code:

    num = 7
    num2 = "\17"
    puts "ni  #{num2.inspect}"
    
    # extra \ to fix error, for demo
    parsed_num = "\\1#{num}"
    puts "pi  #{parsed_num.inspect}"
    
    # for note, but ICK!!!
    p2 = eval('"' + parsed_num + '"')
    puts "p2i #{p2.inspect}"
    puts "p2= #{p2 == num2}"
    
    dec = (10 + num).to_s.oct
    p3 = dec.chr
    puts "p3i #{p3.inspect}"
    puts "p3= #{p3 == num2}"
    

    Result:

    ni  "\017"
    pi  "\\17"
    p2i "\017"
    p2= true
    p3i "\017"
    p3= true
    

    The reason why "\1#{num}" didn’t work is that string literals — and the embedded escape sequences — are handled during parsing while the string interpolation itself (#{}) happens later, at run-time. (This is required, because who knows what may happen to be in num?)

    In the case of p2 I used eval, which parses and then executes the supplied code. The code there is equivalent to eval('"\17"'), because parsed_num contained the 3-letter string: \17. (Please note, this approach is generally considered bad!)

    In the case of p3 I manually did what the parser does for string interpolation of \octal: took the value of octal, in, well, octal, and then converted it into the “character” with the corresponding value.

    Happy coding.

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

Sidebar

Related Questions

When I'm testing my simple geometric library, I usually have one or two methods
I am trying to write a simple script in JavaScript, just for testing purposes.
I need to compare hash values of two strings. I use string template for
This is for automating a testing process. I have two string arrays (extracted from
I have two strings, one is generated by PHP and stored in a database
I have two strings, and I would like to test if they are anagrams
I have two classes that I am testing (let's call them ClassA and ClassB).
I am testing my app in two different devices. My app is relies heavily
I'm making my first steps with unit testing and am unsure about two paradigms
My table is having four fields in which two fields are unique. They are

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.