5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyのメソッド名の付け方、構文を英語とみなしてみる方法。

Last updated at Posted at 2017-06-21

基本

http://www.find-job.net/startup/english-for-engineers-naming-conventions
http://futurismo.biz/archives/2326

#冠詞

newをaに、既に生成されたオブジェクト、定数へのアクセスをtheとする

# an apple
Apple.new

# the warter
Water
# Alice has the pen.
alice.have(pen)

#不定詞
形容詞を取る時はto be

# This expect to be blank.
expect(array, &:blank?)

動詞を取る時はtoは&

# Alice bring something cold to drink.
something = [Milk.new, Beer.new, Liquor.new]

alice.bring(something, :cold, &:drink)

#前置詞
キーワード引数にせよ

# Alice has a gift for Bob.
alice.have(Gift.new, for: bob)

メンバー変数は、変数の中身が前置詞の後に来るイメージ

#Alice was registerd at 1988-10-19.
alice.registered_at #=> 1988-10-19

#助動詞
アンダーバーでメソッド名のプレフィックスにせよ
iOSっぽくwill didでコールバック登録を表現せよ

view.will_appear do
# ...
end

#接続詞
文を接続詞とみなし、if文のブロックの中は全て暗黙で後置に文が続いているとみなす。

# Alice will help bob if Bob is busy.
alice.help(bob) if bob.busy?

# Mallory will obstruct bob if Bob is busy.
mallory.obstruct(bob) if bob.busy?

if bob.busy?
  alice.help(bob)
  mallory.obstruct(bob)
end

#肯定文にハテナマークをつけて疑問文と解釈する

# Array is blank?
array.blank?

#デフォルト引数でオブジェクトをnewして目的語を省略する

class Alice
  def read(book = Book.new)
    # ...
  end
end

# Alice read (a book).
alice.read

#グローバル変数でコンテキストを表現して目的語を省略する
ActiveRecordのscopingにならって、スレッドセーフなグローバル変数にする
https://apidock.com/rails/ActiveRecord/Relation/scoping

class Alice
  def read(book = current_object)
    # ...
  end
end

def current_object
  Thread.current[:current_object]
end

def current_object=(obj)
  Thread.current[:current_object] = obj
end

def context(obj)
  previous, current_object = current_object, obj
  yield
ensure
  current_object = previous
end

context(Letter.new) do
  # Alice read (the letter).
  alice.read
end

#これに影響を受けました
http://qiita.com/gazayas/items/3d352d1b6ec9a225c6f6

5
2
2

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?