How to make Ruby sleep for amount input by user? - ruby

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)

Related

how to pass dynamic values for time in the cmd

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")

Ruby until always validates as false?

So I've written this piece of code to play around with numbers and the until loop:
number = rand(10)
puts number
puts "Guess the number"
guess = gets.chomp
until guess == number
puts "Guess again!"
guess = gets.chomp
end
puts "You've guessed it right! The number is #{guess}
But for some reason it it always stuck in the until loop and I am not sure why. I puts the random number to know that i guess right and try out the code. I'm completely new to Ruby, So I guess it's a very obvious thing I am missing, but I just dont see it.
From my point of view, whenever I prompt for the guess again, that guess that validated by the until loop with until guess == number.
Who can help me clear this up?
The reason is simple, and that is you're failing to cast the user input into an integer.
i.e. if I write
number = gets.chomp
and I type 1 for the number, then the number variable will equal the string "1", not the integer 1.
To fix this, just use guess = gets.chomp.to_i
Instead of guess = gets.chomp(which will return a string of the user input), use guess = gets.to_i (which will convert user input into integer)
to_i method will convert the element into integer and will drop /n character since it's not part of the integer. Don't need to add .chomp method.

Trying to convert a number to binary form

I wrote a simple program which works:
a = 4.to_s(2)
puts a.reverse
I want to be able to change it based on input from the user in the terminal. This is what I wrote:
puts 'Please enter a number you would like to see expressed in binary form'
i = gets.chomp
b = i.to_s(2)
puts b
This is the error that I keep getting:
`to_s': wrong number of arguments(1 for 0) (ArgumentError)
You're starting with a string, so you need to convert it:
i.to_i.to_s(2)
The #to_s method on a string doesn't take any arguments.
You do not need the chomp method, #to_i will take care of it.
Write it as:
puts 'Please enter a number you would like to see expressed in binary form'
i = gets.to_i
b = i.to_s(2)
puts b
You are calling to_s on an string, not on integer as you are thinking, because Kernel#gets always gives you a String object.
First, convert it to Fixnum, then call Fixnum#to_s on the Fixnum instance, which takes an argument, but String#to_s doesn't accept arguments, which was why you got a complaint from Ruby.
It seems that you want to use Fixnum#to_s but in your program gets.chomp returns a string, so you actually call String#to_s
You can could do:
i = gets.chomp.to_i

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.

elegant way to check if a string inputed by user is an integer in ruby?

I'm trying to validate that the input a user gives my program via gets is an integer. is_a?(Integer) does not work, as far as i can tell, because gets gets a string from the user, so it will always return false even if the user enters an valid integer (in string form). One would think I could simply use to_i on the input and be done with it, but that raises another issue - "75akjfas".to_i results in 75. So if I relied on to_i to solve my problems, anything starting with numbers will work.
How do I cleanly validate that the value is an integer only, and not a combination of numbers and letters? Or do I need to resort to regex for this?
print "Enter an integer (or try to sneak by something other): "
puts Integer(gets) rescue puts "Hey, that's not an integer!"
How about s.to_i.to_s == s? I'd prefer regex however.
Using regex you could do it like this:
class String
def integer?
!!(self =~ /^[-+]?[0-9]+$/)
end
end
You could use Integer() but you need to be careful with hex, binary, or octal input. http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-Integer
def valid_integer?(string)
begin
!!Integer(string)
rescue ArgumentError
false
end
end
Check this code example for how to use the checked string input by the user
puts "Enter a number: "
user_input = gets.chomp
check = (user_input.to_i.to_s == user_input)
while (!check ) do
puts("Wrong Input, Pls, Enter a number: " )
user_input = gets.chomp
check = (user_input.to_i.to_s == user_input)
end
if user_input.to_i < 0
puts "Number is negative"
elsif user_input.to_i > 0
puts "Number is positve"
else
puts "Number is Zero"
end
Ruby can do it without esoteric solutions:
Integer is a integer
1970.is_a?Integer
true
String is not a integer
"1970".is_a?Integer
false
String to integer is a integer
"1970".to_i.is_a?Integer
true

Resources