how to pass dynamic values for time in the cmd - ruby

I have already tried this, and it returns the present time,
puts "Enter the time"
a = Time.now
p a.strftime('%I:%M %P')
but it has to return the time dynamically i.e., if I pass some random time in the terminal it has to print the time in 24hr format

I think i got the answer for that question
require 'time'
puts "Enter the time"
a = gets.chomp
p Time.parse(":#{a}").strftime("%H:%M:%S")

Related

How to make Ruby sleep for amount input by user?

I've tried to make Ruby sleep for an amount that the user has input by doing this:
puts "Time?"
time = gets.chomp
time.to_i
sleep(time)
Does anyone know what I'm trying to do and what I'm doing wrong?
The following works for me:
puts "Time?"
time = gets.chomp
sleep(time.to_i)
.to_i doesn't convert the value and overrides the variable, it simply returns the converted result. So it needs to either be used directly in the sleep argument or set its own (or another variable):
time = time.to_i
sleep(time)
Calling time.to_i returns an integer, but doesn't change time itself. Therefore time is still a string.
Change it to:
puts 'Time?'
string = gets.chomp
time = string.to_i
sleep(time)
Because to_i doesn't care if there is a "\n" at the end of the string, you can skip the chomp call. Therefore you can just write:
puts 'Time?'
time = gets.to_i
sleep(time)

Not able to iterate properly over hash in my continuous input program (Ruby)

I am trying to see if I can create a list of user input requests with a hash in this program I am trying to create. Whats the best way to go about this?
print "would you like to begin a changeover? (y/n) "
input = gets.chomp
tracking = {
"start time" => input.Time.now,
"Type?" => input,
"shift?" => input,
"standard hrs?" => input,
"actual hrs?" => input,
"end time" => input = gets.chomp.Time.now
}
tracking.each do |key, value|
puts "great please answer the following: #{tracking}"
end
thanks in advance!
You have to remember that the evaluation is sequential, going from top to bottom (unless you are defining functions/methods). In your code:
You ask the user about a changeover
You get user input (say, y) into the variable input
You make a hash, with six values; four of them will contain y, two of them will contain current time
You iterate over the hash, printing its values (and asking the user nothing).
Or at least it would if gets.chomp.Time.now was not an error.
So, taking care about the timing:
print "would you like to begin a changeover? (y/n) "
unless gets.chomp.tolower == 'n'
puts "great please answer the following:"
tracking = {
"start time" => Time.now
}
[
"Type?",
"shift?",
"standard hrs?",
"actual hrs?"
].each do |question|
print question
tracking[question] = gets.chomp
}
tracking["end_time"] = Time.now
end
Thanks Alot! This set me on the right track. However, was not time stamping the beginning and end of the questionnaire the way I wanted. After playing with the code a bit on my own however, I was able to make it work.
print "would you like to begin a changeover? (y/n) "
unless gets.chomp. == "n"
puts Time.now
puts "great please answer the following: "
end
questionnaire = ["Type", "Shift?", "Standard hrs?", "Actual hrs?"]
.each do |question|
puts question
questionnaire = gets.chomp
end
puts "Please type 'done' when change over complete"
input = gets.chomp
puts Time.now

storing user input to an empty hash

I'm writing a basic program. I am starting out with an empty hash, and using gets.chomp and want to store the info into the hash so that it can be recalled later (in the same session).
runs = {}
puts "how many miles did you run?"
miles = gets.chomp
puts "how long did it take you?"
time = gets.chomp
... program continues
Later I would like to have the user enter 'review' to display this (and any others that were entered) as information:
runs.each do |miles, time|
puts "#{miles} miles in #{time} minutes."
end
I know I am missing something before/after the "gets.chomp". Any suggestions?
To add a key value pair to a hash, you would use runs[miles] = time.
So your code would look something like this:
runs = {}
puts "How many miles did you run?"
miles = gets.chomp
puts "How long did it take you?"
time = gets.chomp
runs[miles] = time
# When user enters `review`
runs.each do |miles, time|
p "#{miles} miles in #{times} minutes."
end
That's not the only way to add a key/value pair to a hash, so you can look at all the methods available over in the rubydoc.
As your program is written, if a user ran the same distance twice (with different times) only the last result will be recorded, as a hash cannot store duplicate keys. A better way would be to store it in an array of tuples (== array with two values):
runs = []
puts "How many miles did you run?"
miles = gets.chomp
puts "How long did it take you?"
time = gets.chomp
runs << [miles, time]
# ...
runs.each do |miles, time|
puts "#{miles} miles in #{time} minutes."
end
runs = Hash.new
puts "How many miles did you run?"
runs ["miles"] = gets.chomp
puts "How long did it take you?"
runs ["time"] = gets.chomp
# When user enters `review`
runs.each do |miles, time|
p "#{miles} miles in #{times} minutes."
end

Ruby - Random Number is the Same Random Number, Every Time

wordList = ['1930', '1931', '1932', '1933', '1934', '1935', '1936', '1937', '1938', '1939', '1940']
wordLen = wordList.length
wordRand = rand(wordLen)
year = wordList[wordRand]
Very much a newb here... The behavior of year is such that every time I run the program, it selects a random string from wordList. The problem is that it takes that particular randomly-selected number and sets it as equal to year. So, for every instance of the program, year is the same string from the list each time I call it. How can I get it to select a different number each time?
puts 'Why hello there, dear! Grandma is, SO, happy to see you!'
response = gets.chomp
while response != 'BYE'
if response == response.upcase
puts 'NO, NOT SINCE ' + year + '!'
response = gets.chomp
else
puts 'WHAT? SPEAK UP, SONNY!'
response = gets.chomp
end
end
if response == 'BYE'
puts 'OKAY, BYE DEAR!!'
end
edit: added context
You appear to only be generating one value for year, then repeatedly using it in your loop. If you want different numbers, put the call to rand within the loop.

Assigning a value to a variable

I would like the variable size to be defined in my program via user input. I have been unsuccessful in defining it in any way other than by a number manually entered into my code (currently 10).
def pass (size = 10)
This works for me:
def pass(size)
puts size
end
puts "please input a number:"
size = gets
pass(size)
You may need to use to_i, as your user input is a string. Then it works as the previous answer:
def pass(size = 10)
size = size.to_i
puts "It's a number! #{size}" if size.is_a? Integer
end
puts "please input a number:"
size = gets
pass(size)

Resources