LoginSignup
4
4

More than 5 years have passed since last update.

ruby:メモ

Last updated at Posted at 2015-04-26

ruby:メモ

コーディング規約

覚えておきたいメソッド

"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"
4
4
0

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
4
4