This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| anonymous_class = Class.new(String) do | |
| def hello(name) | |
| "Hello, #{name}" | |
| end | |
| end | |
| ex_class = anonymous_class.new | |
| p ex_class.hello("murajun1978") # => murajun1978 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Foo | |
| def bar | |
| :bar | |
| end | |
| def baz | |
| :baz | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Enemy | |
| def initialize | |
| @enemies = {} | |
| end | |
| def method_missing(method_name, *args) | |
| enemy = method_name.to_s | |
| if enemy =~ /=\z/ | |
| @enemies[enemy.chop] = args[0] | |
| else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [1, 2, 3].select {|n| n == 2} # => [2] | |
| [1, 2, 3].select &2.method(:==) # => [2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Foo | |
| private_class_method def self.common_method(*syms) | |
| syms.each do |sym| | |
| define_method sym do | |
| puts sym | |
| end | |
| end | |
| end | |
| common_method :murajun, :murajun1978 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Murajun | |
| class String | |
| def to_s | |
| "murajun1978" | |
| end | |
| end | |
| end | |
| "Ruby".to_s # => "Ruby" | |
| Murajun::String.new.to_s # => "murajun1978" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class String | |
| def to_s | |
| "murajun1978" | |
| end | |
| end | |
| "Ruby".to_s # => "murajun1978" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Foo | |
| def foo | |
| :foo | |
| end | |
| end | |
| class Bar | |
| include Foo | |
| def bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ary = Array.new(2, Hash.new) | |
| ary =[0][:dog] = "pochi" | |
| ary #=> ??? | |
| ary = Array.new(2){Hash.new} | |
| ary[0][:dog] = "pochi" | |
| ary #=> ??? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ex. | |
| [[5,3],[7,2],[8,/],[X],[7,1],[9,-],[6,2],[X],[6,/],[8,-]] | |
| score: 126 | |
| []: 1フレーム | |
| n: 倒したピンの数 | |
| X: ストライク | |
| /: スペア | |
| -: ガター |