# How to make a string into an object instance name?

**URL:** <https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324>\
**Category:** Rails\
**Created:** [January 7, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324 "2007-01-07T19:50:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 7, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324/1 "2007-01-07T19:50:05Z")

</div>

> > text\_field(“post”, “title”, “size” =\> 20)  
> > will generate:
> 
> >

so, when you name “post”, there is a way to translate this word to  
“@post”, an object’s name. how can I do that?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 7, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324/2 "2007-01-07T19:53:31Z")

</div>

and i saw:  
def link\_to\_page(page\_name, web = @web, text = nil, options = {})  
…  
end

in a helper file.  
what does “web = @web” do here?

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 7, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324/3 "2007-01-07T20:59:09Z")

</div>

Nanyang Z. wrote:

> > > text\_field(“post”, “title”, “size” =\> 20)  
> > > will generate:
> > 
> > >
> 
> so, when you name “post”, there is a way to translate this word to  
> “@post”, an object’s name. how can I do that?

In your view you can do this with:  
object\_name = “post”  
instance\_variable\_get("@#{object\_name}")

However, most of the time you should use @post directly.

Dan M.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 7, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324/4 "2007-01-07T21:01:09Z")

</div>

Nanyang Z. wrote:

> and i saw:  
> def link\_to\_page(page\_name, web = @web, text = nil, options = {})  
> …  
> end
> 
> in a helper file.  
> what does “web = @web” do here?

It means if “web” is not passed in to the link\_to\_page method, it  
defaults to the value of @web.

link\_to\_page(:page\_name) #=\> uses @web  
link\_to\_page(:page\_name, :my\_web) #=\> uses :my\_web

Dan M.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 7, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324/5 "2007-01-07T21:14:39Z")

</div>

Dan M. wrote:

> > what does “web = @web” do here?
> 
> It means if “web” is not passed in to the link\_to\_page method, it  
> defaults to the value of @web.

Thanks Dan.

What about my first question?  
I defined a helper method, namely address\_field(object\_name)  
I used it in a view file like address\_field(“school”)  
now I want to use @school.city inside this method, how can I do that?  
I don’t want to put @school.city inside directly, because this helper  
method will be used by other objects.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 7, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/how-to-make-a-string-into-an-object-instance-name/81324/6 "2007-01-07T21:21:12Z")

</div>

Dan M. wrote:

> In your view you can do this with:  
> object\_name = “post”  
> instance\_variable\_get("@#{object\_name}")

> Dan M.

I am sorry, I replied before I saw your first post.

I added  
@object = instance\_variable\_get("@#{object\_name}")  
in to the method, I can use @object.city now.

Thanks again!
