Hello, I''m little confused about class variables class Polygon attr_accessor :sides @@sides = 10 end p Polygon.new.sides I get nil as return from sides?? but when I do this class Polygon @@sides = 10 def sides @@sides end end This work, but then I need to write all the accessors for every global variable? Regards, Jamal -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You want #cattr_accessor attr_accessor gives you: def var; @var; end def var=(val); @var = val; end while cattr_accessor gives you: def self.var; @@var; end def self.var=(val); @@var = val; end Which also means that you will normally do Polygon.sides. Jason On 4/2/07, Jamal Soueidan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hello, > > I''m little confused about class variables > > class Polygon > attr_accessor :sides > @@sides = 10 > end > > p Polygon.new.sides > > I get nil as return from sides?? > > but when I do this > > class Polygon > @@sides = 10 > def sides > @@sides > end > end > > This work, but then I need to write all the accessors for every global > variable? > > Regards, > Jamal > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> You want #cattr_accessor > > attr_accessor gives you: > > def var; @var; end > def var=(val); @var = val; end > > while cattr_accessor gives you: > > def self.var; @@var; end > def self.var=(val); @@var = val; end > > Which also means that you will normally do Polygon.sides. > > JasonWell I don''t want to write the set and get, I tought with attr_accessor they write it for you? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
attr_accessor writes out set/get for instance variables. cattr_accessor writes out set/get for class variables. Use those, and you don''t have to write out your own set/get Jason On 4/2/07, Jamal Soueidan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Jason Roelofs wrote: > > You want #cattr_accessor > > > > attr_accessor gives you: > > > > def var; @var; end > > def var=(val); @var = val; end > > > > while cattr_accessor gives you: > > > > def self.var; @@var; end > > def self.var=(val); @@var = val; end > > > > Which also means that you will normally do Polygon.sides. > > > > Jason > > Well I don''t want to write the set and get, I tought with attr_accessor > they write it for you? > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> attr_accessor writes out set/get for instance variables. > > cattr_accessor writes out set/get for class variables. > > Use those, and you don''t have to write out your own set/get > > JasonThanks Jason Roelofs :D -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> attr_accessor writes out set/get for instance variables. > > cattr_accessor writes out set/get for class variables. > > Use those, and you don''t have to write out your own set/get > > JasonI actually just try it out, undefined method cattr_accesstor? class Link cattr_accessor :url, :response def initialize(url) @@url = URI::parse(url) @@response = Net::HTTP.get(@@url) end end link = Link.new("http://www.google.com/") p link.response -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 4/2/07, Jamal Soueidan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Jason Roelofs wrote: > > attr_accessor writes out set/get for instance variables. > > > > cattr_accessor writes out set/get for class variables. > > > > Use those, and you don''t have to write out your own set/get > > > > Jason > > I actually just try it out, undefined method cattr_accesstor? > > class Link > > cattr_accessor :url, :response > > def initialize(url) > @@url = URI::parse(url) > @@response = Net::HTTP.get(@@url) > end > > end > > link = Link.new("http://www.google.com/") > p link.responseHmm, as you are posting to the Rails mailing list, I assumed the Rails environment. You can include ActiveSupport for yourself to use these methods. require ''rubygems'' require ''active_support'' Jason --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---