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
a = ->{10} | |
b = ->{10} | |
c = ->{a.call + b.call} | |
puts c.call # => 20 | |
a = ->{20} | |
puts c.call # => 30 | |
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
require 'observer' | |
class ReactiveValue | |
include Observable | |
attr_reader :value | |
def set(value) | |
@value = value | |
changed | |
notify_observers(self) |
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
def try_something | |
begin | |
nyan | |
rescue | |
begin | |
wan | |
rescue | |
piyo | |
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 Solver | |
def initialize(open_char, close_char) | |
@open_char = open_char | |
@close_char = close_char | |
@count = 0 | |
end | |
def run(string) | |
chars = string.split("") | |
chars.each do |char| |
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
/* | |
* here is comment | |
*/ | |
// needed to compile comment | |
/* | |
* here is comment | |
*/ | |
// needed to compile comment | |
1; |
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 Collision < StandardError; end | |
def generate_random_string | |
raise Collision; | |
end | |
def nyan(retries = 10) | |
s = generate_random_string | |
rescue Collision => e | |
p "コリジョンが発生しました" |
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 Collision < StandardError; end | |
def generate_random_string | |
raise Collision; | |
end | |
def nyan(retries = 10) | |
s = generate_random_string | |
rescue Collision => e | |
p "コリジョンが発生しました" |
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
object Nyan { | |
class A { | |
def nyan = "A" | |
} | |
implicit class B (self:A){ | |
def nyan = "B" | |
def wan = "B" | |
} |
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
object DecoratorSample extends App{ | |
val nyan: Nyan = new NyanImpl | |
nyan.nyan("にゃーん") // => にゃーん | |
val nyanWithDecorator: Nyan = new NyanDecorator(new NyanImpl) | |
nyanWithDecorator.nyan("にゃーん") // => にゃーんなのです | |
// という感じで、既存のクラス(NyanImpl)を書き換えずに、 | |
// その動きを変える(拡張する)ことができるのが | |
// デコレーターパターン |
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
trait Nyan { | |
def nyan(arg: String): String | |
} | |
// デコレーターで拡張される側のクラス | |
class NyanImpl extends Nyan { | |
def nyan(arg: String): String = { | |
arg + "にゃーん" | |
} | |
} |
OlderNewer