Professional Web Applications Themes

noobie question - module/class vs obj - Ruby

Hi all, Couple questions 1) I'm still pretty new to ruby.In my code i'm using quite a few classes and i do not need multiple instances of most of them. So in lieu of creating a global object $obj = new SomeClass, i simply use the Class and it's class methods/atts directly. Is this ok/good practice? Should I just use these as modules in this case? In simular situations w/javscript i do this: instead of: function class(){ this.prop = 0 } obj = new class ... I use obj = { prop : 0 } Which is kinda what i'm ...

  1. #1

    Default noobie question - module/class vs obj

    Hi all,
    Couple questions
    1) I'm still pretty new to ruby.In my code i'm using quite a few classes
    and i do not need multiple instances of most of them. So in lieu of
    creating
    a global object $obj = new SomeClass, i simply use the Class and
    it's class methods/atts directly. Is this ok/good practice? Should I
    just use these as modules in this case?
    In simular situations w/javscript i do this:
    instead of:
    function class(){
    this.prop = 0

    }
    obj = new class
    ... I use
    obj = {
    prop : 0
    }
    Which is kinda what i'm shooting for in Ruby.

    2) I thot i would include a module with some generic class methods into
    a class ie:
    module Somemodule
    self.boo
    puts 'boo'
    end
    end
    class SomeClass
    include Somemodule

    self.boohoo

    # puts boo #dies cant find SomeClass method boo
    puts Somemodule.boo #works ok

    end
    end

    Should'nt the include pull those class meths into SomeClass?Am i causing
    this issue/confusion by not intantiating objects from these classes?
    Shoul I make this class
    a module. I think i'll go try that....hmmm.

    Thanks for any inight,paul


    Paul Guest

  2. #2

    Default Re: noobie question - module/class vs obj

    Paul Vudmaska wrote: 

    Well, in JS that's creating an Object literal, rather than creating a
    class. The equivalents in ruby would be either a Hash literal:

    obj = { 'prop' => 0 }

    or a Singleton design.

    See:

    http://phrogz.net/ProgrammingRuby/frameset.asp?content=intro.asp%23arraysandhashes
    and
    http://phrogz.net/ProgrammingRuby/frameset.asp?content=language.asp%23hashes

    for information on Hashes, and

    http://phrogz.net/ProgrammingRuby/frameset.asp?contenttut_classes.asp%23singletonsan dotherconstructors
    and
    http://phrogz.net/ProgrammingRuby/frameset.asp?content=lib_patterns.asp%23singleton

    for information on Singletons.

     

    If you're talking about instance methods, you want:

    module Somemodule
    def boo
    puts 'boo'
    end
    end
    class SomeClass
    include Somemodule
    def boohoo
    boo
    end
    end


    If you're talking about class methods, then you want:

    module Somemodule
    def self.boo
    puts 'boo'
    end
    end
    class SomeClass
    include Somemodule
    def self.boohoo
    self.class.boo
    end
    end
    Gavin Guest

  3. #3

    Default Re: noobie question - module/class vs obj

    Gavin Kistner wrote: 

    Oops...I thought that would work. Frankly...I'm a beginner to, and
    apparently I don't know how (or if) you can call an included class
    method as part of a 'native' class method.
    Gavin Guest

Similar Threads

  1. Flex Noobie Question - Remote Object
    By projectproofing in forum Macromedia Flex General Discussion
    Replies: 5
    Last Post: July 8th, 05:26 PM
  2. RFC: new module Class::MakeIntrospecMethods
    By Dominique Dumont in forum PERL Modules
    Replies: 0
    Last Post: October 18th, 03:30 PM
  3. Noobie question
    By Matt_MA in forum Macromedia Freehand
    Replies: 3
    Last Post: February 26th, 07:39 PM
  4. noobie question
    By Mark in forum Macromedia Fireworks
    Replies: 5
    Last Post: December 12th, 02:29 PM
  5. One Module for one Class?
    By Jeremy Whetzel in forum Ruby
    Replies: 3
    Last Post: August 22nd, 04:02 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139