Ruby cheat sheet / variable / class / module

It's just a cheat sheet for Ruby beginners.
 

Screen Shot 2014-02-04 at 12.56.39 PM

 


# Module is a part which is shared by many classes
# You can include module like, 'include Keyboard'
# It's neseccery to put module before class definition
module Keyboard

    # You can access instance variable if it exist
    def type(key)
        puts "You typed [#{key}] with #{self.name}.\n\n"
    end

end




class Phone

    # Module
    include Keyboard

    # Class constant
    TYPE = "mobile"

    # Class variable
    # This is shared by every Phone instance
    # This can't be called by accessor method
    @@count = 0

    # Class instance variable
    @camera = "NO"

    # Execute when Phone new instance is created
    # Same as constructor in JAVA
    def initialize(name, version, color)
        # Instance variable
        # This is unique in each instance
        @name = name
        @version = version
        @color = color
        # Change class variable when instance is created
        @@count += 1
    end

    # Accessor method
    # You can access instance variable like, phone.name
    # These have different permission
    attr_reader     :name       # read
    attr_writer     :version    # write
    attr_accessor :color        # read/write

    def start
        puts "Welcome to #{self.color} #{self.name}! (instance method)\n\n"
    end

    def sleep(wake_up_time)
        puts "See you at #{wake_up_time}, zzz.... (instance method)\n\n"
    end

    def exit
        puts "Good bye! from #{self.name} (instance method)\n\n"
    end

    def change_color(color)
        # Change value of instance variable
        self.color = color
        puts "You changed color to #{color} (instance method)\n\n"
    end

    # This is class method, using 'self.'
    def self.show_device_count
        puts "@@count : #{@@count} ('Class' method with class variable)\n\n"
    end

    # This is class method, using 'self.'
    def self.camera?
        puts "Camera : #{@camera}\n\n"
    end

end





class SmartPhone < Phone

    # Module
    # include Keyboard

    # Additional method only for SmartPhone
    def take_photo
        puts "You can take photo with smartphone!\n\n"
        # Private function
        store_photo(self)
    end

    # Override method and execute super class method inside
    def change_color(color, surface)
        super(color)
        puts "Now your phone's surface is polished! (Override instance method)\n\n"
    end

    # This method doesn't work
    # @camera is not accessible from SmartPhone class
    def self.camera?
        puts "Camera : #{@camera} (of course dosn't work)\n\n"
    end


    # You can call private method within SmartPhone class
    private

        def store_photo(device)
            puts " -> Photo is saved in #{device.name}. (Private method)\n\n"
        end

end






# Create new instance from Phone class
# The arguments are used for initialize method
phone = Phone.new('Traditional Phone', 1.0, 'black')

# Class constant
puts "\n\nTYPE : #{Phone::TYPE}"

# Class method with class instance variable
puts Phone.camera?

# Instance method
phone.start

# Instance methods with arguments
phone.change_color('white')

# You can write version but can't read
# because of accessor method
phone.version = 1.1

now = Time.now()
wake_up_time = now + 2*60*60
phone.sleep(wake_up_time)

# Method from Keyboard module
phone.type('t')

# Class method
Phone.show_device_count

# Create instance from SmartPhone class
iphone = SmartPhone.new('iPhone', 5.2, 'black')
iphone.start

# Now you can see class variable is changed
Phone.show_device_count

# SmartPhone class has more methods than Phone class
iphone.take_photo

# Override method
iphone.change_color('gold', 'polished')

# This does'nt show anything because
# clas instance variable in not accessible from SmartPhone class
SmartPhone.camera?

# Method from Keyboard module
iphone.type('i')

phone.exit
iphone.exit


  
 
The following should be the result.


TYPE : mobile
Camera : NO


Welcome to black Traditional Phone! (instance method)

You changed color to white (instance method)

See you at 2014-02-04 14:24:50 +0100, zzz.... (instance method)

You typed [t] with Traditional Phone.

@@count : 1 ('Class' method with class variable)

Welcome to black iPhone! (instance method)

@@count : 2 ('Class' method with class variable)

You can take photo with smartphone!

 -> Photo is saved in iPhone. (Private method)

You changed color to gold (instance method)

Now your phone's surface is polished! (Override instance method)

Camera :  (of course dosn't work)

You typed [i] with iPhone.

Good bye! from Traditional Phone (instance method)

Good bye! from iPhone (instance method)

  

何かありましたらコメントをどうぞ

comments powered by Disqus

人気の記事

950 Points チリ出身のギタリストが弾くドラゴンボールZがむちゃくちゃかっこいい…
774 Points Wordpress + Heroku + PostgreSQL + Amazon S3 = ¥0 / 無料でサイト運営
700 Points Rubyのチートシート 変数 / クラス / モジュール
524 Points Rubyのチートシート / アクティブサポート
451 Points 紙のデザイナーがウェブ開発できるようになるまでに必要なこと
435 Points Rails / Google Analyticsのデータを使って分析や管理画面のためのグラフをつくる
323 Points RailsとHerokuでノーティフィケーションをプッシュする / PusherとTurbolinksの兼ね合い
222 Points Rails / RSpec テスト書いたことない メンドクサイ(n´Д`)という時のチートシート
193 Points Rails / Ajaxを使って画面遷移しない一時保存機能をつける
193 Points Protractorでスクレイピングしてみた