Writing code Ruby

What is the act of writing code with ruby?

There are many things that you can't see just by reading the ruby writing method and thinking method in a book and moving it for the time being. If you have low reading comprehension to read the code of others, you will be stuck with habitual expressions. Also, if you don't know the habit, people who are accustomed to it may be surprised by the code you wrote. The naming method I wrote earlier is one of them, but this time I would like to summarize another kind of lessons learned.

Class method self

What is self written in this class method ... You can't call that method without writing it. I'm talking about why. The first question you think is. If you're not used to object-orientation in the first place, you'll stumble here.

The answer is to show the class method. A method whose class name is the receiver. Below is how to write a class method as seen in the book. When calling, the class name Greeter is used as 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 like a receiver. I think it's the class's own greet method. At the beginning, I saw only this, so I didn't understand anything.

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 you can write it as follows. You can also call the greet method by writing the class name directly like a receiver instead of self.

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 --Prevention of writing twice

It seems that there are two points. I will describe the difference from the instance method at a later date.

Class-related digression

I've seen some code also use slef in class_attribute in Rails Active Support. This also worked even if I rewrote the self part to the Firebase class, so it seems that this class_attribute is also called and is classified as Class # like a letter.

firebase.rb


class Firebase
  require 'google/cloud'

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

The first move of logic is essential

If you shoot the ball at 45 degrees from the upper left to the lower right on the pool table and make an ideal reflection on the wall, what coordinates will the ball go through and how many steps will it fall into the corner hole?

When I got the code of game programming as an issue (I realized later that it was game programming), because I was working on logic assembly in an infinite loop (for example, one month user) Since the number of registrants is calculated, the idea of this kind of infinite loop logic (like wanting to loop for the number of users retrieved from the DB) did not come up. In the first place, even the foundation of thinking logic has not been left, and this problem could not be solved until the answers were matched.

billiard.rb


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

if !x_max || !y_max
  puts "Please specify an argument"
  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 I have learned.

State management

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

--A board created by the number given by the argument --Initial value of the ball --Ball direction

Think about what state to change and what is fixed to create the desired code.

boundary condition

Write a conditional expression and a process that gives movement to the state by ascertaining what triggers the change in the state or the end. To make it concrete in this billiard problem,

--Minimum and maximum values in the x-axis and y-axis directions on the four walls

It means that. By suppressing this, it is possible to determine when the moving state of the ball changes. It is important when considering conditional expressions. When running logic, be aware of boundary conditions.

Correct infinite loop

Infinite loops in programming

** Use an infinite loop for things where you can see the end condition while executing the loop. In other words, use an infinite loop by taking advantage of something that you don't clearly know the end condition when writing a program. ** **

The game program is exactly that character's dash motion 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, it requires almost the same number of loops as an infinite loop, so it is necessary to design it so that it ends when certain conditions are met.

Impressions

In building the logic, I couldn't reach the task this time because I couldn't write the code in the first place whether it was a finite loop or a loop that continued to a certain condition, but infinite as an important thought. I'm glad I understood the loop.

Recommended Posts

Writing code Ruby
Ruby vertical writing
Format Ruby with VS Code
A memorandum for writing beautiful code
Writing code with classes and instances
[Note] [Beginner] Ruby writing memo (refactoring) 1
[Ruby] Array
Ruby basics
Ruby learning 5
[Ruby] Writing notes for cherry books [Notes for yourself]
Ruby basics
[Ruby on Rails] Code check using Rubocop-airbnb
Ruby Review 2
Differences in writing in Ruby, PHP, Java, JS
Ruby addition
Ruby learning 3
Ruby setting 2
Ruby problem ⑦
Ruby learning 2
Testable code
[Ruby] Block
Refactoring Ruby
ruby calculator
Write code using Ruby classes and instances
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
Nostalgic code game, Ruby Warrior is good
[Ruby] Module
Ruby Gem Code Reading settings logic Part 1
[Ruby] Code to display the day of the week
How to write code that thinks object-oriented Ruby