narzero
narzero

Reputation: 2299

Why is this value getting added to the end of a my variable?

Why does the value <REDACTED> get added to the end of variable two when running #get_digest?

Here's what my code looks like:

require 'digest'
require 'uri'

class <REDACTED>   
  def request_to_string(args = {})
    encoded_search_query = URI.encode_www_form(args[:<REDACTED>])
    "#{<REDACTED>}#{<REDACTED>}"
  end

  <redacted>
end

Every time I run #<REDACTED> the value of two changes to something it shouldn't be. What seems to be going wrong?

Upvotes: 1

Views: 79

Answers (2)

konsolebox
konsolebox

Reputation: 75458

You're modifying the String instance in args[:request_string] directly.

Change it to this instead:

str = "#{args[:request_string]}"

Upvotes: 3

user2421167
user2421167

Reputation:

Replace request_string: two with request_string: two.dup().

Ruby passes the variable by reference, so inside of get_digest, when you use the << operator, it is modifying the variable in-place.

Upvotes: 3

Related Questions