riで複数個
ri printと入力したら、たくさんあるよといわれてしまいました。
>ri print More than one method matched your request. You can refine your search by asking for information on one of: CGI#print, IO#print, IO#printf, Kernel#print, Kernel#printf, Kernel#sprintf, StringIO#print, StringIO#printf, Zlib::GzipWriter#print, Zlib::GzipWriter#printf
明示的に指定しないといけないのですね。
>ri Kernel#print ----------------------------------------------------------- Kernel#print print(obj, ...) => nil ------------------------------------------------------------------------ Prints each object in turn to +$stdout+. If the output field separator (+$,+) is not +nil+, its contents will appear between each field. If the output record separator (+$\+) is not +nil+, it will be appended to the output. If no arguments are given, prints +$_+. Objects that aren't strings will be converted by calling their +to_s+ method. print "cat", [1,2,3], 99, "\n" $, = ", " $\ = "\n" print "cat", [1,2,3], 99 _produces:_ cat12399 cat, 1, 2, 3, 99
はろーるびー
まあそんなに飛ばさないで。順当なところからいきましょう。
print "Hello, Ruby!\n";
デフォルト引数は明示的に評価される
デフォルト引数が評価されることをテスト。
def useDefault(n) print "useDefault: n=#{n}\n" 456 end def foo(x, y=useDefault(123)) print "x = #{x}, y = #{y}\n" end foo(1) foo(1, 2)
実行結果。
useDefault: n=123 x = 1, y = 456 x = 1, y = 2
メソッドfooの第二引数yに明示的に引数を与えたときにはuseDefault(123)は評価されない。
perldocに相当するものはriでしょうか
Perlではコマンドラインでリファレンスを引くのにperldocを使います。
Rubyではriがそれに相当するらしいですね。
>ri eval ------------------------------------------------------------ Kernel#eval eval(string [, binding [, filename [,lineno]]]) => obj ------------------------------------------------------------------------ Evaluates the Ruby expression(s) in _string_. If _binding_ is given, the evaluation is performed in its context. The binding may be a +Binding+ object or a +Proc+ object. If the optional _filename_ and _lineno_ parameters are present, they will be used when reporting syntax errors. def getBinding(str) return binding end str = "hello" eval "str + ' Fred'" #=> "hello Fred" eval "str + ' Fred'", getBinding("bye") #=> "bye Fred"
プログラムのテスト
print "Hello\n"