[Ruby] Writing code Ruby

4 minute read

What is the act of writing code with # ruby … Reading ruby’s writing method and thinking method in a book, there are many things that can not be seen just by moving it for the time being. Even when reading other people’s codes, if your reading ability is low, even habitual expressions get stuck. Also, if you don’t know the habit, people who are accustomed to it will be surprised by the code you wrote. One of them is the naming method I wrote before, but this time I’ll put together another type of lesson learned.

class method self

What is self written in this class method? You cannot call that method unless you write it. Why are you talking about that? The first question to ask is If you are new to object orientation, you will stumble here. It’s a sequel.

The answer is to show the class method. A method in which the receiver is the class name. Below is how to write a class method as seen in the book. When calling, the class name Greeter is the receiver and the greet method is called.

class_method.ruby


class Greeter
class << self
def greet
puts "Good evening"
end
end
end

Greeter.greet

The following is how to write using self as a receiver. I think it’s the greet method of the class itself. At the beginning, I didn’t understand anything because I saw only this.

self.rb


class Greeter
def self.greet
puts "Good evening"
end
end

Greeter.greet

The reason for considering it as the greet method of the class itself is that the following can be written. You can call the greet method by writing the class name directly instead of self, like a receiver.

class.rb


class Greeter
def Greeter.greet
puts "Good evening"
end
end

Greeter.greet

So why use self in class methods?

  • Write self and differentiate from instance method
  • Prevent double writing

It seems that there are two points. At a later date, the difference from the instance method will be described.

Aside from class

In a certain code, I have seen that slef is used even in class_attribute in Active Support of rails. This also works even if I rewrite the self part to the Firebase class, so it seems that this class_attribute is called and is classified into Class# like the character.

firebase.rb


class Firebase
  require'google/cloud'

  class_attribute :contact
  self.contact = Google::Cloud.new(ENV['GOOGLE_PROJECT']).firestore
end

The first step of logic is essential

If the ball is fired from the top left to the bottom right 45 degrees on the billiard table and has an ideal reflection on the wall, what coordinate does the ball fall through into the hole in the corner?

When I received the game programming code as an issue (I later realized that it was game programming), I was working on logic combination in a finite loop until now (for example, 1 month user I couldn’t come up with an idea of this kind of infinite loop logic like I want to loop only the number of users fetched from the DB because I calculate the number of registrants. In the first place, even the foundation of thinking logic was not left, and this task could not be solved until it was answered.

billiard.rb


x_max = ARGV[0]
y_max = ARGV[1]

if !x_max || !y_max
  puts "Please specify arguments"
  exit 1
end

x_max = x_max.to_i
y_max = y_max.to_i

# Status
x = 1
y = 1
step = 1
x_way = 1
y_way = 1

puts "#{step} (#{x},#{y})"
x += 1
y += 1
loop do
  step += 1
  puts "#{step} (#{x},#{y})"

  if y == 1 && x == x_max || x == 1 && y == y_max || x == 1 && y == 1 || x == x_max && y == y_max
    puts "GOAL!!"
    break
  elsif x == x_max
    x_way = -1
  elsif y == y_max
    y_way = -1
  elsif x == 1
    x_way = 1
  elsif y == 1
    y_way = 1
  end

  x += x_way
  y += y_way
end

Below is a summary of what we have learned.

State management

A program is a change in the status of the data it handles. To make this billiard problem concrete,

  • Board made by the number given by the argument
  • Initial value of the ball
  • Ball traveling direction

Think about what changes the state and what can be fixed to create the desired code.

boundary condition

Write the processing that gives movement to the conditional expression and state by determining what triggered the change in state or the end. To make this billiard problem concrete,

  • Minimum and maximum values along the x- and y-axes on the four walls

It means that. By suppressing this, it is possible to judge at what timing the movement state of the ball changes. It becomes important when considering conditional expressions. The logic run is aware of boundary conditions.

Correct infinite loop

An infinite loop in programming is

** Use an infinite loop for things where the exit condition is visible while executing the loop. In other words, use an infinite loop by taking advantage of the fact that the end condition is not known when writing the program. **

The program of the game is exactly that the dash motion of the character depends on the user’s input, so it is necessary to loop infinitely as long as it is input.

Use in an infinite loop is also necessary for DB relations. When processing a large amount of data in a loop, the number of loops that is almost the same as an infinite loop is required, so it is necessary to design so that it will end when certain conditions are met.

Impression

Since I couldn’t write the code either in a finite loop or a loop that continued until a certain condition in the logic formation, I couldn’t reach this task at all, but it was an important thought as infinite. It was nice to understand the loop.