beginning question. modules + object creation

Discussion in 'Ruby' started by Corey Konrad, Mar 18, 2007.

  1. Corey Konrad

    Corey Konrad Guest

    I was wondering if i create a modules file called mathematics.rb:

    module Mathematics
    class Add
    def add(operand_one, operand_two)
    return operand_one + operand_two
    end

    end#end class
    end

    and then i create another file called usemodules.rb

    require 'mathematics'


    adder = Add.new
    puts "2 + 3 = " + adder.add(2, 3).to_s

    how do i create an Add object in this new file, i understand i could
    just make the add method above into a class method and then i wouldnt
    need to create an object at all. However how would i create an object to
    use.

    thanks

    --
    Posted via http://www.ruby-forum.com/.
    Corey Konrad, Mar 18, 2007
    #1
    1. Advertising

  2. Corey Konrad

    Corey Konrad Guest

    Corey Konrad wrote:
    > I was wondering if i create a modules file called mathematics.rb:
    >
    > module Mathematics
    > class Add
    > def add(operand_one, operand_two)
    > return operand_one + operand_two
    > end
    >
    > end#end class
    > end
    >
    > and then i create another file called usemodules.rb
    >
    > require 'mathematics'
    >
    >
    > adder = Add.new
    > puts "2 + 3 = " + adder.add(2, 3).to_s
    >
    > how do i create an Add object in this new file, i understand i could
    > just make the add method above into a class method and then i wouldnt
    > need to create an object at all. However how would i create an object to
    > use.
    >
    > thanks


    never mind i figured it out on my own i would do it like
    Mathematics::Add.new
    zen and the art of programming as soon as i shut my brain off by asking
    a question the answer pops in my head, lol.

    --
    Posted via http://www.ruby-forum.com/.
    Corey Konrad, Mar 18, 2007
    #2
    1. Advertising

  3. Corey Konrad

    Gary Wright Guest

    On Mar 18, 2007, at 6:38 PM, Corey Konrad wrote:

    > I was wondering if i create a modules file called mathematics.rb:
    >
    > module Mathematics
    > class Add
    > def add(operand_one, operand_two)
    > return operand_one + operand_two
    > end
    >
    > end#end class
    > end
    >
    > and then i create another file called usemodules.rb
    >
    > require 'mathematics'
    >
    >
    > adder = Add.new
    > puts "2 + 3 = " + adder.add(2, 3).to_s


    Because you nested your class Add within Mathematics you have to
    use the fully qualified name when you are writing code that
    is outside the Mathematics module:

    adder = Mathematics::Add.new
    puts "2 + 3 = #{adder.add(2,3)}"

    Gary Wright
    Gary Wright, Mar 18, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jon wayne
    Replies:
    9
    Views:
    695
    Jim Langston
    Sep 22, 2005
  2. Replies:
    2
    Views:
    880
    Frederick Gotham
    Jul 8, 2006
  3. Marcel Müller
    Replies:
    10
    Views:
    481
    Alf P. Steinbach
    Mar 31, 2009
  4. DaveB
    Replies:
    49
    Views:
    1,913
    DaveB
    Apr 12, 2010
  5. Jesse B.
    Replies:
    9
    Views:
    219
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page