Tutorial by Examples

A string can be written to a file with an instance of the File class. file = File.new('tmp.txt', 'w') file.write("NaNaNaNa\n") file.write('Batman!\n') file.close The File class also offers a shorthand for the new and close operations with the open method. File.open('tmp.txt', 'w') ...
Manually open and close a file. # Using new method f = File.new("test.txt", "r") # reading f = File.new("test.txt", "w") # writing f = File.new("test.txt", "a") # appending # Using open method f = open("test.txt", "r&...
Unlike gets.chomp this will not wait for a newline. First part of the stdlib must be included require 'io/console' Then a helper method can be written: def get_char input = STDIN.getch control_c_code = "\u0003" exit(1) if input == control_c_code input end Its' imporan...
# Get two numbers from STDIN, separated by a newline, and output the result number1 = gets number2 = gets puts number1.to_i + number2.to_i ## run with: $ ruby a_plus_b.rb ## or: $ echo -e "1\n2" | ruby a_plus_b.rb
number1 = ARGV[0] number2 = ARGV[1] puts number1.to_i + number2.to_i ## run with: $ ruby a_plus_b.rb 1 2

Page 1 of 1