Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Return object

    (Inspired by Test Driven Development of Kent Bek)
    How Can I return from a class method object of the class ? Like this:
    class Dollar
    attr_reader :amount
    def initialize(amount)
    @amount = amount
    end
    def times(multiplier)
    return Dollar.new(@amount * multiplier) #??????????
    end
    end


    Dmitry N Orlov Guest

  2. Similar Questions and Discussions

    1. Can I return a query object from a file upload usingFlex 3?
      Hi, I am using Flex 3 and I would be quite grateful if someone can assist me here. I am trying to find out if I can return a query back to flex...
    2. return object instances
      hello, since I'm new to the world of object oriented programming I hope I use the right terms for the concerned things, but I'll try. I was...
    3. Object property with different return types
      I have a control 'MyControl' with a property of type 'MyObject'. MyObject has a property 'MyField' that is a enum type. I want the type of enum...
    4. Can you return a dictionary object from a function?
      "Mark Schupp" <mschupp@ielearning.com> wrote in message news:OwPxqtxPDHA.1748@TK2MSFTNGP11.phx.gbl... doh!
  3. #2

    Default Re: Return object

    il Sun, 9 Nov 2003 09:37:42 +0500, "Dmitry N Orlov"
    <NOSPAM_orlovdn@cipro.uz> ha scritto::
    >(Inspired by Test Driven Development of Kent Bek)
    >How Can I return from a class method object of the class ? Like this:
    >class Dollar
    > attr_reader :amount
    > def initialize(amount)
    > @amount = amount
    > end
    > def times(multiplier)
    > return Dollar.new(@amount * multiplier) #??????????
    > end
    >end
    >
    well, times is not a class method:
    >> class Dollar
    >> attr_reader :amount
    >> def initialize(amount)
    >> @amount = amount
    >> end
    >> def times(multiplier)
    >> return Dollar.new(@amount * multiplier) #??????????
    >> end
    >> end
    => nil
    >> Dollar.times(1)
    NoMethodError: undefined method `times' for Dollar:Class
    from (irb):10

    and @amount is an instance variable:
    >> a=Dollar.new(10)
    => #<Dollar:0x2806c68 @amount=10>
    >> a.times(10)
    => #<Dollar:0x2804178 @amount=100>

    as you can see everything is fine :)
    but probably you may want to use

    def Dollar.times
    ....
    end
    or
    def self.times
    ....
    end
    and @@amount ?
    gabriele renzi Guest

  4. #3

    Default Re: Return object

    gabriele renzi <surrender_it@remove.yahoo.it> wrote in message news:<lv7sqv86nbca1r62cf2hud3u952uv103ov@4ax.com>. ..
    I just want return object of the class from object-method times(). See
    TDD, please, to understand me :)
    Dmitry N Orlov Guest

  5. #4

    Default Re: Return object

    [email]orlovdn@rambler.ru[/email] (Dmitry N Orlov) wrote in message news:<45323c22.0311092310.56710a4e@posting.google. com>...
    > gabriele renzi <surrender_it@remove.yahoo.it> wrote in message news:<lv7sqv86nbca1r62cf2hud3u952uv103ov@4ax.com>. ..
    > I just want return object of the class from object-method times(). See
    > TDD, please, to understand me :)
    Sorry. It's fine

    class Dollar
    attr_reader :amount
    def initialize(amount)
    @amount = amount
    end
    def times(multiplier)
    return Dollar.new(@amount * multiplier) #??????????
    end
    end

    five = Dollar.new(5)
    product = five.times(6)
    #Now product is a Instance of Dollar
    p product.inspect
    p five.inspect
    p (product.times(10).inspect)
    p product.inspect
    p five.inspect

    #<Dollar:0x2787810 @amount=30>
    #<Dollar:0x27878e8 @amount=5>
    #<Dollar:0x2787750 @amount=300>
    #<Dollar:0x2787810 @amount=30>
    #<Dollar:0x27878e8 @amount=5>
    Dmitry N Orlov Guest

  6. #5

    Default Re: Return object

    il 10 Nov 2003 05:32:34 -0800, [email]orlovdn@rambler.ru[/email] (Dmitry N Orlov) ha
    scritto::
    >orlovdn@rambler.ru (Dmitry N Orlov) wrote in message news:<45323c22.0311092310.56710a4e@posting.google. com>...
    >> gabriele renzi <surrender_it@remove.yahoo.it> wrote in message news:<lv7sqv86nbca1r62cf2hud3u952uv103ov@4ax.com>. ..
    >> I just want return object of the class from object-method times(). See
    >> TDD, please, to understand me :)
    >Sorry. It's fine
    well, I supposed this :)

    BTW you don't need to do
    p object.inspect
    p object
    is enough

    Kernel#p calls argument#inspect by itself, that's what is for :)


    gabriele renzi Guest

Posting Permissions

  • You may not post new threads
  • You may 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