# Beginner question on classes on different files

**URL:** <https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295>\
**Category:** Ruby\
**Created:** [January 31, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295 "2006-01-31T17:15:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elriba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9f8e36/32.png) [@elriba](https://www.ruby-forum.com/u/elriba)\
**Post date:** [January 31, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295/1 "2006-01-31T17:15:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elriba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9f8e36/32.png) [@elriba](https://www.ruby-forum.com/u/elriba)\
**Post date:** [January 31, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295/2 "2006-01-31T17:21:16Z")

</div>

On 1/31/06, Edgard R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> end  
> 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’

–

---

<div class="post-metadata">

**Author:** ![elriba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9f8e36/32.png) [@elriba](https://www.ruby-forum.com/u/elriba)\
**Post date:** [January 31, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295/3 "2006-01-31T17:36:37Z")

</div>

Edgard R. wrote:

> 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?

---

<div class="post-metadata">

**Author:** ![elriba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9f8e36/32.png) [@elriba](https://www.ruby-forum.com/u/elriba)\
**Post date:** [January 31, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295/4 "2006-01-31T19:25:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elriba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9f8e36/32.png) [@elriba](https://www.ruby-forum.com/u/elriba)\
**Post date:** [January 31, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295/5 "2006-01-31T21:04:14Z")

</div>

Edgard R. 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

---

<div class="post-metadata">

**Author:** ![elriba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9f8e36/32.png) [@elriba](https://www.ruby-forum.com/u/elriba)\
**Post date:** [January 31, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/beginner-question-on-classes-on-different-files/53295/6 "2006-01-31T21:21:48Z")

</div>

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
