# User defined instance variables

**URL:** <https://www.ruby-forum.com/t/user-defined-instance-variables/51701>\
**Category:** Ruby\
**Created:** [January 6, 2006, 12:37am UTC](https://www.ruby-forum.com/t/user-defined-instance-variables/51701 "2006-01-06T00:37:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 12:37am UTC](https://www.ruby-forum.com/t/user-defined-instance-variables/51701/1 "2006-01-06T00:37:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 2:16am UTC](https://www.ruby-forum.com/t/user-defined-instance-variables/51701/2 "2006-01-06T02:16:09Z")

</div>

Hi –

On Fri, 6 Jan 2006, John M. 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  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 6, 2006, 2:58am UTC](https://www.ruby-forum.com/t/user-defined-instance-variables/51701/3 "2006-01-06T02:58:12Z")

</div>

On Jan 5, 2006, at 5:35 PM, John M. 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 G. II
