ruby:メモ
コーディング規約
- http://shugo.net/ruby-codeconv/codeconv.html
- http://www.loveruby.net/w/RubyCodingStyle.html
- http://www.caliban.org/ruby/rubyguide.shtml
- https://github.com/styleguide/ruby
- https://github.com/thoughtbot/guides
- https://github.com/cookpad/styleguide
覚えておきたいメソッド
"hello, world".upcase
=> "HELLO, WORLD"
"hello, world".reverse
=> "dlrow, olleh"
"hello, world".methods
=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*....
object_id(=self.object_id)
=> 17084920
1.+(1)(=1 + 1)
=> 2
object.singleton_methods
=> [:to_s, :inspect, :context, :conf, :irb_quit,...
hello = "hello, world"
hello.class
=> String
クラスとインスタンス
インスタンス変数の定義
class Book
def author
@author
end
def title
@title
end
def isbn
@isbn
end
def author=(author)
@author = author
end
def title=(title)
@title = title
end
def isbn=(isbn)
@isbn = isbn
end
end
↓等価↓
class Book
attr_accessor :author, :title, :isbn
end
外部ファイルの読み込み
require_relative 'book'
=> true
インスタンス生成
book = Book.new
=> #<Book:0x007fba434564f3>
値の設定
book.author = "jon smith"
=> "jon smith"
値の取得
book.author
=> "jon smith"