Try something like Python for-else in Ruby

Trigger

Read Effective Python Memo Item 12 Avoid using else blocks after for and while loops, and wonder how to write it as Ruby. Became.

things to do

a = 4
b = 9
for i in range(2, min(a, b) + 1):
    print('Testing', i)
    if a % i == 0 and b % i == 0:
        print('Not coprime')
        break
    else:
        print('Coprime')

The ʻelse` clause is executed when the loop ends uninterrupted. Sure, it's not intuitive. I'll do this with Ruby.

Consideration

Ignoring the ʻelse` clause, in the case of Ruby,

a = 4
b = 9
(2..[a, b].min).each do |i|
  puts "Testing: #{i}"
  if (a % i).zero? && (b % i).zero?
    puts "Not coprime"
    break
  end
end

It will be. ʻEachreturnsself, so if the iteration ends without break, 2 .. [a, b], that is, the true value is returned, and if break, nil`, that is, A false value is returned.

So, if you connect with ʻand`, it seems to work.

Conclusion

a = 4
b = 9
(2..[a, b].min).each do |i|
  puts "Testing: #{i}"
  if (a % i).zero? && (b % i).zero?
    puts "Not coprime"
    break
  end
end and puts "Coprime"

For the time being, it went well. But it's not beautiful.

To use &&, you need to use puts ("Coprime") or (puts" Coprime ") to strengthen the connection between puts and"Coprime".

bonus

I also thought about how to get Ruby Poku puts out.

Part 1

a = 4
b = 9
puts (2..[a, b].min).each_with_object("Coprime") { |i|
  puts "Testing: #{i}"
  if (a % i).zero? && (b % i).zero?
    break "Not coprime"
  end
}

Part 2

a = 4
b = 9
puts "Coprime".tap {
  (2..[a, b].min).each do |i|
    puts "Testing: #{i}"
    if (a % i).zero? && (b % i).zero?
      break "Not coprime"
    end
  end
}

All are difficult to understand.

As you can see in "Effective Python", it seems best to cut out to a method without doing something like for-else.

def coprime?(a, b)
  (2..[a, b].min).each do |i|
    return false if (a % i).zero? && (b % i).zero?
  end
  true
end

puts coprime?(4, 9) ? "Coprime" : "Not coprime"

Recommended Posts

Try something like Python for-else in Ruby
Something like JS setTimeout in python
Something like tail -f in Python
Do something like Redis transactions in Python
Try gRPC in Python
Try 9 slices in Python
Try LINE Notify in Python
Try implementing Yubaba in Python 3
I want to do something like sort uniq in Python
Do something like a Python interpreter in Visual Studio Code
Try implementing extension method in python
Try using LevelDB in Python (plyvel)
Let's try Fizz Buzz in Python
Display characters like AA in python
Try to calculate Trace in Python
Try PLC register access in Python
Try using Leap Motion in Python
Try python
I wanted to do something like an Elixir pipe in Python
Something like 40-32 / 2 = 4!
Try logging in to qiita with Python
Try using the Wunderlist API in Python
Differences between Ruby and Python in scope
Try using the Kraken API in Python
Find files like find on linux in Python
Try working with binary data in Python
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Big difference in ruby, python, httpd performance
Referencing INI files in Python or Ruby
Quickly try Microsoft's Face API in Python
Try calling Python from Ruby with thrift
Try text mining your diary in Python
Try hitting the YouTube API in Python
Try a functional programming pipe in Python
How to write Ruby to_s in Python
Try to make something like C # LINQ
#I tried something like Vlookup with Python # 2
Python in optimization
CURL in python
Rock-paper-scissors in Ruby
Metaprogramming in Python
First steps to try Google CloudVision in Python
Convert Unicode escape sequences like \ u in Python
Python 3.3 in Anaconda
Try to implement Oni Maitsuji Miserable in python
Geocoding in python
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Try auto to automatically price Enums in Python 3.6
Meta-analysis in Python
[Cloudian # 7] Try deleting the bucket in Python (boto3)
Unittest in python
When I try matplotlib in Python, it says'cairo.Context'
Try using the BitFlyer Ligntning API in Python
Epoch in Python
Discord in Python
[CpawCTF] Q14. [PPC] Try writing Sort! In Python
GNU GLOBAL (gtags) + α in Go, Ruby, Python
Sudoku in Python
DCI in Python