user defined instance variables

Discussion in 'Ruby' started by John Maclean, Jan 5, 2006.

  1. John Maclean

    John Maclean Guest

    Hey Chaps,
    So far so good. I've a menu as shown below. So far by following the "pick axe" I've been able to "hard code" all of my instance variables. I'd like a user to enter his/her details and let his entry specify that variable? Make sense?

    #!/usr/bin/ruby
    # Thu Jan 5 20:41:45 GMT 2006
    # defining a new class for inspectors
    class Inpsector
    def welcome_inspector
    puts "Welcome to the inspector screen"
    puts "~~~~~~~ ~~ ~~~ ~~~~~~~~~ ~~~~~~"
    print "0:- Inspectors, ready to enter your details?\nq:- quit!\n"
    # all we want to do here is collect the inspector's details
    end

    def initialize(fname, sname, company, dept, team, empno, mobileno)
    # these are the instance variables
    @fname = fname
    @sname = sname
    @company = company
    @dept = dept
    @team = team
    @empno = empno
    @mobileno = mobileno
    end

    def insp_choice
    print "enter your choice (0,q) : "
    ip = $stdin.gets

    if ip.chomp! =~ /^[0q]/
    case ip
    when "0"
    print "Entering details capturing session\n"
    when "q"
    print "about to quit!\n"
    end
    else
    print "poor choice!\n"
    end
    end

    def to_s
    "Inpsector: #@fname #@sname #@company, #@dept, #@team, #@empno, #@mobileno\n"
    end
    end

    # todo:- replace this "hard-coded" way of enteringinstance variables
    # with those that the user has entered
    inspector = Inpsector.new("John", "Mac", "t4m", "insp", "bcv", "666", "07666") #<--------- here
    inspector.inspect
    inspector.welcome_inspector
    inspector.insp_choice
    puts inspector



    --
    John Maclean
    MSc (DIC)
    07739 171 531
    John Maclean, Jan 5, 2006
    #1
    1. Advertising

  2. John Maclean

    Guest

    Hi --

    On Fri, 6 Jan 2006, John Maclean wrote:

    > Hey Chaps,
    > So far so good. I've a menu as shown below. So far by following the
    > "pick axe" I've been able to "hard code" all of my instance
    > variables. I'd like a user to enter his/her details and let his
    > entry specify that variable? Make sense?


    Can't you just get keyboard input and initialize the object with that?

    print "First name: "
    fname = gets.chomp

    etc.

    > print "Entering details capturing session\n"


    Don't forget about puts :)


    David

    --
    David A. Black


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black
    , Jan 6, 2006
    #2
    1. Advertising

  3. On Jan 5, 2006, at 5:35 PM, John Maclean wrote:

    > Hey Chaps,
    > So far so good. I've a menu as shown below. So far by following the
    > "pick axe" I've been able to "hard code" all of my instance
    > variables. I'd like a user to enter his/her details and let his
    > entry specify that variable? Make sense?


    Some idea, in code:

    class Inspector
    WELCOME = <<-END_WELCOME.gsub(/^\s+/, "").chomp
    Welcome to the inspector screen
    ~~~~~~~ ~~ ~~~ ~~~~~~~~~ ~~~~~~
    0:- Inspectors, ready to enter your details?
    q:- quit!
    enter your choice (0,q) :
    END_WELCOME
    DETAILS = %w{fname sname company dept team empno mobileno}

    def initialize
    DETAILS.each { |var| instance_variable_set("@#{var}", nil) }
    end

    def enter?( input )
    case input
    when "0"
    true
    else
    false
    end
    end

    def build
    DETAILS.each do |var|
    answer = yield var
    instance_variable_set("@#{var}", answer) unless answer.empty?
    end
    end
    end

    if __FILE__ == $0
    inspector = Inspector.new
    p inspector

    print Inspector::WELCOME
    if inspector.enter? $stdin.gets.to_s.chomp
    puts "Entering details capturing session"
    inspector.build do |var|
    print "#{var}: "
    $stdin.gets.to_s.chomp
    end
    else
    puts "about to quit!"
    end

    p inspector
    end

    Hopefully that gets you going.

    James Edward Gray II
    James Edward Gray II, Jan 6, 2006
    #3
    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. Replies:
    10
    Views:
    35,842
    jporter892
    Jun 6, 2011
  2. Oodini
    Replies:
    1
    Views:
    1,750
    Keith Thompson
    Sep 27, 2005
  3. Replies:
    1
    Views:
    481
    Sion Arrowsmith
    Jul 10, 2008
  4. Eric D.
    Replies:
    3
    Views:
    175
    Jeremy Henty
    Feb 1, 2006
  5. Tammo Tjarks
    Replies:
    2
    Views:
    275
    Tammo Tjarks
    Sep 13, 2007
Loading...

Share This Page