Skip to content

Commit

Permalink
Use Object#in? throughout the stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Jan 30, 2020
1 parent 4049ff7 commit 65edcc4
Show file tree
Hide file tree
Showing 22 changed files with 37 additions and 36 deletions.
7 changes: 4 additions & 3 deletions samples/2048.cr
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,17 @@ class Game
end

def execute_action(action)
if [:up, :down, :left, :right].includes? action
case action
when :up, :down, :left, :right
if can_move_in? action
shift_grid action
true
else
false
end
elsif [:ctrl_c, :escape, :q].includes? action
when :ctrl_c, :escape, :q
end_game "Bye"
elsif action == :unknown
when :unknown
false # ignore
else
raise ArgumentError.new "Unknown action: #{action}"
Expand Down
2 changes: 1 addition & 1 deletion spec/compiler/crystal/tools/implementations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private def assert_implementations(code)
end
end

code = code.delete { |char| {'‸', '༓'}.includes? char }
code = code.delete &.in?('‸', '༓')

if cursor_location
visitor, result = processed_implementation_visitor(code, cursor_location)
Expand Down
2 changes: 1 addition & 1 deletion spec/std/bit_array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe "BitArray" do
slice[1] = 0b01010101_u8
slice[5] = 0b11111101_u8
ary.each_with_index do |e, i|
e.should eq({1, 3, 5, 7, 8, 10, 12, 14, 40, 42}.includes?(i))
e.should eq(i.in?(1, 3, 5, 7, 8, 10, 12, 14, 40, 42))
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1979,12 +1979,12 @@ describe "String" do
it { "hello world\\r\\n".count("\\A").should eq(0) }
it { "hello world\\r\\n".count("X-\\w").should eq(3) }
it { "aabbcc".count('a').should eq(2) }
it { "aabbcc".count { |c| ['a', 'b'].includes?(c) }.should eq(4) }
it { "aabbcc".count(&.in?('a', 'b')).should eq(4) }
end

describe "squeeze" do
it { "aaabbbccc".squeeze { |c| ['a', 'b'].includes?(c) }.should eq("abccc") }
it { "aaabbbccc".squeeze { |c| ['a', 'c'].includes?(c) }.should eq("abbbc") }
it { "aaabbbccc".squeeze(&.in?('a', 'b')).should eq("abccc") }
it { "aaabbbccc".squeeze(&.in?('a', 'c')).should eq("abbbc") }
it { "a bbb".squeeze.should eq("a b") }
it { "a bbb".squeeze(' ').should eq("a bbb") }
it { "aaabbbcccddd".squeeze("b-d").should eq("aaabcd") }
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/codegen/target.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class Crystal::Codegen::Target
end

def gnu?
environment_parts.any? { |part| {"gnu", "gnueabi", "gnueabihf"}.includes? part }
environment_parts.any? &.in?("gnu", "gnueabi", "gnueabihf")
end

def musl?
environment_parts.any? { |part| {"musl", "musleabi", "musleabihf"}.includes? part }
environment_parts.any? &.in?("musl", "musleabi", "musleabihf")
end

def windows?
Expand All @@ -105,7 +105,7 @@ class Crystal::Codegen::Target
end

def armhf?
environment_parts.includes?("gnueabihf") || environment_parts.includes?("musleabihf")
environment_parts.any? &.in?("gnueabihf", "musleabihf")
end

