Skip to content

Instantly share code, notes, and snippets.

View Shinpeim's full-sized avatar

Shinpei Maruyama Shinpeim

View GitHub Profile
a = ->{10}
b = ->{10}
c = ->{a.call + b.call}
puts c.call # => 20
a = ->{20}
puts c.call # => 30
require 'observer'
class ReactiveValue
include Observable
attr_reader :value
def set(value)
@value = value
changed
notify_observers(self)
def try_something
begin
nyan
rescue
begin
wan
rescue
piyo
end
end
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|
/*
* here is comment
*/
// needed to compile comment
/*
* here is comment
*/
// needed to compile comment
1;
class Collision < StandardError; end
def generate_random_string
raise Collision;
end
def nyan(retries = 10)
s = generate_random_string
rescue Collision => e
p "コリジョンが発生しました"
class Collision < StandardError; end
def generate_random_string
raise Collision;
end
def nyan(retries = 10)
s = generate_random_string
rescue Collision => e
p "コリジョンが発生しました"
object Nyan {
class A {
def nyan = "A"
}
implicit class B (self:A){
def nyan = "B"
def wan = "B"
}
@Shinpeim
Shinpeim / Decorator.scala
Last active August 29, 2015 13:58
Decorator パターンの例
object DecoratorSample extends App{
val nyan: Nyan = new NyanImpl
nyan.nyan("にゃーん") // => にゃーん
val nyanWithDecorator: Nyan = new NyanDecorator(new NyanImpl)
nyanWithDecorator.nyan("にゃーん") // => にゃーんなのです
// という感じで、既存のクラス(NyanImpl)を書き換えずに、
// その動きを変える(拡張する)ことができるのが
// デコレーターパターン
@Shinpeim
Shinpeim / Decorator.scala
Last active August 29, 2015 13:58
もうちょっとよくした
trait Nyan {
def nyan(arg: String): String
}
// デコレーターで拡張される側のクラス
class NyanImpl extends Nyan {
def nyan(arg: String): String = {
arg + "にゃーん"
}
}