![]() |
newbie question - how to use multiple source files
I'm coming from Java, and am trying to understand how to break upo code into more than one file. So in this example, I have one class in the first file, another in the second file, and I want to create an object from the first class, but from the second file. Environment - Aptana on XP machine. See below: 1) First file named first_class_file.rb: class FirstClassFile public def initialize super puts "Hello from First Class" end end my1stObject = FirstClassFile.new 2) Second file named second_class_file.rb: class SecondClassFile def initialize super puts "Hello from Second Class" end end my2ndObject = SecondClassFile.new my3rdObject = FirstClassFile.new 3) Console result of running second file: Hello from Second Class second_class_file.rb:11: uninitialized constant FirstClassFile (NameError) Question - What obvious thing am I doing wrong? Thanks! - Ben |
Re: newbie question - how to use multiple source files
Subject: newbie question - how to use multiple source files
Date: lun 31 dic 07 02:36:28 +0900 Quoting Ben Hoffmann (bensshop@skypoint.com): > I'm coming from Java, and am trying to understand how to break upo code > into more than one file. So in this example, I have one class in the first > file, another in the second file, and I want to create an object from the > first class, but from the second file. Environment - Aptana on XP machine. In the second file, before referring to the FirstClassFile class, you must add require 'first_class_file' and first_class_file.rb has to be either in your current directory, or in a directory included in your search path. Carlo -- * Se la Strada e la sua Virtu' non fossero state messe da parte, * K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe * di parlare tanto di amore e di rettitudine? (Chuang-Tzu) |
Re: newbie question - how to use multiple source files
Ben Hoffmann wrote:
> > I'm coming from Java, and am trying to understand how to break upo code > into more than one file. So in this example, I have one class in the > first file, another in the second file, and I want to create an object > from the first class, but from the second file. Environment - Aptana on > XP machine. See below: Check out the "require" and "load" Kernel methods. The "require" method is what you want, but "load" also has its uses. -- RMagick: http://rmagick.rubyforge.org/ RMagick 2: http://rmagick.rubyforge.org/rmagick2.html |
Re: newbie question - how to use multiple source files
The second file must require the first:
require 'first_class_file' This would generally go at the top of the file, but anywhere before FirstClassFile is referenced is fine. The require method looks at each path in the $LOAD_PATH global variable, then attempts to find a Ruby source file or C extension that matches the name you pass in. Check out http://www.rubycentral.com/pickaxe/tut_modules.html#S4 for more details. In your example, the public and super calls are unnecessary. Public is the default visibility, and, as both of your classes inherit from Object, there's no need to call the initializer of the superclass. It's also quite common to have multiple Ruby classes in the same source file. Ruby classes are often small, and many people find it easier to manage a small handful of larger files than dozens of tiny ones. On Dec 30, 1:36 pm, Ben Hoffmann <benss...@skypoint.com> wrote: > I'm coming from Java, and am trying to understand how to break upo code > into more than one file. So in this example, I have one class in the > first file, another in the second file, and I want to create an object > from the first class, but from the second file. Environment - Aptana on > XP machine. See below: > > 1) First file named first_class_file.rb: > > class FirstClassFile > > public > > def initialize > super > puts "Hello from First Class" > end > end > > my1stObject = FirstClassFile.new > > 2) Second file named second_class_file.rb: > > class SecondClassFile > > def initialize > super > puts "Hello from Second Class" > end > end > > my2ndObject = SecondClassFile.new > > my3rdObject = FirstClassFile.new > > 3) Console result of running second file: > > Hello from Second Class > second_class_file.rb:11: uninitialized constant FirstClassFile (NameError) > > Question - What obvious thing am I doing wrong? > Thanks! > - Ben |
Re: newbie question - how to use multiple source files
On Dec 30, 6:36 pm, Ben Hoffmann <benss...@skypoint.com> wrote:
> I'm coming from Java, and am trying to understand how to break upo code > into more than one file. So in this example, I have one class in the > first file, another in the second file, and I want to create an object > from the first class, but from the second file. Environment - Aptana on > XP machine. See below: > > 1) First file named first_class_file.rb: > > class FirstClassFile > > public > > def initialize > super > puts "Hello from First Class" > end > end > > my1stObject = FirstClassFile.new > > 2) Second file named second_class_file.rb: > > class SecondClassFile > > def initialize > super > puts "Hello from Second Class" > end > end > > my2ndObject = SecondClassFile.new > > my3rdObject = FirstClassFile.new > > 3) Console result of running second file: > > Hello from Second Class > second_class_file.rb:11: uninitialized constant FirstClassFile (NameError) > > Question - What obvious thing am I doing wrong? > Thanks! > - Ben Coming from Java I presume that you are familiar with import directive. Ruby has "require" for the same job as stated before. |
Re: newbie question - how to use multiple source files
On Mon, 31 Dec 2007 02:36:28 +0900, Ben Hoffmann wrote:
> So in this example, I have one class in the first file, another in the > second file, and I want to create an object from the first class, Hi :) I'm coming from java, too. Heads up: one of the big but silly differences is to not capitalize file names, like Foo.rb, instead call it foo.rb. How about something like the following (note the output of "ruby driver.rb"): thufir@arrakis ~/Desktop/foobar $ ll total 12 -rw-r--r-- 1 thufir users 53 Dec 30 14:17 bar.rb -rw-r--r-- 1 thufir users 86 Dec 30 14:18 driver.rb -rw-r--r-- 1 thufir users 53 Dec 30 14:17 foo.rb thufir@arrakis ~/Desktop/foobar $ thufir@arrakis ~/Desktop/foobar $ ruby driver.rb hello from a foo hello from a bar thufir@arrakis ~/Desktop/foobar $ cat driver.rb require 'foo.rb' require 'bar.rb' foo = Foo.new foo.hello bar = Bar.new bar.hello thufir@arrakis ~/Desktop/foobar $ thufir@arrakis ~/Desktop/foobar $ cat foo.rb class Foo def hello puts "hello from a foo" end end thufir@arrakis ~/Desktop/foobar $ thufir@arrakis ~/Desktop/foobar $ cat bar.rb class Bar def hello puts "hello from a bar" end end thufir@arrakis ~/Desktop/foobar $ The driver, driver.rb, does all the work. You may be interested in the code at: http://code.google.com/p/dwemthys/source HTH, Thufir |
| All times are GMT. The time now is 04:41 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.