Ask A Question

Notifications

You’re not receiving notifications from this thread.

Seeking guidance from above!

Tai PAUL asked in Ruby

Hi there, I'm new to ruby and seeking help from better/experienced peers for guidance.
I'm facing an issue with executing in creatings an array for the scenario of train data.
My current skeleton for ruby is below and will appreciate if anyone can help in commenting what I should be correcting or where I should be focus on.

I've broken my code into 3 sections if to help in clarity of what I need to do for each command (hope to hear advice from one and all!)
Thanks in advance and cheers for your time (I hope that this code is almost done but with coding its hard to tell sometimes)

---==== start of code ===---

Section 1. Train data from user - Function called read_train() that reads from the user input values for each of the fields in a train record and returns the completed record. Also included: Trains Array Data - function called read_trains() that calls the read_train() and returns an array of each input train.

def read_train()
train_name = read_string('Enter train name:')
train_id = read_integer('Enter train id:')
origin = read_string('Enter station of origin:')
destination = read_string('Enter final destination station:')
end

def read_trains()
trains = Array.new()
index = 0
while index < quantity
train = read_train()
trains << train
index += 1
end
return trains
end

Section 2. Print train data - function called print_train(train) that takes a train record and writes each of the fields to the terminal with a description for the field as well as the field value. Also included: Print collated trains - function called print_trains(trains); that calls the print_train(train) procedure for each train in the array.

def print_train(train)
puts(train_name)
puts(train_id)
puts(origin)
puts(destination)
end

def print_trains(trains)
count = quantity
index = 0
while index < count
print_train
index += 1
end
end

Section 3. Main program - Write a program to execute the following scenario needed.

def main()
quantity = read_integer('How many trains are you entering:')
read_trains = read_train
trains = read_trains()
print_trains(trains)
end

---==== end of code ===---

Reply

I do have another input_functions file rb. so that the system understands the syntax I'm more familiar with, apologies for any confusion!

Display the prompt and return the read string

def read_string prompt
puts prompt
value = gets.chomp
end

Display the prompt and return the read float

def read_float prompt
value = read_string(prompt)
value.to_f
end

Display the prompt and return the read integer

def read_integer prompt
value = read_string(prompt)
value.to_i
end

Read an integer between min and max, prompting with the string provided

def read_integer_in_range(prompt, min, max)
value = read_integer(prompt)
while (value < min or value > max)
puts "Please enter a value between " + min.to_s + min.to_s + ": "
value = read_integer(prompt);
end
value
end

Display the prompt and return the read Boolean

def read_boolean prompt
value = read_string(prompt)
case value
when 'y', 'yes', 'Yes', 'YES'
true
else
false
end
end

Test the functions above

=begin
def main
puts "String entered is: " + read_string("Enter a String: ")
puts "Boolean is: " + read_boolean("Enter yes or no:").to_s
puts "Float is: " + read_float("Enter a floating point number: ").to_s
puts "Integer is: " + read_integer_in_range("Enter an integer between 3 and 6: ", 3, 6).to_s
end

main
=end

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.