tags:

views:

56

answers:

2

For instance, in Python, I can create a class like this:

class foo(object):
    bar = 'x'
    def __init__(self, some_value):
        self.some_attr = some_value

...where bar is a class attribute and some_attr is an instance attribute. What is the idiomatic way to do things like this in Ruby?

+6  A: 
class Foo

  @@bar = 'x'
  attr_accessor :some_attr

  def initialize(some_value)
    self.some_attr = some_value
  end

end
Simone Carletti
+1  A: 

Pretty much what weppos wrote, but I would use the @-sigil for the instance variable (it's common practice).

Like:

def initialize(some_value)
    @some_attr = some_value
end

Also, I would not name the class "Foo", but that has nothing do to with Ruby.

Johan
I wouldn't name it Foo either, but it's always the first thing that springs to mind when giving examples like these. :-)
Jason Baker
Yes, I get some of the reasons for foo/bar etc... It's good to have common names for examples I guess, I just don't like them because they are "ugly" names :P. Spam and eggs from the Python community are, if nothing else, funnier :)
Johan