Need help with this...

K

kiranhk

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
 
R

rmagick

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
 
K

kiranhk

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

Ross Bamford

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top