def to_target_machine(cpu = "", features = "", release = false,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ class Crystal::Command

output_filename ||= original_output_filename
output_format ||= "text"
if !["text", "json"].includes?(output_format)
unless output_format.in?("text", "json")
error "You have input an invalid format, only text and JSON are supported"
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/command/env.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Crystal::Command
private def env
if ARGV.size == 1 && {"--help", "-h"}.includes?(ARGV[0])
if ARGV.size == 1 && ARGV[0].in?("--help", "-h")
env_usage
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
num_members = enum_type.types.size
if num_members > 0 && enum_type.flags?
# skip None & All, they doesn't count as members for @[Flags] enums
num_members = enum_type.types.count { |(name, _)| !{"None", "All"}.includes?(name) }
num_members = enum_type.types.count { |(name, _)| !name.in?("None", "All") }
end

if num_members == 0
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2261,11 +2261,11 @@ module Crystal

if !delimiter_state && current_char == '%' && ident_start?(peek_next_char)
char = next_char
if char == 'q' && (peek = peek_next_char) && {'(', '<', '[', '{', '|'}.includes?(peek)
if char == 'q' && (peek = peek_next_char) && peek.in?('(', '<', '[', '{', '|')
next_char
delimiter_state = Token::DelimiterState.new(:string, char, closing_char, 1)
next_char
elsif char == 'r' && (peek = peek_next_char) && {'(', '<', '[', '{', '|'}.includes?(peek)
elsif char == 'r' && (peek = peek_next_char) && peek.in?('(', '<', '[', '{', '|')
next_char
delimiter_state = Token::DelimiterState.new(:regex, char, closing_char, 1)
next_char
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@ module Crystal
next_token_skip_space
end

unless @token.keyword? && {:when, :else, :end}.includes?(@token.value)
unless @token.keyword? && @token.value.in?(:when, :else, :end)
cond = parse_op_assign_no_control
skip_statement_end
end
Expand Down Expand Up @@ -3509,7 +3509,7 @@ module Crystal
end

def check_valid_def_name
if {:is_a?, :as, :as?, :responds_to?, :nil?}.includes?(@token.value)
if @token.value.in?(:is_a?, :as, :as?, :responds_to?, :nil?)
raise "'#{@token.value}' is a pseudo-method and can't be redefined", @token
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Crystal::Doc::Method
end
end
end
{type.name, "self"}.includes?(return_type.to_s)
return_type.to_s.in?(type.name, "self")
end

def abstract?
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4220,7 +4220,7 @@ module Crystal
end

# Mop up any trailing unused : or ::, don't write them since they should be removed
while {:":", :"::"}.includes? @token.type
while @token.type.in?(:":", :"::")
next_token_skip_space_or_newline
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/init.cr
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module Crystal

def self.fetch_skeleton_type(opts, args)
skeleton_type = fetch_required_parameter(opts, args, "TYPE")
unless {"lib", "app"}.includes?(skeleton_type)
unless skeleton_type.in?("lib", "app")
raise Error.new "Invalid TYPE value: #{skeleton_type}", opts
end
skeleton_type
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/unix/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Crystal::System::File
if ret == 0
FileInfo.new(stat)
else
if {Errno::ENOENT, Errno::ENOTDIR}.includes? Errno.value
if Errno.value.in?(Errno::ENOENT, Errno::ENOTDIR)
return nil
else
raise Errno.new("Unable to get info for '#{path.inspect_unquoted}'")
Expand Down
2 changes: 1 addition & 1 deletion src/dir/glob.cr
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class Dir
end

if entry = read_entry(dir)
next if {".", ".."}.includes?(entry.name)
next if entry.name.in?(".", "..")
next if !options[:match_hidden] && entry.name.starts_with?('.')

if dir_path.bytesize == 0
Expand Down
2 changes: 1 addition & 1 deletion src/ecr/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buffer_name = ARGV[1]
begin
puts ECR.process_file(filename, buffer_name)
rescue ex : Errno
if {Errno::ENOENT, Errno::EISDIR}.includes?(ex.errno)
if ex.errno.in?(Errno::ENOENT, Errno::EISDIR)
STDERR.puts ex.message
exit 1
else
Expand Down
2 changes: 1 addition & 1 deletion src/html.cr
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ module HTML
# last two of each plane (nonchars) disallowed
codepoint & 0xFFFF >= 0xFFFE ||
# unicode control characters expect space
(codepoint < 0x0020 && !{0x0009, 0x000A, 0x000C}.includes?(codepoint))
(codepoint < 0x0020 && !codepoint.in?(0x0009, 0x000A, 0x000C))
codepoint.unsafe_chr
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/http/server/handlers/static_file_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HTTP::StaticFileHandler
end

def call(context)
unless {"GET", "HEAD"}.includes?(context.request.method)
unless context.request.method.in?("GET", "HEAD")
if @fallthrough
call_next(context)
else
Expand Down
8 changes: 4 additions & 4 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2260,7 +2260,7 @@ class String
# returns the number of times the block returned a truthy value.
#
# ```
# "aabbcc".count { |c| ['a', 'b'].includes?(c) } # => 4
# "aabbcc".count(&.in?('a', 'b')) # => 4
# ```
def count
count = 0
Expand Down Expand Up @@ -2291,7 +2291,7 @@ class String
# block returned a truthy value removed.
#
# ```
# "aabbcc".delete { |c| ['a', 'b'].includes?(c) } # => "cc"
# "aabbcc".delete(&.in?('a', 'b')) # => "cc"
# ```
def delete
String.build(bytesize) do |buffer|
Expand Down Expand Up @@ -2327,8 +2327,8 @@ class String
# block returned a truthy value.
#
# ```
# "aaabbbccc".squeeze { |c| ['a', 'b'].includes?(c) } # => "abccc"
# "aaabbbccc".squeeze { |c| ['a', 'c'].includes?(c) } # => "abbbc"
# "aaabbbccc".squeeze(&.in?('a', 'b')) # => "abccc"
# "aaabbbccc".squeeze(&.in?('a', 'c')) # => "abbbc"
# ```
def squeeze
previous = nil
Expand Down
2 changes: 1 addition & 1 deletion src/time/format/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ struct Time::Format
end

def time_zone_z
raise "Invalid timezone" unless {'Z', 'z'}.includes? current_char
raise "Invalid timezone" unless current_char.in?('Z', 'z')

@location = Location::UTC
next_char
Expand Down
6 changes: 3 additions & 3 deletions src/time/location/loader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Time::Location

# 1-byte version, then 15 bytes of padding
version = io.read_byte
raise InvalidTZDataError.new unless {0_u8, '2'.ord, '3'.ord}.includes?(version)
raise InvalidTZDataError.new unless version.in?(0_u8, '2'.ord, '3'.ord)
io.skip(15)

# six big-endian 32-bit integers:
Expand Down Expand Up @@ -138,8 +138,8 @@ class Time::Location
zone_idx = transition_indexes[transition_id]
raise InvalidTZDataError.new unless zone_idx < zones.size

isstd = !{nil, 0_u8}.includes? isstddata[transition_id]?
isutc = !{nil, 0_u8}.includes? isstddata[transition_id]?
isstd = !isstddata[transition_id]?.in?(nil, 0_u8)
isutc = !isstddata[transition_id]?.in?(nil, 0_u8)

ZoneTransition.new(time, zone_idx, isstd, isutc)
end
Expand Down
4 changes: 2 additions & 2 deletions src/uri/encoding.cr
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class URI
def self.reserved?(byte) : Bool
char = byte.unsafe_chr
'&' <= char <= ',' ||
{'!', '#', '$', '/', ':', ';', '?', '@', '[', ']', '='}.includes?(char)
char.in?('!', '#', '$', '/', ':', ';', '?', '@', '[', ']', '=')
end

# Returns whether given byte is unreserved character defined in
Expand All @@ -173,7 +173,7 @@ class URI
def self.unreserved?(byte) : Bool
char = byte.unsafe_chr
char.ascii_alphanumeric? ||
{'_', '.', '-', '~'}.includes?(char)
char.in?('_', '.', '-', '~')
end

# URL-decodes *string* and writes the result to *io*.
Expand Down

0 comments on commit 65edcc4

Please sign in to comment.