diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/ansi_colors.rb | 6 | ||||
-rw-r--r-- | examples/asking_for_arrays.rb | 4 | ||||
-rw-r--r-- | examples/basic_usage.rb | 33 | ||||
-rw-r--r-- | examples/color_scheme.rb | 16 | ||||
-rw-r--r-- | examples/get_character.rb | 8 | ||||
-rw-r--r-- | examples/menus.rb | 13 | ||||
-rw-r--r-- | examples/overwrite.rb | 6 | ||||
-rw-r--r-- | examples/repeat_entry.rb | 4 | ||||
-rw-r--r-- | examples/trapping_eof.rb | 2 | ||||
-rw-r--r-- | examples/using_readline.rb | 2 |
10 files changed, 48 insertions, 46 deletions
diff --git a/examples/ansi_colors.rb b/examples/ansi_colors.rb index 6986e72..e39b5cc 100644 --- a/examples/ansi_colors.rb +++ b/examples/ansi_colors.rb @@ -9,7 +9,7 @@ require "rubygems" require "highline/import" # Supported color sequences. -colors = %w{black red green yellow blue magenta cyan white} +colors = %w[black red green yellow blue magenta cyan white] # Using color() with symbols. colors.each_with_index do |c, i| @@ -26,7 +26,7 @@ say("This should be <%= color('underlined', UNDERLINE) %>!") say("This might even <%= BLINK %>blink<%= CLEAR %>!") # It even works with list wrapping. -erb_digits = %w{Zero One Two Three Four} + +erb_digits = %w[Zero One Two Three Four] + ["<%= color('Five', :blue) %%>"] + - %w{Six Seven Eight Nine} + %w[Six Seven Eight Nine] say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>") diff --git a/examples/asking_for_arrays.rb b/examples/asking_for_arrays.rb index 42c3268..85fb74f 100644 --- a/examples/asking_for_arrays.rb +++ b/examples/asking_for_arrays.rb @@ -12,8 +12,8 @@ require "pp" puts "Using: #{HighLine.default_instance.class}" puts -grades = ask( "Enter test scores (or a blank line to quit):", - lambda { |ans| ans =~ /^-?\d+$/ ? Integer(ans) : ans} ) do |q| +grades = ask("Enter test scores (or a blank line to quit):", + ->(ans) { ans =~ /^-?\d+$/ ? Integer(ans) : ans }) do |q| q.gather = "" end diff --git a/examples/basic_usage.rb b/examples/basic_usage.rb index 62e3ccb..3f28b02 100644 --- a/examples/basic_usage.rb +++ b/examples/basic_usage.rb @@ -12,32 +12,33 @@ require "yaml" puts "Using: #{HighLine.default_instance.terminal.class}" puts -contacts = [ ] +contacts = [] class NameClass - def self.parse( string ) + def self.parse(string) if string =~ /^\s*(\w+),\s*(\w+)\s*$/ - self.new($2, $1) + new(Regexp.last_match(2), Regexp.last_match(1)) else raise ArgumentError, "Invalid name format." end end def initialize(first, last) - @first, @last = first, last + @first = first + @last = last end attr_reader :first, :last end begin - entry = Hash.new + entry = {} # basic output say("Enter a contact:") # basic input - entry[:name] = ask("Name? (last, first) ", NameClass) do |q| + entry[:name] = ask("Name? (last, first) ", NameClass) do |q| q.validate = /\A\w+, ?\w+\Z/ end entry[:company] = ask("Company? ") { |q| q.default = "none" } @@ -47,26 +48,28 @@ begin q.case = :up q.validate = /\A[A-Z]{2}\Z/ end - entry[:zip] = ask("Zip? ") do |q| + entry[:zip] = ask("Zip? ") do |q| q.validate = /\A\d{5}(?:-?\d{4})?\Z/ end - entry[:phone] = ask( "Phone? ", - lambda { |p| p.delete("^0-9"). - sub(/\A(\d{3})/, '(\1) '). - sub(/(\d{4})\Z/, '-\1') } ) do |q| - q.validate = lambda { |p| p.delete("^0-9").length == 10 } + entry[:phone] = ask("Phone? ", + lambda { |p| + p.delete("^0-9"). + sub(/\A(\d{3})/, '(\1) '). + sub(/(\d{4})\Z/, '-\1') + }) do |q| + q.validate = ->(p) { p.delete("^0-9").length == 10 } q.responses[:not_valid] = "Enter a phone numer with area code." end entry[:age] = ask("Age? ", Integer) { |q| q.in = 0..105 } entry[:birthday] = ask("Birthday? ", Date) - entry[:interests] = ask( "Interests? (comma separated list) ", - lambda { |str| str.split(/,\s*/) } ) + entry[:interests] = ask("Interests? (comma separated list) ", + ->(str) { str.split(/,\s*/) }) entry[:description] = ask("Enter a description for this contact.") do |q| q.whitespace = :strip_and_collapse end contacts << entry -# shortcut for yes and no questions + # shortcut for yes and no questions end while agree("Enter another contact? ", true) if agree("Save these contacts? ", true) diff --git a/examples/color_scheme.rb b/examples/color_scheme.rb index 1fe8863..1889483 100644 --- a/examples/color_scheme.rb +++ b/examples/color_scheme.rb @@ -10,11 +10,11 @@ require 'highline/import' # Create a color scheme, naming color patterns with symbol names. ft = HighLine::ColorScheme.new do |cs| - cs[:headline] = [ :bold, :yellow, :on_black ] - cs[:horizontal_line] = [ :bold, :white, :on_blue] - cs[:even_row] = [ :green ] - cs[:odd_row] = [ :magenta ] - end + cs[:headline] = %i[bold yellow on_black] + cs[:horizontal_line] = %i[bold white on_blue] + cs[:even_row] = [:green] + cs[:odd_row] = [:magenta] +end # Assign that color scheme to HighLine... HighLine.color_scheme = ft @@ -26,7 +26,7 @@ say("<%= color('-'*20, :horizontal_line) %>") # Setup a toggle for rows. i = true ("A".."D").each do |row| - row_color = i ? :even_row : :odd_row - say("<%= color('#{row}', '#{row_color}') %>") - i = !i + row_color = i ? :even_row : :odd_row + say("<%= color('#{row}', '#{row_color}') %>") + i = !i end diff --git a/examples/get_character.rb b/examples/get_character.rb index c0a666f..0276ca9 100644 --- a/examples/get_character.rb +++ b/examples/get_character.rb @@ -8,8 +8,8 @@ puts choices = "ynaq" answer = ask("Your choice [#{choices}]? ") do |q| - q.echo = false - q.character = true - q.validate = /\A[#{choices}]\Z/ - end + q.echo = false + q.character = true + q.validate = /\A[#{choices}]\Z/ +end say("Your choice: #{answer}") diff --git a/examples/menus.rb b/examples/menus.rb index 258c1ca..5941f17 100644 --- a/examples/menus.rb +++ b/examples/menus.rb @@ -7,7 +7,7 @@ puts "Using: #{HighLine.default_instance.terminal.class}" puts # The old way, using ask() and say()... -choices = %w{ruby python perl} +choices = %w[ruby python perl] say("This is the old way using ask() and say()...") say("Please choose your favorite programming language:") say(choices.map { |c| " #{c}\n" }.join) @@ -25,10 +25,9 @@ choose do |menu| menu.prompt = "Please choose your favorite programming language? " menu.choice :ruby do say("Good choice!") end - menu.choices(:python, :perl) do say("Not from around here, are you?") end + menu.choices(:python, :perl) { say("Not from around here, are you?") } menu.default = :ruby - end say("\nThis is letter indexing...") @@ -39,7 +38,7 @@ choose do |menu| menu.prompt = "Please choose your favorite programming language? " menu.choice :ruby do say("Good choice!") end - menu.choices(:python, :perl) do say("Not from around here, are you?") end + menu.choices(:python, :perl) { say("Not from around here, are you?") } end say("\nThis is with a different layout...") @@ -50,7 +49,7 @@ choose do |menu| menu.prompt = "Favorite? " menu.choice :ruby do say("Good choice!") end - menu.choices(:python, :perl) do say("Not from around here, are you?") end + menu.choices(:python, :perl) { say("Not from around here, are you?") } end say("\nYou can even build shells...") @@ -60,10 +59,10 @@ loop do menu.shell = true - menu.choice(:load, "Load a file.") do |command, details| + menu.choice(:load, "Load a file.") do |_command, details| say("Loading file with options: #{details}...") end - menu.choice(:save, "Save a file.") do |command, details| + menu.choice(:save, "Save a file.") do |_command, details| say("Saving file with options: #{details}...") end menu.choice(:quit, "Exit program.") { exit } diff --git a/examples/overwrite.rb b/examples/overwrite.rb index f3c9754..cd02090 100644 --- a/examples/overwrite.rb +++ b/examples/overwrite.rb @@ -15,8 +15,8 @@ prompt = "here is your password:" ask( "#{prompt} <%= color('mypassword', RED, BOLD) %> (Press Any Key to blank) " ) do |q| - q.overwrite = true - q.echo = false # overwrite works best when echo is false. - q.character = true # if this is set to :getc then overwrite does not work + q.overwrite = true + q.echo = false # overwrite works best when echo is false. + q.character = true # if this is set to :getc then overwrite does not work end say("<%= color('Look! blanked out!', GREEN) %>") diff --git a/examples/repeat_entry.rb b/examples/repeat_entry.rb index 5e67f7d..9afd7ec 100644 --- a/examples/repeat_entry.rb +++ b/examples/repeat_entry.rb @@ -17,8 +17,8 @@ puts "Ok, you did it." pass = ask("<%= key %>: ") do |q| q.echo = '*' q.verify_match = true - q.gather = {"Enter a password" => '', - "Please type it again for verification" => ''} + q.gather = { "Enter a password" => '', + "Please type it again for verification" => '' } end puts "Your password is now #{pass}!" diff --git a/examples/trapping_eof.rb b/examples/trapping_eof.rb index a63188c..cd0dfef 100644 --- a/examples/trapping_eof.rb +++ b/examples/trapping_eof.rb @@ -13,7 +13,7 @@ loop do name = ask("What's your name?") break if name == "exit" puts "Hello, #{name}!" - rescue EOFError # HighLine throws this if @input.eof? + rescue EOFError # HighLine throws this if @input.eof? break end end diff --git a/examples/using_readline.rb b/examples/using_readline.rb index d8c072a..13c128b 100644 --- a/examples/using_readline.rb +++ b/examples/using_readline.rb @@ -9,7 +9,7 @@ require "rubygems" require "highline/import" loop do - cmd = ask("Enter command: ", %w{save sample load reset quit}) do |q| + cmd = ask("Enter command: ", %w[save sample load reset quit]) do |q| q.readline = true end say("Executing \"#{cmd}\"...") |