Skip to content

Instantly share code, notes, and snippets.

@zzak
Created July 12, 2013 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzak/5986014 to your computer and use it in GitHub Desktop.
Save zzak/5986014 to your computer and use it in GitHub Desktop.
This is a very basic representation of internationalized documentation support in ruby.
# This is a very basic representation of internationalized documentation
# support in ruby.
#
# To use this demo try the following commands:
#
# ruby rdoc_i18n.rb Class#allocate
# ruby rdoc_i18n.rb -h Class#allocate > output.html
# # then open output.html in your browser
# ruby rdoc_i18n.rb --lang ja Class#allocate
#
# For more resources, and ideas, see:
#
# https://github.com/svenfuchs/i18n
# http://guides.rubyonrails.org/i18n.html
#
require 'rdoc'
require 'psych'
require 'optparse'
require 'ostruct'
doc = Psych.load(DATA.read)
options = OpenStruct.new
# Defaults
options.html = false
options.lang = "en"
OptionParser.new do |opts|
opts.on("-h", "--html", "Output HTML") { options.html = true }
opts.on("--lang LANGUAGE", "Language to use for translation of documents") do |v|
options.lang = v
end
end.parse!
query = ARGV.first.dup
tokens = RDoc::RubyLex.tokenize query, RDoc::Options.new
begin
if options.html
renderer = RDoc::Markup
formatter = RDoc::Markup::ToHtml.new(RDoc::Options.new, nil)
puts renderer.parse(doc[options.lang][tokens[0].text][tokens[1].text]).accept(formatter)
else
puts doc[options.lang][tokens[0].text][tokens[1].text]
end
rescue
raise "Nothing known about #{tokens[0].text}#{tokens[1].text}"
end
__END__
en:
"Class":
"#allocate": |
call-seq:
class.allocate() -> obj
Allocates space for a new object of <i>class</i>'s class and does not
call initialize on the new instance. The returned object must be an
instance of <i>class</i>.
klass = Class.new do
def initialize(*args)
@initialized = true
end
def initialized?
@initialized || false
end
end
klass.allocate.initialized? #=> false
"::new": |
call-seq:
Class.new(super_class=Object) -> a_class
Class.new(super_class=Object) { |mod| ... } -> a_class
Creates a new anonymous (unnamed) class with the given superclass
(or <code>Object</code> if no parameter is given). You can give a
class a name by assigning the class object to a constant.
If a block is given, it is passed the class object, and the block
is evaluated in the context of this class using
<code>class_eval</code>.
fred = Class.new do
def meth1
"hello"
end
def meth2
"bye"
end
end
a = fred.new #=> #<#<Class:0x100381890>:0x100376b98>
a.meth1 #=> "hello"
a.meth2 #=> "bye"
Assign the class to a constant (name starting uppercase) if you
want to treat it like a regular class.
ja:
"Class":
"#allocate": |
自身のインスタンスを生成して返します。生成したオブジェクトは 自身のインスタンスであること以外には何も特性を持ちません。
"::new": |
新しく名前の付いていない superclass のサブクラスを生成します。
名前のないクラスは、最初に名前を求める際に代入されている定数名を検 索し、見つかった定数名をクラス名とします。
p foo = Class.new # => #<Class:0x401b90f8>
p foo.name # => ""
Foo = foo # ここで p foo すれば "Foo" 固定
Bar = foo
p foo.name # => "Bar" ("Foo" になるか "Bar" になるかは不定)
ブロックが与えられた場合、生成したクラスを引数として クラスのコンテキストでブロックを実行します。以下のコードと同じです。
klass = Class.new(superclass)
klass.module_eval {|m| ... }
klass
この場合も生成したクラスを返します。 ブロックの実行は Class#initialize が行います。
[PARAM] superclass:
生成するクラスのスーパークラスを指定します。
例:
k = Class.new{|c|
def initialize
p "in initialize"
end
def hoge
p "hoge hoge hoge"
end
}
o = k.new #=> "in initialize"
o.hoge #=> "hoge hoge hoge"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment