技術リンク
- 2005年 10月28日
- 投稿者 : ino
<http://www.fk.urban.ne.jp/home/kishida/>
- Kishida’s SITE – Java 入門講座
Java 初心者だけではなく Java で新しい事を学びたいときに参考になる。
たとえば、[2004-11-09], [2005-05-30] にもここの記事を参考にしてたり。
カテゴリー : 2005年 10月
<http://www.fk.urban.ne.jp/home/kishida/>
- Kishida’s SITE – Java 入門講座
Java 初心者だけではなく Java で新しい事を学びたいときに参考になる。
たとえば、[2004-11-09], [2005-05-30] にもここの記事を参考にしてたり。
- 処理を場合に応じて自由に変えられる。スマートになった Visitor パターンみたいなものかなぁ..
イテレーションを Iterator パターンなしでできる(内部で Iterator パターンを使って外には each メソッドを公開する等)。
class Iterator
@values
@nIndex
def initialize(*values)
@values = values.dup
@nIndex = 0
end
def each
yield(getNext) while hasNext
end
def hasNext
return @nIndex < @values.size
end
private :hasNext
def getNext
throw RangeError.new(“no element for next”) if !hasNext
res = @values[@nIndex]
@nIndex += 1
return res
end
private :getNext
end
iterator = Iterator.new(0,1,2,3,4)
iterator.each{ |i|
print( i * 2, “, ” )
}
# result:
# 0, 2, 4, 6, 8,
#
この例はそのまま [0,1,2,3,4].each{|i| … } とした時の動作と同じだけど、マスターデータが膨大になりうる時などは自分で yield するメソッドを実装するとコスト削減になってよい。
参考:
<http://www.ruby-lang.org/ja/man/?cmd=view;name=%A5%E1%A5%BD%A5%C3%A5
%C9%B8%C6%A4%D3%BD%D0%A4%B7#yield>
n++, ++n, n–, –n
こういうインクリメント/デクリメント式は Ruby には存在しない。
代わりに
n+=1, n-=1
等を使うしかないみたい。OOP 的にインクリメンタルな処理には Iterator や yield を使えってことでしょうかね。
参考:
<http://www.ruby-lang.org/ja/man/?cmd=view;name=%B1%E9%BB%BB%BB%D2%BC%B0>
def foo ( *args )
のように引数を一つだけ、最初に * をつけて定義すると、引数を配列(Array インスタンス)として扱える。
def main(* args)
print args.size, ” arguments: “
args.each { |i|
print i, “, “
}
print “\n”
end
main(1,2,3)
main(“foo”, 2)
main()
# result:
# 3 arguments: 1, 2, 3,
# 2 arguments: foo, 2,
# 0 arguments:
#
def foo
end
private :foo
def boo
end
protected :foo
def woo
end
public :woo
: とメソッド名前の間には空白をはさんではいけない。
ちなみに, private としたメソッドを子クラスで再定義すると, 上書きされてしまうので注意が必要。
class Parent
def foo(arg)
print “parent “, arg, “\n”
end
private :foo
def woo
foo(“hello”)
end
end
class Child < Parent
def foo
print “child\n”
end
end
Child.new.woo
# result:
# in `foo’: wrong number of arguments (1 for 0) (ArgumentError)
配列は [], 連想配列(ハッシュ)は {“arg1″=>”value1″, … } で定義できる。
foo = [1,2,3]
p foo[0]
boo = {“abc”=>1, “def”=>2}
p boo["abc"]
<http://www.ruby-lang.org/ja/man/?cmd=view;name=Array>
- Array クラス
<http://www.ruby-lang.org/ja/man/?cmd=view;name=Hash>
- Hash クラス
- initialize メソッドがコンストラクタになる。
class Foo
def initialize(arg)
print “Hello, ” + arg + “!”
end
end
Foo.new(“world”)
# result:
# Hello, world!
- 継承は “<"。シェルで使うような取り込むイメージ
class Parent
end
class Child < Parent
end
- 親クラスメソッド呼び出しはオーバーライドするメソッドで super とする。
class Parent
def foo(arg)
print “parent “, arg, “\n”
end
end
class Child < Parent
def foo(arg)
print “child\n”
super(arg)
end
end
Child.new.foo(“abc”)
#
# result:
# child
# parent abc
#
- メソッドのオーバーロードはできない
オーバーロードしようとすると最後の定義のみ有効になり、それ以外の呼び出しでは引数の数が違うというエラーが出る。
wrong number of arguments (1 for 0) (ArgumentError)
- 自己参照は this ではなく self
class A
attr_reader :name # 必須
def initialize
@name = “John”;
end
def foo
print self.name
end
end
A.new.foo
#
# result:
# John
self での参照はクラス外部からの参照と同等となるのでアクセス制限に注意!!
- 多重継承はモジュールに対してのみ可能
<http://www.ruby-lang.org/ja/man/?cmd=view;name=Module>
include で疑似的に実装する。
class Parent
end
module SubParent # module で宣言されていることに注意
def foo
end
end
module SubParent2 # module で宣言されていることに注意
include SubParent # module -> module の継承
def woo
end
end
class Child < Parent # class -> class の継承
include SubParent # module -> class の(多重)継承
include SubParent2 # module -> class の(多重)継承
end
ちゃんと SubParent, SubParent2 の子として認識されるのでまぁ安心して使える。
多重にするには親を module にしないとだめって事で制限があるので、 Java でいうところの interface に近い使い方になるのかな。
- 委譲 (delegate)
デリゲート用のライブラリがあった。
<http://www.ruby-lang.org/ja/man/?cmd=view;name=delegate.rb>
でもデリゲートしてるのに宣言時点で親クラスにならないとだめとか微妙なような..
include delegate(Array)
みたいな使い方ならいいのにな。
その1
attr_accessor :attr1, :attr2, :attr3# read/write
attr_reader :attr3, :attr4 # read-only
attr_writer :attr5 # write-only
: を使う宣言式共通の注意事項だが
attr_accessor: attr1
などとするとエラーになるので注意。
: は先頭の宣言にかかるのではなく各プロパティにかかるので、各プロパティごとに空白を挟まず先頭に : をつけないといけない。
@ をつけた同名のインスタンス変数に対してのアクセサとなる。
その2
# reader
def attr1
return @attr1
end
# writer
def attr1= (arg)
@attr1 = arg
end
任意の実装をしたい場合はこちらを使う。
毎回忘れるのでメモ。公式ドキュメント読みづらいっす。
参考:
<http://www.ruby-lang.org/ja/man/?cmd=view;name=FAQ%3A%3A%A5%E1%A5%BD
%A5%C3%A5%C9>
- Ruby 公式ドキュメント -> FAQ -> 5. メソッド
- ゲームを作っていると背景などで同じ画像をタイル状に張る処理をよくやるのでメソッド化してみた。なかなか便利。とりあえず DoJa 用だが、ImageObserver を渡してやる部分をくっつければすぐに AWT 用に移植できるはず。
/**
* イメージを指定範囲に敷き詰める.
*
* @see Graphics#drawImage(com.nttdocomo.ui.Image, int, int)
* @see Graphics#fillRect(int, int, int, int)
*
* @param g 敷き詰め先
* @param img 敷き詰める画像
* @param x 敷き詰め先左上 X 座標
* @param y 敷き詰め先左上 Y 座標
* @param w 敷き詰め先の幅
* @param h 敷き詰め先の高さ
* @author hiro.I
* @since 2005-10-20 18:12:31
*/
public static final void fillImage(Graphics g, Image img, int x, int y,
int w, int h) {
final boolean bHasHorizonalFraction = w % img.getWidth() > 0;
final boolean bHasVerticalFraction = h % img.getHeight() > 0;
final int COLS = w / img.getWidth() + (bHasHorizonalFraction ? 1 : 0);
final int ROWS = h / img.getHeight() + (bHasVerticalFraction ? 1 : 0);
for (int i = 0; i < ROWS; i++) {
final int ty = y + i * img.getHeight();
final int th = (i == ROWS – 1 && bHasVerticalFraction ? h
% img.getHeight() : img.getHeight());
for (int k = 0; k < COLS; k++) {
final int tx = x + k * img.getWidth();
final int tw = (k == COLS – 1 && bHasHorizonalFraction ? w
% img.getWidth() : img.getWidth());
g.drawImage(img, tx, ty, 0, 0, tw, th);
}
}}
- Boost
<http://boost.cppll.jp/HEAD/> – 日本語
<http://boost.org/> – 公式
あちこちで聞く名前だったのでいまさらにメモ。ドキュメントをざっと眺めるだけでも面白い。