I want to get the value in Ruby

Make a note of the list of value acquisition in Ruby

Get value

■ Get the value in one line

value = gets

However, if this is left as it is, a line break will occur when outputting, so use the chomp method.

■ Get the line feed code cut off

value = gets.chomp

The to_i method is convenient when you want to calculate the acquired numerical value.

■ Get the value as a number

value = gets.to_i

to_i method can convert string to number Conversely, if you want to change a number to a string, use the to_s method

■ Get multiple elements from one line

a b c When the values are entered on one line separated by spaces like this, the split method value = gets.split(" ")

You can get the values in the form of an array by using the split method You can check the contents in the same form as an array like value [0] and value [1]

■ Get the value obtained in one line as an array

value = gets.chars

a b c When the values are entered on one line separated by spaces like this value = ["a", " ", "b", " ", "c"] Get the values as an array called

value = gets.chomp.split(" ") By writing like this, the line feed code can also be cut off, which is convenient. I think this is generally used

value = gets.split(" ").map(&:to_i) When all the values are numbers, you can get them in the form of an array and convert them to numbers at the same time by writing like this.

■ Get the value in multiple lines

value = readlines

redalines can get multi-line strings as a line-by-line array.

Unlike gets, typing a line break does not end keyboard input.

Please let me know if you make a mistake or have an easier way!

Recommended Posts

I want to get the value in Ruby
[Android] I want to get the listener from the button in ListView
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
[Ruby] I want to output only the odd-numbered characters in the character string
[Ruby] I want to extract only the value of the hash and only the key
I want to get the IP address when connecting to Wi-Fi in Java
I want to embed any TraceId in the log
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to set the conditions to be displayed in collection_check_boxes
I want to perform high-speed prime factorization in Ruby (ABC177E)
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I want to create a Parquet file even in Ruby
I want to transition to the same screen in the saved state
[Ruby] I want to reverse the order of the hash table
I want to simplify the conditional if-else statement in Java
# 1_JAVA I want to get the index number by specifying one character in the character string.
How to get date data in Ruby
How to get the date in java
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
I tried to make a parent class of a value object in Ruby
I want to sort by tab delimited by ruby
I want to write JSP in Emacs more easily than the default.
I want to output the day of the week
I want to send an email in Java.
I want to pass APP_HOME to logback in Gradle
[Note] I want to get in reverse order using afterLast with JdbcTemplate
rsync4j --I want to touch rsync in Java.
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
[Ruby] I want to display posted items in order of newest date
[Xcode] I want to manage images in folders
How to get the value after "_" in Windows batch like Java -version
How to get the setting value (property value) from the database in Spring Framework
I want to be eventually even in kotlin
I thought about the best way to create a ValueObject in Ruby
I tried to make full use of the CPU core in Ruby
I want to display an error message when registering in the database
[Ruby] I tried to summarize the methods that frequently appear in paiza
I want to get the field name of the [Java] field. (Old tale tone)
To get Ruby Silver at the last minute
How to build the simplest blockchain in Ruby
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
How to create your own annotation in Java and get the value
I get an error when I try to use "^" or "$" in ruby ​​regular expression
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
[Rails / ActiveRecord] I want to validate the value before the type is converted (_before_type_cast)
I want to get only the time from Time type data ...! [Strftime] * Additional notes
I want to recursively get the superclass and interface of a certain class
I was addicted to the NoSuchMethodError in Cloud Endpoints
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I get Mysql2 :: Error :: ConnectionError in the production environment
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
[Ruby] Method to easily get the receiver type .class
I want to judge the range using the monthly degree