1.Ruby in normaly
階乗 n! を計算する
-Variable
-Method
-Number value
-String
-Structure
-Output
2. How to use Ruby without using any numbers or letters
全部文字と数字を削除しましょう
1.Variable
Rubyに_とか$などでVariableを作られます。
普段:
my_number = 2
=>
_ = 2
2. Number
Ruby_docsによると
pid -> Integer
カレントプロセスのプロセス ID を返します。変数 $$ の値と同じです。
$$の値はいつも != 0
+,-,x,/, <<
で任意ナンバーが作られます。
$$/$$ = 1
$$ - $$ = 0
my_number = 2
は _ = $$/$$ + $$/$$
になります。
楽しい?
3. String
普段:
my_string = "basic"
"basic" = "b" << "a" << "s" << "i" << "c"
ASCIIに基づけASCIIコードを使って、Stringが作られます。
"b" << "a" << "s" << "i" << "c" <=> '' << 98 << 97 << 115 <<105 << 99
結果:
__ = '' << 98 << 97 << 115 <<105 << 99
5. Method
普段:
def count
x = 1
x += 1
p x
end
メソッドの理想
“package code first, call it later”
Procとほぼ一生です。
my_proc = Proc.new{|x| x + 1; p x}
... # more code
my_proc.call(2) #=> 3
characterとnumberを削除しる
_ = $$/$$
___ = Proc.new{|__| __ + _; p __}
... # more code
___.call(_+_) #=> 3
Proc.new
の代わりに、 ->
シンボルが作られます。
call
使わず、 []
シンボルが作られます。
結果:
_ = $$/$$
___ = -> {|_| _ + ; p _}
... # more code
__.[(+_)] #=> 3
almost finish!
6. structure
どんな言語でも、Structureの種類は3つがあります。
- 1.sequence
- 2.selection
- 3.iteration
6.1.Sequence
Sequenceでは,普通な実装です。上から下までで実行します。
6.2.selection
Character禁止なので、if-esle
がつかえない。
だが、if-else
の代わりに、?:
のシンタックスを作る
6.3.interation
ループを作るために、リカーシブとLambdaシンタックスを兼ねる。
普段:
(1..10).each do |x|
p x
end
Alphanumericを使わない:
(___ = -> {
x += 1
p x
x <= 10 ? ___[] : 0
})[]
- Output
今まで、結果を表示するために、putsのコマンドを作っています。
Rubyに
$>
シンボルはputsの使い方と同じです。
puts "basic"
<=>
$> '' << 98 << 97 << 115 <<105 << 99 # => basic
3.Summary
例
def factorial n
return n if n == 1
n*factorial(n - 1)
end
p factorial 5
_ = $$ / $$ #1
__ = _ + _ #2
@_ = _ + _ # 2
$_ = @_ + _ # 3
$__ = @_ + $_ # 5
$-_ = $__ * $_ # 15
@__ = $-_*$__ + $-_*$_ - $__
@___ = $__*__*$__ + $__ + $_
$___ = '' << @__ - _ << @__ - _ - $__*@_ - $_ << @__ << @__ + __ << @__ + $_ - $__*@_ << @__ + _ << @___
#r #e #s #u #l #t #:
(___ = -> (__){
__ != _ ? __*___[__ - _] : _
})
$> << $___
$> << ___[$__]
参考:http://threeifbywhiskey.github.io/2014/03/05/non-alphanumeric-ruby-for-fun-and-not-much-else/