hello sharma,
i think you'll be able to figure this out better if you follow some
simple formatting guidelines that make reading code much easier for
others and for you! it's a good idea to indent each block, making it
easier to follow the flow of the code to spot missing `end`s that wreak
havoc. something like:
class Engine
def initialize(...)
...
if ...
...
...
end
...
...
end
end
a good habit to get into is that whenever you make a call that
requires an end statement, write the `end` immediately after making the
call, before writing any of the code that goes in between - that way
you're sure that you close every loop or block that you open... start
like this:
class Engine
end
...and not like this:
class Engine
def inialize
...if you indent your above code, you'll see that there are a lot of
missing `ends` and it is pretty 'all over the place...' maybe try
starting over - and start simply...
if i understand correctly, what you're doing here is making a
salesperson who asks if we are a current customer or not before making a
sale. so start by making a salesperson...
class Salesperson
end
it's not much, but it won't give you any errors! now, the first thing
we want this guy to do when we call him is to ask if the person at the
other end of the terminal is a current client, right?
class Salesperson
def initialize(name)
puts "Hi, I'm #{name}, are you a current customer? (y/n) "
answer = gets.chomp.downcase
if answer == "y"
sell_to_customer
elsif answer == "n"
register_new_customer
else
raise "answer must be 'y' or 'n'"
end
end
def sell_to_customer
end
def register_new_customer
end
end
joe = Salesman.new("Joe")
...so, we create a salesman called joe, and he asks us if we're a
current customer or not. if we are, a sell_to_customer method is
called, otherwise a register_new_customer method is called - these are
both things that salespeople do every day...
write little bits of code and run it often to see where it breaks...
notice that you can call methods before writing them fully - it's enough
to just define and end them, to make sure you're getting that far. so
after running this, the next thing you could think of is what a
salesperson does when he or she registers a new customer...
first they ask you questions, and then they record that information
(not the other way around, this is important!) so back in the
Salesperson class add some stuff to the register_new_customer method:
def register_new_customer
puts "What's your first name please: "
fname = gets.chomp
end
...this gets a string from the terminal and then assigns it to a
variable - the order is important, you can't call a variable before
you've assigned it.
then you could put all the info you get into an array:
def register_new_customer
puts "What's your first name please: "
fname = gets.chomp
customer_info = []
customer_info << fname
end
...and send that array off to update your csv file.
take things step by step, and try to do things as simply as possible -
i'm sure you'll figure it out ;)
good luck -
- j
--
Posted via http://www.ruby-forum.com/.