# Can we add a string to a instance varaiable

**URL:** <https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557>\
**Category:** Rails\
**Created:** [June 16, 2008, 8:31am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557 "2008-06-16T08:31:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [June 16, 2008, 8:31am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557/1 "2008-06-16T08:31:19Z")

</div>

HI ,

can we add a string to an instance variable like

@jan+"#{users}"=User.find(:all)

because i’ve the same query to be executed for both User and Post, for  
every month jan…dec.

so can i write a method which contains

@jan+"#{name}"=#{Name}.find(:all) name–\> either post or user that  
i pass to the method…

and how to convert Name i,e; string into object?

plz reply…

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [June 16, 2008, 8:41am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557/2 "2008-06-16T08:41:38Z")

</div>

User.find(:all).group\_by(&:month)

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [June 16, 2008, 9:00am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557/3 "2008-06-16T09:00:47Z")

</div>

Ryan B. wrote:

> User.find(:all).group\_by(&:month)

i need some thing like

@jan is an instance varaible and can i add a string like name=“hi”  
to the instance variable like

@jan+"#{name}"

is it possible and even i want to know can we convert a string object  
into object(instance)?

like we do in java to\_String() and to\_Object()

?

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [June 16, 2008, 9:06am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557/4 "2008-06-16T09:06:52Z")

</div>

Why? What are you trying to accomplish?

On Mon, Jun 16, 2008 at 4:30 PM, Vamsi K. \<  
removed\_email\_address@domain.invalid\> wrote:

> >

–  
Appreciated my help?  
Recommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [June 16, 2008, 9:50am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557/5 "2008-06-16T09:50:36Z")

</div>

On Jun 16, 8:31 am, Vamsi K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> so the query is same for both of them…
> 
> so can i write
> 
> @jan+“#{name}”=“#{Name}”.find(:all,:condition=\>[“created\_at between ?  
> AND ?”,“#{changedyear}-01-01”,“#{changedyear}-01-31”]) where name may be  
> post or user …

Well I thin you’re looking for instance\_variable\_set and constantize,  
but personally I’d put most of that method in the post/user model (and  
probably return a hash keyed by the month you’re looking at).

Fred

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [June 16, 2008, 9:31am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557/6 "2008-06-16T09:31:04Z")

</div>

Ryan B. wrote:

> Why? What are you trying to accomplish?
> 
> On Mon, Jun 16, 2008 at 4:30 PM, Vamsi K. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > >
> 
> –  
> Appreciated my help?  
> Recommend me on Working With Rails  
> [http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

yeah, to be clear …

i’ve 2 methods one for posts and another for users…  
i’d like to get number of users for every month and same for number of  
posts for every month…

for that i had written like

```
           @januser=User.find(:all,:condition=>["created_at between 

```

? AND ?“,”#{changedyear}-01-01",“#{changedyear}-01-31”])  
like this for every month as i don’t 've a feild name month in  
database…

and the same query can be used by post like  
@janpost=Post.find(:all,:condition=\>[“created\_at between ? AND  
?”,“#{changedyear}-01-01”,“#{changedyear}-01-31”])

and changedyear is a dynamic one…

so the query is same for both of them…

so can i write

@jan+“#{name}”=“#{Name}”.find(:all,:condition=\>[“created\_at between ?  
AND ?”,“#{changedyear}-01-01”,“#{changedyear}-01-31”]) where name may be  
post or user …

---

<div class="post-metadata">

**Author:** ![vamsi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vamsi](https://www.ruby-forum.com/u/vamsi)\
**Post date:** [June 16, 2008, 9:59am UTC](https://www.ruby-forum.com/t/can-we-add-a-string-to-a-instance-varaiable/140557/7 "2008-06-16T09:59:30Z")

</div>

The finder methods generated by ActiveRecord take a range (I think), so  
you  
can find the start date for January and then the end date and do  
something  
like this:

Post.find\_all\_by\_created\_at(beginning\_of\_january…end\_of\_january)
