Hello all. I have a variable that I want to share between all methods in my controller, but I don''t know how to do this. I''ve declared the variable outside of any method of the controller (like in Java code), but it doesn''t work. Can anyone help me? Here is some of the code I''m using: MyController < ApplicationController @global_variable = Hash.new def new ... end def edit ... end end Thanks in advance. Best regards, Hugo
Class variables in ruby begin with a @@ so @@global_variable = Hash.new should work.> Hello all. > I have a variable that I want to share between all methods in my > controller, but I don''t know how to do this. I''ve declared the > variable outside of any method of the controller (like in Java code), > but it doesn''t work. Can anyone help me? > > Here is some of the code I''m using: > > MyController < ApplicationController > > @global_variable = Hash.new > > def new > ... > end > > def edit > ... > end > > end > > Thanks in advance. > > Best regards, > Hugo > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Hugo Magalhaes
2005-Sep-04 12:49 UTC
Re: Where to put global variables - Beginner question
Thanks for the answer Oliver, but aren''t class variables shared among all objects of a class? I want this variable to be shared only within MyController object and not all MyController objects. On 9/4/05, Oliver Legg <lists-XiLho7JcZycsV2N9l4h3zg@public.gmane.org> wrote:> Class variables in ruby begin with a @@ so @@global_variable > Hash.new should work. > > > > Hello all. > > I have a variable that I want to share between all methods in my > > controller, but I don''t know how to do this. I''ve declared the > > variable outside of any method of the controller (like in Java code), > > but it doesn''t work. Can anyone help me? > > > > Here is some of the code I''m using: > > > > MyController < ApplicationController > > > > @global_variable = Hash.new > > > > def new > > ... > > end > > > > def edit > > ... > > end > > > > end > > > > Thanks in advance. > > > > Best regards, > > Hugo > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Patrick McCafferty
2005-Sep-04 12:57 UTC
Re: Where to put global variables - Beginner question
You want what? A class variable shares a variable across all instances of a class, that''s how it works... which is what you need to do if you want it to persist between requests to your app, since a new instance of your class is created for every request. I can''t even begin to think of how else to work with classes in a web application context. If you want to store it for a given session store it in the session. On 9/4/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks for the answer Oliver, but aren''t class variables shared among > all objects of a class? > I want this variable to be shared only within MyController object and > not all MyController objects. > > On 9/4/05, Oliver Legg <lists-XiLho7JcZycsV2N9l4h3zg@public.gmane.org> wrote: > > Class variables in ruby begin with a @@ so @@global_variable > > Hash.new should work. > > > > > > > Hello all. > > > I have a variable that I want to share between all methods in my > > > controller, but I don''t know how to do this. I''ve declared the > > > variable outside of any method of the controller (like in Java code), > > > but it doesn''t work. Can anyone help me? > > > > > > Here is some of the code I''m using: > > > > > > MyController < ApplicationController > > > > > > @global_variable = Hash.new > > > > > > def new > > > ... > > > end > > > > > > def edit > > > ... > > > end > > > > > > end > > > > > > Thanks in advance. > > > > > > Best regards, > > > Hugo > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I''m afraid (not sure) it''s imposible to initializate instance variables like this outside methods. I''m doing this with before_filter method, where I''m preparing variables like this. <code> class MyController < ApplicationController before_filter :prepare_user . . . def prepare_user @user = ... end end </code> Hope it helps :-). On 9/4/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks for the answer Oliver, but aren''t class variables shared among > all objects of a class? > I want this variable to be shared only within MyController object and > not all MyController objects. > > On 9/4/05, Oliver Legg <lists-XiLho7JcZycsV2N9l4h3zg@public.gmane.org> wrote: > > Class variables in ruby begin with a @@ so @@global_variable > > Hash.new should work. > > > > > > > Hello all. > > > I have a variable that I want to share between all methods in my > > > controller, but I don''t know how to do this. I''ve declared the > > > variable outside of any method of the controller (like in Java code), > > > but it doesn''t work. Can anyone help me? > > > > > > Here is some of the code I''m using: > > > > > > MyController < ApplicationController > > > > > > @global_variable = Hash.new > > > > > > def new > > > ... > > > end > > > > > > def edit > > > ... > > > end > > > > > > end > > > > > > Thanks in advance. > > > > > > Best regards, > > > Hugo > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Bogdan Ionescu
2005-Sep-04 13:29 UTC
Re: Where to put global variables - Beginner question
If i get your question right, you should define your hash in environment.db Like: $my_global_variable= Has.new() (or whatever) bogdan On 9/4/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello all. > I have a variable that I want to share between all methods in my > controller, but I don''t know how to do this. I''ve declared the > variable outside of any method of the controller (like in Java code), > but it doesn''t work. Can anyone help me? > > Here is some of the code I''m using: > > MyController < ApplicationController > > @global_variable = Hash.new > > def new > ... > end > > def edit > ... > end > > end > > Thanks in advance. > > Best regards, > Hugo > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Bogdan Ionescu
2005-Sep-04 13:30 UTC
Re: Where to put global variables - Beginner question
Sorry for the typos It''s environment.rb (and it''s found in the config directory of your rails project) Also its Hash instead of Has bogdan On 9/4/05, Bogdan Ionescu <bogdan.ionescu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > If i get your question right, you should define your hash in > environment.db > Like: > > $my_global_variable= Has.new() (or whatever) > > bogdan > > > On 9/4/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello all. > > I have a variable that I want to share between all methods in my > > controller, but I don''t know how to do this. I''ve declared the > > variable outside of any method of the controller (like in Java code), > > but it doesn''t work. Can anyone help me? > > > > Here is some of the code I''m using: > > > > MyController < ApplicationController > > > > @global_variable = Hash.new > > > > def new > > ... > > end > > > > def edit > > ... > > end > > > > end > > > > Thanks in advance. > > > > Best regards, > > Hugo > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hugo Magalhaes
2005-Sep-04 13:34 UTC
Re: Where to put global variables - Beginner question
Now I get it! I''m not used to build web applications. I think I''m going to use before_filter (thanks pepe), althought I didn''t want to create a Hash every time a request was made, but that''s life! :-) Thanks to all for your help. On 9/4/05, Patrick McCafferty <ryouga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You want what? A class variable shares a variable across all instances of a > class, that''s how it works... which is what you need to do if you want it to > persist between requests to your app, since a new instance of your class is > created for every request. > > I can''t even begin to think of how else to work with classes in a web > application context. If you want to store it for a given session store it in > the session. > > On 9/4/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Thanks for the answer Oliver, but aren''t class variables shared among > > all objects of a class? > > I want this variable to be shared only within MyController object and > > not all MyController objects. > > > > On 9/4/05, Oliver Legg < lists-XiLho7JcZycsV2N9l4h3zg@public.gmane.org> wrote: > > > Class variables in ruby begin with a @@ so @@global_variable > > > Hash.new should work. > > > > > > > > > > Hello all. > > > > I have a variable that I want to share between all methods in my > > > > controller, but I don''t know how to do this. I''ve declared the > > > > variable outside of any method of the controller (like in Java code), > > > > but it doesn''t work. Can anyone help me? > > > > > > > > Here is some of the code I''m using: > > > > > > > > MyController < ApplicationController > > > > > > > > @global_variable = Hash.new > > > > > > > > def new > > > > ... > > > > end > > > > > > > > def edit > > > > ... > > > > end > > > > > > > > end > > > > > > > > Thanks in advance. > > > > > > > > Best regards, > > > > Hugo > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 9/4/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Now I get it! I''m not used to build web applications. > I think I''m going to use before_filter (thanks pepe), althought I > didn''t want to create a Hash every time a request was made, but that''s > life! :-) ><snip from previous mes> I want this variable to be shared only within MyController object and not all MyController objects. </snip from previous mes> If object = instance, that there is no other way on web how safely do it. You can even have server restarted between requests for one user. If it''s for one user then go for sessions. If it depends on more things then before_filter is the way. Believe me before_filter and all call back stuff is one of the things, which is sending me to nirvana when programing in RoR ;-D. Every day!
Wouldn''t the declaration @global_variable = Hash.new just go in the initialize method? On 9/5/05, pepe <damn.pepe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 9/4/05, Hugo Magalhaes <hugo.mag-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Now I get it! I''m not used to build web applications. > > I think I''m going to use before_filter (thanks pepe), althought I > > didn''t want to create a Hash every time a request was made, but that''s > > life! :-) > > > > <snip from previous mes> > I want this variable to be shared only within MyController object and > not all MyController objects. > </snip from previous mes> > > If object = instance, that there is no other way on web how safely do > it. You can even have server restarted between requests for one user. > If it''s for one user then go for sessions. If it depends on more > things then before_filter is the way. > > Believe me before_filter and all call back stuff is one of the things, > which is sending me to nirvana when programing in RoR ;-D. > > Every day! > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails