# Helper functions permanently altering variables

**URL:** <https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507>\
**Category:** Rails\
**Created:** [July 19, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507 "2008-07-19T18:01:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [July 19, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507/1 "2008-07-19T18:01:33Z")

</div>

Greetings,

I’m not sure if I’m maybe not understanding the helper functions  
correctly, but I’m having a weird problem that seems like it shouldn’t  
be. I’m sanitizing some book names for url’s using my application  
helper:

– application helper

def format\_url(url)  
title = url  
clean\_string(title)  
truncate(@title, 100, “”)  
return title  
end

def clean\_string(str)  
str.gsub!(/\W+/, ’ ') # all non-word chars to spaces  
str.strip! # ohh la la  
str.downcase! #  
str.gsub!(/\ +/, ‘-’) # spaces to dashes, preferred separator char  
everywhere  
end

– view showing the books

book.product\_name  
#returns “a name with a lot of spaces and characters in it”

link\_to(image\_tag(book.image\_url\_small), :controller =\> “book”, :action  
=\> “detail”, :book\_url =\> format\_url(book.product\_name), :key =\>  
book.isbn)

book.product\_name  
#returns “a-name-with-a-lot-of-spaces-and-characters-in-it”

so the problem is I want a function that formats just the path, but what  
I have here alters book.product name permanently, so that when I want to  
write it as text to the screen after using format\_url,  
it-has-the-escape-dashes-everywhere-in-it. how come it alters this when  
I pass it as a param, and then variable assign it to “title” and do all  
the alterations on the “title” variable?

Thanks,

Jason

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [July 19, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507/2 "2008-07-19T18:03:28Z")

</div>

by the way I notice that the above code is wrong - assume that title and  
@title are consistent throughout the function.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [July 19, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507/3 "2008-07-19T18:55:17Z")

</div>

Jason P. wrote:

> by the way I notice that the above code is wrong - assume that title and  
> @title are consistent throughout the function.

The easy answer is that in ruby, everything is an object and objects get  
passed by reference, not by value. So when you pass in url to a method,  
and then assign it to another variable (title), that new variable is  
just a pointer to the same memory location as what was passed in. The  
solution is to clone or dup:

title = url.clone

And you should be okay. I may have over-simplified the explanation, and  
if so, someone else might elect to explain it better.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [July 19, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507/4 "2008-07-19T19:00:08Z")

</div>

> The easy answer is that in ruby, everything is an object and objects get  
> passed by reference, not by value. So when you pass in url to a method,  
> and then assign it to another variable (title), that new variable is  
> just a pointer to the same memory location as what was passed in. The  
> solution is to clone or dup:

Ahhh, ok thanks Phillip! Explanation was good and understood.

I actually solved the problem just recently by doing thus:

title = url + “”

which seems to work and force title not to just remain as a pointer to  
url.

Cheers,

Jason

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [July 19, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507/5 "2008-07-19T19:04:22Z")

</div>

Jason P. wrote:

> I actually solved the problem just recently by doing thus:
> 
> title = url + “”

And if I had to guess (which I do because I don’t feel like digging  
through source), I’d say that under the hood the equivalent of

title = url.clone + “”

is happening. So you could save the one or two processor cycles by doing  
the clone yourself and not worrying about the concatenation.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [July 19, 2008, 7:10pm UTC](https://www.ruby-forum.com/t/helper-functions-permanently-altering-variables/143507/6 "2008-07-19T19:10:21Z")

</div>

Great thanks for the explanation. Processing cycles saved.
