-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_find_all.rb
executable file
·65 lines (48 loc) · 2.06 KB
/
test_find_all.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Playing with find_all
div_five = (1..100).find_all { |x| x % 5 == 0 }
puts div_five.join(", ")
puts
hand = ["Ace of Spades", "Four of Spades", "Ace of Hearts", "Ace of Diamonds", "Ten of Hearts", "Three of Diamonds", "Ace of Clubs"]
join_me = hand.find_all { |card| card.include?("Ace") }
puts join_me.join(", ")
puts
origin = hand
destination = []
all_the_things = origin.find_all { |card| card.include?("Ace") }
all_the_things.each do |card_a|
origin.each do |card_b|
origin.delete(card_b) if card_b == card_a
end
end
until all_the_things.length == 0
destination << all_the_things.delete(all_the_things[0])
end
puts "Origin: #{origin}."
puts "Destination: #{destination}."
puts "All The Things: #{all_the_things}."
puts
# Playing with select
hand = ["Ace of Spades", "Four of Spades", "Ace of Hearts", "Ace of Diamonds", "Ten of Hearts", "Three of Diamonds", "Ace of Clubs"]
select_all_aces = hand.select { |x| x =~ /(Ace)/ } .join(", ")
puts "The hand: #{hand}."
puts "Selecting aces only: #{select_all_aces}."
puts "The hand again: #{hand}."
puts
# As expected, using select! alters the original hand
hand = ["Ace of Spades", "Four of Spades", "Ace of Hearts", "Ace of Diamonds", "Ten of Hearts", "Three of Diamonds", "Ace of Clubs"]
select_all_aces = hand.select! { |x| x =~ /(Ace)/ } .join(", ")
puts "The hand: #{hand}."
puts "Selecting aces only: #{select_all_aces}."
puts
# Alternatively, we can use reject to refuse any non-Ace
hand = ["Ace of Spades", "Four of Spades", "Ace of Hearts", "Ace of Diamonds", "Ten of Hearts", "Three of Diamonds", "Ace of Clubs"]
select_all_aces = hand.reject { |x| x.include?("Ace") != true } .join(", ")
puts "The hand: #{hand}."
puts "Selecting aces only: #{select_all_aces}."
puts "The hand again: #{hand}."
puts
# And reject! alters the original hand
hand = ["Ace of Spades", "Four of Spades", "Ace of Hearts", "Ace of Diamonds", "Ten of Hearts", "Three of Diamonds", "Ace of Clubs"]
select_all_aces = hand.reject! { |x| x.include?("Ace") != true } .join(", ")
puts "The hand: #{hand}."
puts "Selecting aces only: #{select_all_aces}."