SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class declaration

    So if I followed things correctly, Ruby (unlike PHP) does not accept procedural code.

    I've read the ressources but I'm looking for someone who would agree to start from scratch with me (so I can ask question). About building a class that is.

    1. How do you properly start a class.
    2. How should look the constructor (is it an obligation to have a constructor)?
    3. Are parent/private/protected keyowrds mandatory before member functions?
    4. Once you've built your class, how do you instantiate it?

    I know you could tell me to check the ressources etc. But honestly, I learn so much better with interactivity... If it's too lame, don't worry.

    Thanks in advance

  2. #2
    SitePoint Zealot bronze trophy
    Join Date
    Jun 2004
    Location
    Stockholm, Sweden
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you read this: Programming Ruby?
    If there is a way to overcome the suffering, there is no need to worry; if there is no way to overcome the suffering, there is no point to worry.
    - Shantideva

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. How do you properly start a class.

    class ClassName
    <definitions>
    end

    ClassName must start with uppercase and be in CamelCase

    2. How should look the constructor (is it an obligation to have a constructor)?

    def initialize(<arguments>)
    <code>
    end

    no, no obligation

    3. Are parent/private/protected keyowrds mandatory before member functions?

    no, default is public for methods and private for attributes

    4. Once you've built your class, how do you instantiate it?

    some_var = SomeClass.new(<arguments>)

  4. #4
    SitePoint Zealot bronze trophy
    Join Date
    Jun 2004
    Location
    Stockholm, Sweden
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ClassName doesn't have to be in CamelCase. Classnames must be Constant, which means they need to have a leading upper-case character. You could name it Foobar, Foo_bar, FOOBAR, FooBar, FoObAr etc.
    If there is a way to overcome the suffering, there is no need to worry; if there is no way to overcome the suffering, there is no point to worry.
    - Shantideva

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KTottE
    ClassName doesn't have to be in CamelCase. Classnames must be Constant, which means they need to have a leading upper-case character. You could name it Foobar, Foo_bar, FOOBAR, FooBar, FoObAr etc.
    This is true, though CamelCase seems to be the chosen convention.

    no, default is public for methods and private for attributes
    I *think* that attributes are always private in Ruby. If you want to access an attribute you must define a getter/setter. Fortunately, Ruby provides easy helper functions to do this:

    Code:
    class MyClass
      attr_reader :my_var # for read access
      attr_writer :my_var # for write access
      attr_accessor :my_var # does both of the above
    
      def initialize
        @my_var = 'my variable'
      end
    end
    
    example = MyClass.new
    example.my_var  # => 'my variable'
    example.my_var = 'something else'
    example.my_var  # => 'something else'
    Also, the way of defining method access is different in Ruby:

    Code:
    class MyOtherClass
     
      def my_method_public_by_default
    
      end
    
      protected
        def this_is_protected
    
        end
    
      private
        def this_is_private
    
        end
    end
    Of course, as has been pointed out, you should really read Programming Ruby. Much of what you need to know about classes to get started is here.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Technically it's true, but isn't camel case for classes (and underscores for methods) a de facto standard ?

  7. #7
    SitePoint Zealot bronze trophy
    Join Date
    Jun 2004
    Location
    Stockholm, Sweden
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    CamelCase:
    Yes, this is the de-facto standard (and recommended Ruby style). But they don't have to be.

    Access:
    There is no way to make member variables public. No way, no how. You can, as described, make methods that access them however. To give a little more insight to the attr_* methods, here's the manual version:

    Code:
    class MyClass
      @foo
      @bar
      @foobar
    
      # attr_reader
      def foo
        return @foo
      end
    
      # attr_writer
      def bar=(new_value)
        @bar = new_value
      end
    
      # attr_accessor
      def foobar
        return @foobar
      end
    
      def foobar=(new_value)
        @foobar = new_value
      end
    end
    
    my_obj = MyClass.new
    my_obj.foo #=> returns @foo
    my_obj.bar = 'foo' #=> sets @bar
    my_obj.foobar = 'barfoo' #=> sets @foobar
    my_obj.foobar #=> returns @foobar
    If you try to set the value of @foo, you'll get a method missing error. If you try to read the value of @bar, you'll get a method missing error.

    When you use the attr_* methods, what you're saying is "create these methods operating on the variable identified by this symbol.".

    If you want quick access to something you know won't change, the attr_* methods work just fine. But if you were to use the manual version, you could easily change the internal representation of the variable and make any necessary conversions in your accessor methods.

    Clients of the class continue to use it like before, you do whatever you want with the data inside the class.

    You can also create "virtual accessors" of course, using a different name than the internal data variable or maybe perform some operations on the data before you send it out.

    Code:
    class MyClass
      @foo # containing the numbers 123
      @bar # containing the numbers 456
    
      def foobar
        return @foo.to_s.concat(@bar.to_s)
      end
    end
    
    my_obj = MyClass.new
    puts my_obj.foobar #=> "123456"
    If there is a way to overcome the suffering, there is no need to worry; if there is no way to overcome the suffering, there is no point to worry.
    - Shantideva

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strictly speaking, attributes are not private (at least, in c++/java sense). It's fully possible to change an attribute of any object from outside (see http://www.rubycentral.com/book/ref_....instance_eval). However, the traditional 'object.attribute' accessor is syntactically impossible, because ruby always expects a method name after dot. I'd say attributes in ruby are "inaccessible", not "private".


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •