Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 2.4 KB

keyboard_input.md

File metadata and controls

51 lines (39 loc) · 2.4 KB

Jumpstart Live (JSL)

Day 1

Keyboard Input

Keyboard input is a way to accept input from the user, when running the program in Terminal.

gets

gets accepts a single line of data from the keyboard. A line is something that ends in \n. gets is short for get string which means whatever the user enters (even if it is a number) will be stored as a string

chomp

When the user enters the data and then presses enter (or return) a \n (also called a newline character) is added to the end of the line. In some cases you don't want this character at the end, chomp removes that newline character

Converting user input

gets gives you back a string, no matter what the user enters, even if they enter a number. Two common methods for converting that string into a number are described below.

Returns the result of interpreting leading characters in str as an integer base base (between 2 and 36). Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. This method never raises an exception when base is valid.

Returns the result of interpreting leading characters in str as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0 is returned. This method never raises an exception.

to_i

Converts a string into a fixnum, which is an integer value. If there is not a valid fixnum at the beginning of the string, then 0 is given instead. Any extra characters at the end that are not part of the fixnum are ignored.

puts "2".to_i	# prints 2
puts "2a".to_i	# prints 2
puts "a2".to_i	# prints 0
puts "a".to_i	# prints 0

to_f

Converts a string into a float, which is a decimal value. If there is not a valid float at the beginning of the string, then 0.0 is given instead. Any extra characters at the end that are not part of the float are ignored.

puts "2.1".to_f		# prints 2.1
puts "2.1a".to_f	# prints 2.1
puts "2a".to_f		# prints 2.0
puts "a2.0".to_f	# prints 0.0
puts "a".to_f		# prints 0.0

Example

3.times do
	print "Enter a number: "
	num = gets.chomp.to_i
	puts "You entered #{num}, #{num} to the power of 2 is #{num ** 2}"
end

References