Module: Raheui::IO
- Defined in:
- lib/raheui/io.rb
Overview
IO methods for user input and output.
Class Method Summary collapse
-
.print_chr(value) ⇒ Object
Print an character from character code.
-
.print_int(value) ⇒ Object
Print an Integer.
-
.read_chr ⇒ Object
Read an character from user input.
-
.read_int ⇒ Object
Read an Integer from user input.
Class Method Details
.print_chr(value) ⇒ Object
Print an character from character code.
value - The Integer character code to print.
Examples
IO.print_chr(97)
# a=> nil
IO.print_chr(0xAC00)
# 가=> nil
# Rescue RangeError
IO.print_chr(-1)
# [U+..FF]=> nil
# Rescue RangeError
IO.print_chr(0x110000)
# [U+110000]=> nil
Returns nothing.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/raheui/io.rb', line 55 def self.print_chr(value) # See https://github.com/jruby/jruby/issues/1921. if RUBY_PLATFORM == 'java' && (0xD800 <= value && value <= 0xDFFF || value > 0x10_FFFF) fail RangeError, "invalid codepoint 0x#{value.to_s(16).upcase} in UTF-8" end $stdout.print value.chr(Encoding::UTF_8) rescue RangeError $stdout.print format('[U+%04X]', value) end |
.print_int(value) ⇒ Object
Print an Integer.
value - The Integer to print.
Examples
IO.print_int(42)
# 42=> nil
Returns nothing.
30 31 32 |
# File 'lib/raheui/io.rb', line 30 def self.print_int(value) $stdout.print value end |
.read_chr ⇒ Object
Read an character from user input.
Returns the Integer character code of the character user entered.
16 17 18 |
# File 'lib/raheui/io.rb', line 16 def self.read_chr $stdin.getc.ord end |
.read_int ⇒ Object
Read an Integer from user input.
Returns the Integer user entered.
9 10 11 |
# File 'lib/raheui/io.rb', line 9 def self.read_int $stdin.gets.to_i end |