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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:20:16+00:00 2026-06-09T05:20:16+00:00

In this Ruby on Rails helper , there is the following method: # Convert

  • 0

In this Ruby on Rails helper, there is the following method:

# Convert an 8-bit or 16-bit string to an array of big-endian words
# In 8-bit function, characters >255 have their hi-byte silently ignored.
def str2binb(str)
    bin = []
    mask = (1 << $chrsz) - 1
    #for(var i = 0; i < str.length * $chrsz; i += $chrsz)
    i = 0
    while(i < str.length * $chrsz)
        bin[i>>5] ||= 0
        p 'i'
        p i
        p '$chrsz'
        p $chrsz
        p 'str[i / $chrsz]'
        p str[i / $chrsz]
        p 'mask'
        p mask
        bin[i>>5] |= (str[i / $chrsz] & mask) << (32 - $chrsz - i%32)
        i += $chrsz
    end
    return bin
end

When run, this error occurs on the bin[i>>5] |= (str[i / $chrsz] & mask) << (32 - $chrsz - i%32) line:

ActionView::Template::Error (undefined method `&' for "J":String)

I don’t understand how & is being used in this instance. How can I resolve this error? I am using Rails 3.2.0 and Ruby 1.9.2.

Here is the logging output:

"i"
0
"$chrsz"
8
"str[i / $chrsz]"
"J"
"mask"
255
  • 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-06-09T05:20:19+00:00Added an answer on June 9, 2026 at 5:20 am

    My guess is that this script is working on Ruby 1.8 but fails in Ruby 1.9 because Ruby1.9 outputs b when accessing "bla"[0] instead of the ASCII value of the char as it used to be in Ruby1.8.

    You should be able to replace the problematic line by this :

    bin[i>>5] |= (str.getbyte(i / $chrsz) & mask) << (32 - $chrsz - i%32)
    

    Or using ord instead :

    bin[i>>5] |= (str[(i / $chrsz)].ord & mask) << (32 - $chrsz - i%32)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a helper method I found in the ruby on rails application spree
This is not a ruby/rails project deploy. I have the following situation and I
Referencing this post: ruby on rails average per day I'm getting the average per
This is more a general question but my particular case involves a ruby/rails app
I'm currently facing this problem with ruby on rails 3, devise and facebook oauth.
I'm new on both this site and ruby on rails! I have a common
this time it's about Ruby On Rails. I have a form in the view
The default in Ruby on Rails is to have this set to false (in
I am learning Ruby on Rails and i did this scaffolding command: rails g
I'm new with ruby and rails and have been struggling with this issue for

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.