Class: Pebbles::Nyarucode

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles/nyarucode.rb,
lib/pebbles/nyarucode.rb

Constant Summary collapse

VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.convert(orig_source) ⇒ Object

任意のソースコードに対する、うー!にゃー!化されたRubyコード。



125
126
127
128
129
130
131
132
133
# File 'lib/pebbles/nyarucode.rb', line 125

def self.convert(orig_source)
  # 以下のコードは、分かりやすく書くとこうなる
  # (->(arg1, arg2){ send arg1, arg2 }
  # )["eval", orig_source]
  # すなわち、"Kernel.eval(#{orig_source})" という意味になる。
  # ここで、orig_sourceは所望のソースコードを出力するための
  # Rubyコードである(Pebbles::Nyarucode.strメソッドで生成可能)。
  "(->(」・ω・)」うー!,(/・ω・)/にゃー!\n){send(」・ω・)」うー!,(/・ω・)/にゃー!)}\n)[#{Pebbles::Nyarucode.str("eval")},#{Pebbles::Nyarucode.str(orig_source)}]"
end

.num(i) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pebbles/nyarucode.rb', line 78

def self.num(i)
  i = i.to_i
  if i < 0
    return "-(#{Pebbles::Nyarucode.num(-i)})"
  end
  if i == 0
    return Pebbles::Nyarucode.num_base(0)
  end
  
  nyaruko12 = Pebbles::Nyarucode.num_base(12)
  arr = i.base_of(12)
  init_print = true
  
  result_list = []
  
  # 12進法で1桁目
  digit = arr.shift
  result_list << "(#{Pebbles::Nyarucode.num_base(digit)})" if digit != 0
  
  # 12進法で2桁目
  unless arr.empty?
    digit = arr.shift
    result_list << "(#{Pebbles::Nyarucode.num_base(digit)})*\n(#{nyaruko12})" if digit != 0
  end
  
  # 12進法で3桁目以降
  arr.each_with_index do |digit, i|
    result_list << "(#{Pebbles::Nyarucode.num_base(digit)})*\n(#{nyaruko12})**\n(#{Pebbles::Nyarucode.num_base(i+2)})"
  end
  
  result_list.join("+\n")
end

.num_base(i) ⇒ Object

結果が「1つの整数」となる場合限定で、うー!にゃー!化されたRubyコードを出力する。



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pebbles/nyarucode.rb', line 45

def self.num_base(i)
  case i
  when 0
    '/・ω・)/=~(%/・ω・)/)'
  when 1
    '/・ω・)/=~%(/・ω・)/うー!にゃー!)'
  when 2
    '/・ω・)/=~\'(/・ω・)/うー!にゃー!\''
  when 3
    '/・ω・)/=~\' (/・ω・)/うー!にゃー!\''
  when 4
    '/・ω・)/=~\'うー(/・ω・)/にゃー!\''
  when 5
    '/・ω・)/=~\'うー!(/・ω・)/にゃー!\''
  when 6
    '/・ω・)/=~\'うー! (/・ω・)/にゃー!\''
  when 7
    '/・ω・)/=~\'にゃー! (/・ω・)/\''
  when 8
    '/・ω・)/=~\'うー!にゃー(/・ω・)/\''
  when 9
    '/・ω・)/=~\'うー!にゃー!(/・ω・)/\''
  when 10
    '/・ω・)/=~\'(」・ω・)」 (/・ω・)/うー!にゃー!\''
  when 11
    '/・ω・)/=~\'(」・ω・)」うー(/・ω・)/にゃー!\''
  when 12
    '/・ω・)/=~\'(」・ω・)」うー!(/・ω・)/にゃー!\''
  else
    raise ArgumentError, "Unexpected error: only integers 1 to 12 are accepted (given: #{i.inspect})"
  end
end

.str(orig_string) ⇒ Object

結果が「1つの文字列」となる場合限定で、うー!にゃー!化されたRubyコード。



114
115
116
117
118
119
120
# File 'lib/pebbles/nyarucode.rb', line 114

def self.str(orig_string)
  result = "''"
  orig_string.each_codepoint do |c|
    result << "<<\n#{Pebbles::Nyarucode.num(c)}"
  end
  result
end