[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing

Introduction

After studying Java and C language, I implement the program in Ruby. At that time, the loop statement that was initially implemented has a Java-like implementation content, Since it didn't look like Ruby, we refactored it as follows.

I would appreciate it if you could teach me if there is a Ruby-like description method than the following.

Program contents

A program that displays odd numbers from 0 to 100

Before correction

Describe the iterative process according to the following Ruby grammar. Easy to write because you are used to for statements in Java and C.

for variable in object do
Process to be executed repeatedly
end

loop.rb


for n in 0..100 do
  if n % 2 != 0 then
    puts n
  end
end

Revised

As you study Ruby, you will recognize that it is common to use each for iterative processing. In addition, methods that handle integers odd? Even? By knowing the instance method like select, as below You can modify the iterative process.

#Returns true if it is odd
3.odd? #Execution result true

#select method
#Judge each element as a block and return only the true element
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts array.select(&:odd?) #Execution result 1 3 5 7 9

loop.rb


(0..100).select(&:odd?).each do |n|
  puts n
end

Finally

The Ruby for statement seems to have each method executed at runtime. (It seems that the fact that another method is actually executed as described above is called syntactic sugar.) When writing iterative processing in Ruby, I personally recommend each.

From this revision, I would like to learn the following lessons. *** Enter Ruby and follow Ruby *** I would like to work with the same spirit when learning other languages.

Recommended Posts

[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
From Java to Ruby !!
Java language from the perspective of Kotlin and C #
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Java to C and C to Java in Android Studio
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Ruby] Iterative processing
Try to link Ruby and Java with Dapr
Ruby Iterative Processing
How to start a subscript from an arbitrary number in Ruby iterative processing
java iterative processing
[Java] How to get the key and value stored in Map by iterative processing
Convert Java enum enums and JSON to and from Jackson
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Generate models from JSON to Swift, PHP, C #, JAVA
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
[Java] Change language and locale to English in JVM options
How to write and notes when migrating from VB to JAVA
ClassCastException occurs when migrating from Java7 to Java8 ~ Generics and overload ~
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Migration from Cobol to JAVA
New features from Java7 to Java8
Use C program from Ruby
Connect from Java to PostgreSQL
Ruby C extension and volatile
☾ Java / Iterative statement and iterative control statement
From Ineffective Java to Effective Java
C # and Java Overrides Story
[Ruby ~ Iterative processing ~] Study memo 4
Search and execute method by name from instance with processing (java)
Offline real-time how to write F03 ruby and C implementation example
Notes on building Kotlin development environment and migrating from Java to Kotlin
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
How to get and add data from Firebase Firestore in Ruby
I tried to generate a C language program source from cURL
[Java] How to convert from String to Path type and get the path
Introduction to Scala from a Java perspective, decompiled and understood (basic)