Need help with this...

Discussion in 'Ruby' started by kiranhk@gmail.com, Apr 18, 2006.

  1. Guest

    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
     
    , Apr 18, 2006
    #1
    1. Advertising

  2. Guest

    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
     
    , Apr 18, 2006
    #2
    1. Advertising

  3. Guest

    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.
     
    , Apr 18, 2006
    #3
  4. Ross Bamford Guest

    On Tue, 18 Apr 2006 20:53:05 +0100, <>
    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 -
     
    Ross Bamford, Apr 18, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,506
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    787
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    433
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    603
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    536
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page