[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Need help with this...

kiranhk@gmail.com

4/18/2006 7:53:00 PM

I am new to Ruby and going thro' the Pragmatic Programmer. I am using
WIN XP.
I have been developing with Java for many years..
i have few questions...

1) is there anything like environment variable classpath(java) in RUBY
by which ruby will be able to find all the scripts which it needs while
running a script. I dont want to use the -S option when running ruby. I
want to create a environment variable which ruby should use while
running a script. Also will it include zip files ??
2) I am not getting the exact difference b/n String.split and
String.scan. I think split takes a RE and so will be able to do the
same as scan..

also pls help with the following program..


require 'WordIndex.rb'

class SongList
def initialize
@songs = Array.new
@index = WordIndex.new
end
def append(aSong)
@songs.push(aSong)
@index.index(aSong, aSong.name, aSong.artist)
self
end
def lookup(aWord)
@index.lookup(aWord)
end
end

class Song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
end

songs = SongList.new
songs.append Song.new("gone with the wind","kiran","2:34")
songs.append Song.new("test Sun","krishna","1:35")

songs.lookup("gone")

I am getting the following error when i run this..

C:/kiran/ruby/product.rb:13:in `append': undefined method `name' for
#<Song:0x28cca28> (NoMethodError)
from C:/kiran/ruby/product.rb:30


thanks
Kiran

3 Answers

Tim Hunter

4/18/2006 8:06:00 PM

0

kiranhk@gmail.com wrote:

>
> 1) is there anything like environment variable classpath(java) in RUBY
> by which ruby will be able to find all the scripts which it needs while
> running a script. I dont want to use the -S option when running ruby. I
> want to create a environment variable which ruby should use while
> running a script. Also will it include zip files ??

Use the RUBYLIB environment variable to specify a list of directories
that Ruby should search for files named as "require" arguments. Ruby
does not search zip files for programs.

> also pls help with the following program..
(pgm snipped)
> I am getting the following error when i run this..
>
> C:/kiran/ruby/product.rb:13:in `append': undefined method `name' for
> #<Song:0x28cca28> (NoMethodError)
> from C:/kiran/ruby/product.rb:30
>
>

In order to reference the @name variable from outside the instance you
must define an attribute "reader" method:

def name
@name
end

Or, more simply, add this to the class definition:

attr_reader :name


> thanks
> Kiran

kiranhk@gmail.com

4/18/2006 8:29:00 PM

0

thanks a lot..
I did find abt the RUBYLIB. but was wondering if there is anything else
to make it search in zip files..

yes.. now i remember @name by default will be private so i will not be
able to access it from outside. so dumb of me :( need to get some
coffee :)

thanks again for the help.

Ross Bamford

4/18/2006 9:12:00 PM

0

On Tue, 18 Apr 2006 20:53:05 +0100, kiranhk@gmail.com <kiranhk@gmail.com>
wrote:

> 2) I am not getting the exact difference b/n String.split and
> String.scan. I think split takes a RE and so will be able to do the
> same as scan..
>

"oneabtwoabthree".split(/ab/)
# => ["one", "two", "three"]

"oneabtwoabthree".scan(/ab/)
# => ["ab", "ab"]

"oneabtwoabthree".scan(/ab/) { |s| p s } # scan can take a block
"ab"
"ab"
# => "oneabtwoabthree"

"oneabtwoabthree".split(/ab/) { |s| p s } # block is just ignored
# => ["one", "two", "three"]

> also pls help with the following program..
>

Just a note regarding your reply to another message on this thread, @name
will _always_ be private (though private is rather less 'private' than you
might be used to).

--
Ross Bamford - rosco@roscopeco.remove.co.uk