pebbles-nyarucode 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +23 -0
- data/.gemtest +0 -0
- data/Gemfile +4 -0
- data/History.txt +4 -0
- data/Manifest.txt +11 -0
- data/README.txt +58 -0
- data/Rakefile +17 -0
- data/bin/nyarucode +21 -0
- data/lib/pebbles/nyarucode.rb +135 -0
- data/pebbles-nyarucode.gemspec +17 -0
- data/spec/nyarucode_spec.rb +78 -0
- data/spec/spec_helper.rb +8 -0
- metadata +92 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/.gemtest
ADDED
File without changes
|
data/Gemfile
ADDED
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
= nyarucode-ruby
|
2
|
+
|
3
|
+
* http://rubygems.org/gems/pebbles-nyarucode
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
To convert a ruby code into "U-! Nya-!" ruby code
|
8
|
+
(from "Haiyore! Nyaruko-san")
|
9
|
+
|
10
|
+
== INSTALL:
|
11
|
+
|
12
|
+
$ sudo gem install pebble-nyarucode
|
13
|
+
|
14
|
+
== FEATURES/PROBLEMS:
|
15
|
+
|
16
|
+
== SYNOPSIS:
|
17
|
+
|
18
|
+
$ nyarucode path/to/sample.rb > sample-converted.rb
|
19
|
+
$ ruby sample.rb
|
20
|
+
$ ruby sample-converted.rb # probably be the same as the upper one
|
21
|
+
|
22
|
+
== REQUIREMENTS:
|
23
|
+
|
24
|
+
* Ruby 1.9 or newer
|
25
|
+
|
26
|
+
== DEVELOPERS:
|
27
|
+
|
28
|
+
After checking out the source, run:
|
29
|
+
|
30
|
+
$ rake newb
|
31
|
+
|
32
|
+
This task will install any missing dependencies, run the tests/specs,
|
33
|
+
and generate the RDoc.
|
34
|
+
|
35
|
+
== LICENSE:
|
36
|
+
|
37
|
+
(The MIT License)
|
38
|
+
|
39
|
+
Copyright (c) 2012 H.Hiro(Maraigue)
|
40
|
+
|
41
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
42
|
+
a copy of this software and associated documentation files (the
|
43
|
+
'Software'), to deal in the Software without restriction, including
|
44
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
45
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
46
|
+
permit persons to whom the Software is furnished to do so, subject to
|
47
|
+
the following conditions:
|
48
|
+
|
49
|
+
The above copyright notice and this permission notice shall be
|
50
|
+
included in all copies or substantial portions of the Software.
|
51
|
+
|
52
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
53
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
54
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
55
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
56
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
57
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
58
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
|
6
|
+
# Hoe.plugin :compiler
|
7
|
+
# Hoe.plugin :gem_prelude_sucks
|
8
|
+
# Hoe.plugin :inline
|
9
|
+
# Hoe.plugin :racc
|
10
|
+
# Hoe.plugin :rcov
|
11
|
+
# Hoe.plugin :rubyforge
|
12
|
+
|
13
|
+
Hoe.spec 'pebbles-nyarucode' do
|
14
|
+
developer('H.Hiro(Maraigue)', 'main@hhiro.net')
|
15
|
+
end
|
16
|
+
|
17
|
+
# vim: syntax=ruby
|
data/bin/nyarucode
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
libpath = File.expand_path(File.dirname(__FILE__) + "/../lib")
|
4
|
+
$LOAD_PATH << libpath unless $LOAD_PATH.include?(libpath)
|
5
|
+
require 'pebbles/nyarucode'
|
6
|
+
|
7
|
+
def main
|
8
|
+
if ARGV.empty?
|
9
|
+
STDERR.puts "Usage: nyaruko [source-file]"
|
10
|
+
return
|
11
|
+
end
|
12
|
+
|
13
|
+
ARGF.set_encoding("ASCII-8BIT")
|
14
|
+
STDOUT.set_encoding("utf-8")
|
15
|
+
STDOUT.puts "#!/usr/bin/env ruby"
|
16
|
+
STDOUT.puts "# -*- coding: utf-8 -*-"
|
17
|
+
STDOUT.puts Pebbles::Nyarucode.convert(ARGF.read.force_encoding("UTF-8"))
|
18
|
+
end
|
19
|
+
|
20
|
+
main
|
21
|
+
|
@@ -0,0 +1,135 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
# Rubyのソースコードを与えると、同じ働きをする
|
5
|
+
# 「(」・ω・)」うー!(/・ω・)/にゃー!」だらけの
|
6
|
+
# コードを出力するプログラム
|
7
|
+
#
|
8
|
+
# Ruby1.9系列限定、文字コードは1種類に統一する必要あり
|
9
|
+
#
|
10
|
+
# 元ネタ(JavaScript版):
|
11
|
+
# http://sanya.sweetduet.info/unyaencode/
|
12
|
+
# http://gigazine.net/news/20120528-unyaencode/
|
13
|
+
|
14
|
+
class Pebbles
|
15
|
+
class Nyarucode
|
16
|
+
VERSION = '0.1.0'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# n進法
|
21
|
+
|
22
|
+
class Integer
|
23
|
+
def base_of(b)
|
24
|
+
if self < 0
|
25
|
+
return((-self).base_of(b).map{ |x| -x })
|
26
|
+
elsif self == 0
|
27
|
+
return [0]
|
28
|
+
end
|
29
|
+
|
30
|
+
result = []
|
31
|
+
x = self
|
32
|
+
while x > 0
|
33
|
+
result << (x % b)
|
34
|
+
x /= b
|
35
|
+
end
|
36
|
+
|
37
|
+
result
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Pebbles::Nyarucode
|
42
|
+
# 結果が「1つの整数」となる場合限定で、うー!にゃー!化された
|
43
|
+
# Rubyコードを出力する。
|
44
|
+
|
45
|
+
def self.num_base(i)
|
46
|
+
case i
|
47
|
+
when 0
|
48
|
+
'/・ω・)/=~(%/・ω・)/)'
|
49
|
+
when 1
|
50
|
+
'/・ω・)/=~%(/・ω・)/うー!にゃー!)'
|
51
|
+
when 2
|
52
|
+
'/・ω・)/=~\'(/・ω・)/うー!にゃー!\''
|
53
|
+
when 3
|
54
|
+
'/・ω・)/=~\' (/・ω・)/うー!にゃー!\''
|
55
|
+
when 4
|
56
|
+
'/・ω・)/=~\'うー(/・ω・)/にゃー!\''
|
57
|
+
when 5
|
58
|
+
'/・ω・)/=~\'うー!(/・ω・)/にゃー!\''
|
59
|
+
when 6
|
60
|
+
'/・ω・)/=~\'うー! (/・ω・)/にゃー!\''
|
61
|
+
when 7
|
62
|
+
'/・ω・)/=~\'にゃー! (/・ω・)/\''
|
63
|
+
when 8
|
64
|
+
'/・ω・)/=~\'うー!にゃー(/・ω・)/\''
|
65
|
+
when 9
|
66
|
+
'/・ω・)/=~\'うー!にゃー!(/・ω・)/\''
|
67
|
+
when 10
|
68
|
+
'/・ω・)/=~\'(」・ω・)」 (/・ω・)/うー!にゃー!\''
|
69
|
+
when 11
|
70
|
+
'/・ω・)/=~\'(」・ω・)」うー(/・ω・)/にゃー!\''
|
71
|
+
when 12
|
72
|
+
'/・ω・)/=~\'(」・ω・)」うー!(/・ω・)/にゃー!\''
|
73
|
+
else
|
74
|
+
raise ArgumentError, "Unexpected error: only integers 1 to 12 are accepted (given: #{i.inspect})"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.num(i)
|
79
|
+
i = i.to_i
|
80
|
+
if i < 0
|
81
|
+
return "-(#{Pebbles::Nyarucode.num(-i)})"
|
82
|
+
end
|
83
|
+
if i == 0
|
84
|
+
return Pebbles::Nyarucode.num_base(0)
|
85
|
+
end
|
86
|
+
|
87
|
+
nyaruko12 = Pebbles::Nyarucode.num_base(12)
|
88
|
+
arr = i.base_of(12)
|
89
|
+
init_print = true
|
90
|
+
|
91
|
+
result_list = []
|
92
|
+
|
93
|
+
# 12進法で1桁目
|
94
|
+
digit = arr.shift
|
95
|
+
result_list << "(#{Pebbles::Nyarucode.num_base(digit)})" if digit != 0
|
96
|
+
|
97
|
+
# 12進法で2桁目
|
98
|
+
unless arr.empty?
|
99
|
+
digit = arr.shift
|
100
|
+
result_list << "(#{Pebbles::Nyarucode.num_base(digit)})*\n(#{nyaruko12})" if digit != 0
|
101
|
+
end
|
102
|
+
|
103
|
+
# 12進法で3桁目以降
|
104
|
+
arr.each_with_index do |digit, i|
|
105
|
+
result_list << "(#{Pebbles::Nyarucode.num_base(digit)})*\n(#{nyaruko12})**\n(#{Pebbles::Nyarucode.num_base(i+2)})"
|
106
|
+
end
|
107
|
+
|
108
|
+
result_list.join("+\n")
|
109
|
+
end
|
110
|
+
|
111
|
+
# 結果が「1つの文字列」となる場合限定で、うー!にゃー!化された
|
112
|
+
# Rubyコード。
|
113
|
+
|
114
|
+
def self.str(orig_string)
|
115
|
+
result = "''"
|
116
|
+
orig_string.each_codepoint do |c|
|
117
|
+
result << "<<\n#{Pebbles::Nyarucode.num(c)}"
|
118
|
+
end
|
119
|
+
result
|
120
|
+
end
|
121
|
+
|
122
|
+
# 任意のソースコードに対する、うー!にゃー!化された
|
123
|
+
# Rubyコード。
|
124
|
+
|
125
|
+
def self.convert(orig_source)
|
126
|
+
# 以下のコードは、分かりやすく書くとこうなる
|
127
|
+
# (->(arg1, arg2){ send arg1, arg2 }
|
128
|
+
# )["eval", orig_source]
|
129
|
+
# すなわち、"Kernel.eval(#{orig_source})" という意味になる。
|
130
|
+
# ここで、orig_sourceは所望のソースコードを出力するための
|
131
|
+
# Rubyコードである(Pebbles::Nyarucode.strメソッドで生成可能)。
|
132
|
+
"(->(」・ω・)」うー!,(/・ω・)/にゃー!\n){send(」・ω・)」うー!,(/・ω・)/にゃー!)}\n)[#{Pebbles::Nyarucode.str("eval")},#{Pebbles::Nyarucode.str(orig_source)}]"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require './lib/pebbles/nyarucode'
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["H.Hiro(Maraigue)"]
|
6
|
+
gem.email = ["main@hhiro.net"]
|
7
|
+
gem.description = %{This program converts a ruby code into "U-! Nya-!" ruby code (from "Haiyore! Nyaruko-san"). As a result, your program is shown by many emoticons.}
|
8
|
+
gem.summary = %{To convert a ruby code into "U-! Nya-!" ruby code (from "Haiyore! Nyaruko-san")}
|
9
|
+
gem.homepage = "http://rubygems.org/gems/pebble-nyarucode"
|
10
|
+
|
11
|
+
gem.executables = Dir.glob("bin/*").map{ |x| File.basename(x) }
|
12
|
+
gem.test_files = Dir.glob("spec/*")
|
13
|
+
gem.name = "pebbles-nyarucode"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = Pebbles::Nyarucode::VERSION
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
4
|
+
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
describe Integer do
|
8
|
+
it "should be converted to another base correctly" do
|
9
|
+
# binary
|
10
|
+
0.base_of(2).should == [0]
|
11
|
+
1.base_of(2).should == [1]
|
12
|
+
2.base_of(2).should == [0, 1]
|
13
|
+
3.base_of(2).should == [1, 1]
|
14
|
+
4.base_of(2).should == [0, 0, 1]
|
15
|
+
5.base_of(2).should == [1, 0, 1]
|
16
|
+
|
17
|
+
# duodecimal (used in the program)
|
18
|
+
0.base_of(12).should == [0]
|
19
|
+
1.base_of(12).should == [1]
|
20
|
+
12.base_of(12).should == [0, 1]
|
21
|
+
23.base_of(12).should == [11, 1]
|
22
|
+
24.base_of(12).should == [0, 2]
|
23
|
+
145.base_of(12).should == [1, 0, 1]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe Pebbles::Nyarucode do
|
28
|
+
it "should generate appropriate Ruby codes for integers" do
|
29
|
+
# Since it uses duodecimal internally, numbers 0 to 12 should be tested
|
30
|
+
(0..12).each do |x|
|
31
|
+
eval(Pebbles::Nyarucode.num(x)).should == x
|
32
|
+
end
|
33
|
+
|
34
|
+
# Minus numbers and larger numbers than 12 should also be tested
|
35
|
+
[-14, -2, 14, 145, 10000].each do |x|
|
36
|
+
eval(Pebbles::Nyarucode.num(x)).should == x
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should generate appropriate Ruby codes for strings" do
|
41
|
+
['', ' ', 'うー!にゃー!'].each do |x|
|
42
|
+
eval(Pebbles::Nyarucode.str(x)).should == x
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should generate appropriate Ruby codes for any source codes" do
|
47
|
+
src = "def nyaruko_test_factorial(i); i <= 1 ? 1 : i * nyaruko_test_factorial(i - 1); end; nyaruko_test_factorial(5)"
|
48
|
+
eval(Pebbles::Nyarucode.convert(src)).should == eval(src)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be run in a command line and generate a new code" do
|
52
|
+
original_code = Tempfile.open('nyaruko-orig')
|
53
|
+
original_code.print <<FILE
|
54
|
+
#!/usr/bin/env ruby
|
55
|
+
# -*- coding: utf-8 -*-
|
56
|
+
|
57
|
+
puts "ニャル子さん"
|
58
|
+
|
59
|
+
def factorial(arg)
|
60
|
+
if arg <= 1
|
61
|
+
1
|
62
|
+
else
|
63
|
+
arg * factorial(arg - 1)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
puts factorial(5)
|
68
|
+
FILE
|
69
|
+
original_code.close
|
70
|
+
|
71
|
+
resulted_code = Tempfile.open('nyaruko-result')
|
72
|
+
resulted_code.print `#{BINPATH}/nyarucode #{original_code.path}`
|
73
|
+
resulted_code.close
|
74
|
+
|
75
|
+
`ruby #{resulted_code.path}`.should == "ニャル子さん\n120\n"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pebbles-nyarucode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- H.Hiro(Maraigue)
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-05-31 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rdoc
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "3.10"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: hoe
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "3.0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: |-
|
38
|
+
To convert a ruby code into "U-! Nya-!" ruby code
|
39
|
+
(from "Haiyore! Nyaruko-san")
|
40
|
+
email:
|
41
|
+
- main@hhiro.net
|
42
|
+
executables:
|
43
|
+
- nyarucode
|
44
|
+
extensions: []
|
45
|
+
|
46
|
+
extra_rdoc_files:
|
47
|
+
- History.txt
|
48
|
+
- Manifest.txt
|
49
|
+
- README.txt
|
50
|
+
files:
|
51
|
+
- .autotest
|
52
|
+
- Gemfile
|
53
|
+
- History.txt
|
54
|
+
- Manifest.txt
|
55
|
+
- README.txt
|
56
|
+
- Rakefile
|
57
|
+
- bin/nyarucode
|
58
|
+
- lib/pebbles/nyarucode.rb
|
59
|
+
- pebbles-nyarucode.gemspec
|
60
|
+
- spec/nyarucode_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- .gemtest
|
63
|
+
homepage: http://rubygems.org/gems/pebbles-nyarucode
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options:
|
68
|
+
- --main
|
69
|
+
- README.txt
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project: pebbles-nyarucode
|
87
|
+
rubygems_version: 1.8.11
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: To convert a ruby code into "U-! Nya-!" ruby code (from "Haiyore! Nyaruko-san")
|
91
|
+
test_files: []
|
92
|
+
|