Hi everyone, I have a question about class variable in Rails: I have a class Point that contains an class variable @@color_points="green" In a first web Page I want to set up this variable, hence i do Point.color_points = "green" then i test it using puts Point.color_points ( = green, it works,yes!) Then i have a link_to instruction that points to another web page. On this one (the other one), I just want to print Point.color_points, but now its value is "". DOes anyone know why and could explain me? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 28 August 2011 12:16, thelo.g thelo <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> In a first web Page I want to set up this variable > then i test it using puts, it works,yes! > Then i have a link_to instruction that points to another web page. > but now its value is "". > > DOes anyone know why and could explain me?Each request is isolated. At the end of the first request, all the variables you''ve set disappear into the ether. If you want something to persist, you need to store it - in the DB, the session, or somewhere else. The second request builds up new instances of all the objects - so the class variable is back to its default value. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
thelo.g thelo wrote in post #1018888:> Hi everyone, > > I have a question about class variable in Rails: > > I have a class Point that contains an class variable > @@color_points="green" > > In a first web Page I want to set up this variable, hence i do > Point.color_points = "green" then i test it using puts > Point.color_points ( = green, it works,yes!) > Then i have a link_to instruction that points to another web page. > On this one (the other one), I just want to print Point.color_points, > but now its value is "". > > DOes anyone know why and could explain me?In addition, you shouldn''t be using class variables. You can forget they exist. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
class Dog @count = 0 def initialize Dog.count += 1 end def self.count @count end def self.count=(val) @count = val end end d1 = Dog.new puts Dog.count d2 = Dog.new puts Dog.count --output:-- 1 2 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 28 August 2011 14:55, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> In addition, you shouldn''t be using class variables. You can forget > they exist.Bwah! Hahahah! What tosh... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 28, 3:14 pm, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> class Dog > @count = 0 > > def initialize > Dog.count += 1 > end > > def self.count > @count > end > > def self.count=(val) > @count = val > end > > endIf you want to be entirely nit-picky those are class instance variables rather than class variables. Fred> > d1 = Dog.new > puts Dog.count > > d2 = Dog.new > puts Dog.count > > --output:-- > 1 > 2 > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote in post #1018902:> On 28 August 2011 14:55, 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> In addition, you shouldn''t be using class variables. You can forget >> they exist. > > Bwah! Hahahah! > > What tosh... >Live and learn. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.