Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Need help with this...

Reply
Thread Tools

Need help with this...

 
 
kiranhk@gmail.com
Guest
Posts: n/a
 
      04-18-2006
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

 
Reply With Quote
 
 
 
 
rmagick@gmail.com
Guest
Posts: n/a
 
      04-18-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
kiranhk@gmail.com
Guest
Posts: n/a
 
      04-18-2006
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.

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      04-18-2006
On Tue, 18 Apr 2006 20:53:05 +0100, (E-Mail Removed) <(E-Mail Removed)>
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 - (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments