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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:49:47+00:00 2026-05-30T15:49:47+00:00

The ruby gem BCrypt has an example of how to use one of it’s

  • 0

The ruby gem BCrypt has an example of how to use one of it’s modules. Taken from the docs:

def password
  @password ||= Password.new(self.password_hash)
end

def password=(new_password)
  @password = Password.create(new_password)
  self.password_hash = @password
end

Why would we use the instance variable @password at all when we have the self.password_hash attribute? I feel like I am missing something, probably due to my inexperience with ruby. Personally, I would have accomplished what I believe to be the same with:

def password
  self.password_hash ||= Password.new(self.password_hash)
end

def password=(new_password)
  self.password_hash = Password.create(new_password)
end
  • 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-30T15:49:48+00:00Added an answer on May 30, 2026 at 3:49 pm

    The difference is as follows: password hash is a string – a hash of the password. However Password.new(self.password_hash) creates new object of the Password class. Thus the difference: String vs Password. You can call methods such as cost, version etc. on the Password object, which are unavailable for String.

    I guess you find the following method strange:

    def password=(new_password)
      @password = Password.create(new_password)
      self.password_hash = @password
    end
    

    but what happens here, which is not obvious is the conversion of the Password object to String object in the self.password_hash assignment – the to_s method returns the hash of the password and that value is stored in the database. What is more – the @password instance variable is set, so the password method will return an instance of Password class, not the password hash String.

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

Sidebar

Related Questions

Is there a ruby gem which has pens/paint brush and stuff so that one
When I add has_secure_password to the model (inherited from ActiveRecord::Base), error stating that bcrypt-ruby
The ruby gem for LinkedIn that I'm using is here: http://github.com/pengwynn/linkedin When calling LinkedIn::Client.new
I am implementing a validation scheme and am using the bcrypt-ruby gem. require 'bcrypt'
I've installed the Ruby gem 'haml' on my mac, which I can use to
I'm trying to run the ruby gem mongo3 (http://mongo3.com/) I'm new to gem, but
I use curl of ruby gem curb to fetch multi urls under multi-threading env,
Is there a Ruby gem that can generate CSS sprites from PNG images by
Is there a good ruby gem for a WYSIWYG editor that will easily work
Is there any ruby gem/ rails plugin available for parsing the resume and importing

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.