[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Beginner question on classes on different files

Edgard Riba

1/31/2006 4:15:00 PM

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


6 Answers

Tanner Burson

1/31/2006 4:19:00 PM

0

On 1/31/06, Edgard Riba <elriba@rimith.com> 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?
> Thansk, Edgard


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

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


--
===Tanner Burson===
tanner.burson@gmail.com
http://tanner... <---Might even work one day...

Stefan Mahlitz

1/31/2006 4:34:00 PM

0

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?


Edgard Riba

1/31/2006 6:26:00 PM

0

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


Eero Saynatkari

1/31/2006 8:04:00 PM

0

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


Edgard Riba

1/31/2006 8:22:00 PM

0

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


rfischer

7/13/2010 4:06:00 AM

0

Walter Harding <gopartyanimal@gmail.com> wrote:
>On Jul 11, 10:35?pm, "5584 Dead, 717 since 1/20/09"
><dea...@deadduz.com> wrote:
>> On Sun, 11 Jul 2010 20:53:24 -0700, Walter Harding wrote:
>> >> (BTW, on the truth front, the reason we have 12 million or whatever
>> >> number of undocumented guest workers is because they are TRAPPED here.
>> >> Even if they COULD cross the border to return to their families easily,
>> >> there's a war going down on that side!)
>>
>> > And the reason we want that war to extend into America is...?
>>
>> The vast majority of immigrants in the US want nothing at all to do with
>> that war. ?
>
>Well, by definition, they have no respect out American laws.

Something you have in common with them.

> They
>want to take desperately needed jobs by Americans?

Jobs that Americans do not want.

> Further overload
>our educational system?

Adults don't use the education system.

> Help collapse our medical system?

How?!?

--
Ray Fischer
rfischer@sonic.net