Beginner question on classes on different files

Discussion in 'Ruby' started by Edgard Riba, Jan 31, 2006.

  1. Edgard Riba

    Edgard Riba Guest

    Hi,
    I'm beginning to work with ruby and I have a conceptual problem. Let's
    say I have two files:

    hello2.rb:
    class Hello2
    attr_reader :msg
    def initialize
    @msg = "Hello, World2"
    end
    end

    and hello.rb

    require 'hello2.rb'
    class Hello
    attr_reader :msg
    def initialize
    @msg = "Hello, World"
    end
    end
    h = Hello.new()
    h2 = Hello2.new()
    puts h2.msg
    puts h.msg
    print "Press RETURN"
    $stdin.gets

    This is obviously a conceptual excercise. I want to use class
    "Hello2" in my hello.rb code. However this doesn't compile.

    What is wrong with this code? How is something like this done in Ruby?
    Thansk, Edgard

    --
    Posted via http://www.ruby-forum.com/.
    Edgard Riba, Jan 31, 2006
    #1
    1. Advertising

  2. ------=_Part_7947_2159098.1138724344402
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 1/31/06, Edgard Riba <> wrote:
    >
    > Hi,
    > I'm beginning to work with ruby and I have a conceptual problem. Let's
    > say I have two files:
    >
    > hello2.rb:
    > class Hello2
    > attr_reader :msg
    > def initialize
    > @msg =3D "Hello, World2"
    > end
    > end
    >
    > and hello.rb
    >
    > require 'hello2.rb'
    > class Hello
    > attr_reader :msg
    > def initialize
    > @msg =3D "Hello, World"
    > end
    > end
    > h =3D Hello.new()
    > h2 =3D Hello2.new()
    > puts h2.msg
    > puts h.msg
    > print "Press RETURN"
    > $stdin.gets
    >
    > This is obviously a conceptual excercise. I want to use class
    > "Hello2" in my hello.rb code. However this doesn't compile.
    >
    > What is wrong with this code? How is something like this done in Ruby?
    > Thansk, Edgard



    Drop the .rb from you're require line. So it should be
    require 'hello2'

    --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    =3D=3D=3DTanner Burson=3D=3D=3D

    http://tannerburson.com <---Might even work one day...

    ------=_Part_7947_2159098.1138724344402--
    Tanner Burson, Jan 31, 2006
    #2
    1. Advertising

  3. Edgard Riba wrote:
    > Hi,
    > I'm beginning to work with ruby and I have a conceptual problem. Let's
    > say I have two files:
    >
    > hello2.rb:
    > class Hello2
    > attr_reader :msg
    > def initialize
    > @msg = "Hello, World2"
    > end
    > end
    >
    > and hello.rb
    >
    > require 'hello2.rb'
    > class Hello
    > attr_reader :msg
    > def initialize
    > @msg = "Hello, World"
    > end
    > end
    > h = Hello.new()
    > h2 = Hello2.new()
    > puts h2.msg
    > puts h.msg
    > print "Press RETURN"
    > $stdin.gets
    >
    > This is obviously a conceptual excercise. I want to use class
    > "Hello2" in my hello.rb code. However this doesn't compile.
    >
    > What is wrong with this code? How is something like this done in Ruby?


    Works for me under linux and windows.

    Prints

    > Hello, World2
    > Hello, World
    > Press RETURN


    What exactly does not work?
    Stefan Mahlitz, Jan 31, 2006
    #3
  4. Edgard Riba

    Edgard Riba Guest

    Hi Stefan,

    I'm using FreeRuby, and I'm getting intermitent errors.

    This is the error I get
    >ruby c:/ruby/samples/hello.rb

    c:/ruby/samples/hello.rb:11: parse error, unexpected tIDENTIFIER,
    expecting $
    h2 = Hello2.new()^Mputs h2.msg^M
    ^

    However, I placed semi-colons (';') after each line and everything
    compiled fine and ran with expected results:

    h = Hello.new();
    h2 = Hello2.new();

    puts h2.msg;
    puts h.msg;
    print "Press RETURN";
    $stdin.gets;


    Seems like some kind of problem with the parser. Maybe I have
    something setup wrong with respect to the end of line character or
    something like that...

    Thanks,
    Edgard

    --
    Posted via http://www.ruby-forum.com/.
    Edgard Riba, Jan 31, 2006
    #4
  5. Edgard Riba wrote:
    > Hi Stefan,
    >
    > I'm using FreeRuby, and I'm getting intermitent errors.
    >
    > This is the error I get
    >>ruby c:/ruby/samples/hello.rb

    > c:/ruby/samples/hello.rb:11: parse error, unexpected tIDENTIFIER,
    > expecting $
    > h2 = Hello2.new()^Mputs h2.msg^M
    > ^


    This looks like UNIX-style linebreaks (\n)
    that Windows does not interpret correctly.

    > However, I placed semi-colons (';') after each line and everything
    > compiled fine and ran with expected results:
    >
    > h = Hello.new();
    > h2 = Hello2.new();
    >
    > puts h2.msg;
    > puts h.msg;
    > print "Press RETURN";
    > $stdin.gets;
    >
    >
    > Seems like some kind of problem with the parser. Maybe I have
    > something setup wrong with respect to the end of line character or
    > something like that...


    There may be a setting in your editor that toggles
    the linebreak mode. I actually do not recall this
    having been a problem but it has been a while since
    I did any ruby development on Windows.

    > Thanks,
    > Edgard



    E

    --
    Posted via http://www.ruby-forum.com/.
    Eero Saynatkari, Jan 31, 2006
    #5
  6. Edgard Riba

    Edgard Riba Guest

    Hi Eero,

    >
    > This looks like UNIX-style linebreaks (\n)
    > that Windows does not interpret correctly.
    >


    This is what it was... I don't know why they were in there instead of
    CRLF pairs, but I found a way in FreeRide to show the end of line
    markers and was able to remove al single CR, and everything compiles
    fine now.

    Thanks to all,
    Edgard

    --
    Posted via http://www.ruby-forum.com/.
    Edgard Riba, Jan 31, 2006
    #6
    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. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    557
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. JHenstay
    Replies:
    3
    Views:
    540
    osmium
    Jan 11, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    Beginner question? Classes, variables, ...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, Jun 28, 2006, in forum: Python
    Replies:
    4
    Views:
    281
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    Jun 28, 2006
  4. Replies:
    1
    Views:
    317
    Alf P. Steinbach
    Jul 24, 2007
  5. Replies:
    1
    Views:
    312
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jul 24, 2007
Loading...

Share This Page