[Note] [Beginner] Ruby writing memo (refactoring) 1

Overview

I noticed that the Ruby code posted in previous post is not written in a Ruby-like way, so I also made a note of studying as a correction. leave behind.

After seeing the following article, I tried to correct it by taking the code of the previous post as an example while actually referring to the contents for studying. ** [For beginners] Idioms and useful methods that can be used for refactoring in Ruby and Rails | Qiita **

code

Before correction

def Upcasing(result_lines)
  result_lines[0] = result_lines[0].capitalize
  break_flg = false
 ## ④ Hash notation in Ruby (without modification: the reason why it could not be done will be described later)
  break_sign = { "."=>true, "!"=>true, "?"=>true, ","=>false }

   result_lines.each_index do |i|
 ## ① Conditional branch and boolean value
    if break_flg == true
      result_lines[i] = result_lines[i].capitalize
    end

 ## ② How to specify the array element, refer to the character of the specified number in the character string
    if break_sign[result_lines[i].slice(-1)] == true
      break_flg = true
    else
      break_flg = false
    end
  end
  joined_line = result_lines.join(" ")
 ## ③ Notation of return value in Ruby
  return joined_line
end

print Upcasing(["oh,", "yeah!", "hello!"])

## Execution result
 Oh, yeah! Hello!

change point

def Upcasing(result_lines)
  result_lines[0] = result_lines[0].capitalize
  break_flg = false
 ## ④ Hash notation in Ruby (The reason why it could not be changed without modification will be described later)
  break_sign = { "."=>true, "!"=>true, "?"=>true, ","=>false }

   result_lines.each_index do |i|
 ## ① Conditional branch and boolean value
-   if break_flg == true
+   if break_flg
      result_lines[i] = result_lines[i].capitalize
    end

 ## ② How to specify the array element, refer to the character of the specified number in the character string
-   if break_sign[result_lines[i].slice(-1)] == true
+   if break_sign[result_lines[i][-1]]   
      break_flg = true
    else
      break_flg = false
    end
  end
  joined_line = result_lines.join(" ")
 ## ③ Notation of return value in Ruby
- return joined_line
end

print Upcasing(["oh,", "yeah!", "hello!"])

Revised

def Upcasing(result_lines)
  result_lines[0] = result_lines[0].capitalize
  break_flg = false
  break_sign = { "."=>true, "!"=>true, "?"=>true, ","=>false }

   result_lines.each_index do |i|
    if break_flg
      result_lines[i] = result_lines[i].capitalize
    end
    
    if break_sign[result_lines[i][-1]]
      break_flg = true
    else
      break_flg = false
    end
  end
  joined_line = result_lines.join(" ")
end

print Upcasing(["oh,", "yeah!", "hello!"])

## Execution result
 Oh, yeah! Hello!

Details of each correction

We will follow the points to be examined (1) to (4) in numerical order.

① Conditional branch and boolean value

-   if break_flg == true
+   if break_flg

Control structure> if | Ruby 2.7.0 Reference Manual

It's embarrassing, but I'm overusing it by inertia, and I didn't understand the underlying specification that if you write a single variable as an expression, it returns the truth. It's not just about Ruby ... At least, in Ruby, do not write (== true) with a simple truth judgment.

hoge = true
p hoge
if hoge; hoge = false; end
p hoge

 true
 false
var hoge = true;
console.log(hoge);
if (hoge) hoge = false;
console.log(hoge);

// true
// false
public class Hello{
  public static void Main(){
    bool hoge = true;
    System.Console.WriteLine(hoge);
    if (hoge){ hoge = false; }
    System.Console.WriteLine(hoge);
  }
}

// True
// False

(2) How to specify the array element, refer to the character of the specified number in the character string

-   if break_sign[result_lines[i].slice(-1)] == true
+   if break_sign[result_lines[i][-1]]

③ Notation of return value in Ruby

def Upcasing(result_lines)
  ##
 ## Code omitted
  ##
  joined_line = result_lines.join(" ")
- return joined_line
end

-Class / Method Definition> Method Evaluation | Ruby 2.7.0 Reference Manual If return is not called, the value of the last expression in the body will be returned, so there is no need to write one line as before the correction.

④ Hash notation in Ruby

break_sign = { "."=>true, "!"=>true, "?"=>true, ","=>false }

At first, I tried to modify the notation using symbols as shown below.

break_sign = { ".": true, "!": true, "?": true, ",": false }

When I changed it as above, the debug result of the upper method changed. So, when I checked the contents of the hash, the key "!" Stored as below was no longer a character string.

break_sign = { "."=>true, "!"=>true, "?"=>true, ","=>false }
p break_sign
=> {"."=>true, "!"=>true, "?"=>true, ","=>false}

break_sign = { ".": true, "!": true, "?": true, ",": false }
p break_sign
=>{:"."=>true, :!=>true, :"?"=>true, :","=>false}

I thought about expressing it using symbols somehow, but I wondered if there were so many scenes where one special character was applied to the hash key in the first place, so I decided to go through this time. If you notice anything about this, I will post it separately.

Recommended Posts

[Note] [Beginner] Ruby writing memo (refactoring) 1
Ruby vertical writing
Writing code Ruby
Spring boot memo writing (1)
ruby basic syntax memo
What impressed me as a beginner in writing Ruby
Spring boot memo writing (2)
Personal memo Progate Ruby I (2)
Personal memo Progate Ruby I (1)