LoginSignup
5

More than 3 years have passed since last update.

Rubyコーディング規約 ❏Ruby❏

Last updated at Posted at 2019-11-24

コーディング規約

ルールではなくマナー
僕が気をつけているポイントを紹介するよ!

インデントは2つ

if num > 0
●●if num < 100
●●●●puts "100より小さい正の数"
●●end
end

クラス内の各構成要素の区切りに空行を入れる

ただし、最初と最後は空けない。

tweets_controller.rb
class TweetsController < ApplicationController   #最初は空けない
  before_action :move_to_index, except: :index
#---ここ---
  def index
    @tweets = Tweet.all
  end
#---ここ---
  def new
    @tweet = Tweet.new
  end
#---ここ---
  def create
    Tweet.create(tweet_params)
  end
#---ここ---
  private
    def tweet_params
      params.require(:tweet).permit(:text, :image)
    end
#---ここ---
    def move_to_index
      redirect_to root_path unless user_signed_in?
    end
end   #最後も空けない

if文のthen
while文のdoは省略する

if num < 100
  puts "100より小さい"
end

while num < 100
  puts num
  num += 1
end

文字列リテラルはダブルクォーテーション

puts "ダブル!!"

もちろん例外もあるよ!

三項演算子は簡単な条件の時のみ

puts score > 60 ? "合格":"不合格"

少しでも複雑ならif文!

変数・メソッド名は小文字&スネークケース

#変数
num = 15
add_color = "yellow"

#メソッド
def index
end

def add_something
end

2単語繋げる場合は_を使う!



参考

https://shugo.net/ruby-codeconv/codeconv.html



ではまた!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5