クラス
-
ARGF
. class (36) - Array (72)
- BasicObject (24)
- Binding (12)
- Enumerator (12)
- Exception (24)
- IO (60)
- Method (12)
- Module (168)
- Object (156)
- Random (36)
- String (72)
- Thread (48)
-
Thread
:: Backtrace :: Location (12) - TracePoint (12)
- UnboundMethod (24)
- UncaughtThrowError (22)
キーワード
- % (12)
-
add
_ trace _ func (12) -
alias
_ method (12) - ancestors (12)
- autoload (12)
- autoload? (12)
-
backtrace
_ locations (24) -
bind
_ call (12) - class (12)
-
class
_ eval (24) -
class
_ variables (12) -
const
_ defined? (12) - constants (12)
-
defined
_ class (12) - eval (12)
- exception (24)
- freeze (12)
- gets (36)
- hex (12)
- include (12)
- inspect (12)
-
instance
_ eval (24) -
instance
_ variables (12) - method (12)
-
module
_ eval (24) - next (12)
- oct (12)
- owner (24)
- print (12)
-
public
_ instance _ method (12) - putc (12)
- rand (36)
-
remove
_ const (12) - reopen (36)
- sample (48)
- send (24)
-
set
_ trace _ func (12) - shuffle! (24)
-
singleton
_ method (12) - tag (11)
-
to
_ a (12) -
to
_ ary (12) -
to
_ f (12) -
to
_ i (12) -
to
_ int (12) -
to
_ r (12) -
to
_ s (24) -
to
_ str (12) - value (11)
検索結果
先頭5件
-
String
# %(args) -> String (225.0) -
printf と同じ規則に従って args をフォーマットします。
...配列であれば Kernel.#sprintf(self, *args) と同じです。
それ以外の場合は Kernel.#sprintf(self, args) と同じです。
@param args フォーマットする値、もしくはその配列
@return フォーマットされた文字列
//emlist[例][ruby]{
p "i = %d" % 1......= 0xa"
p "i = %#o" % 10 # => "i = 012"
p "%d" % 10 # => "10"
p "%d,%o" % [10, 10] # => "10,12"
//}
=== sprintf フォーマット
Ruby の sprintf フォーマットは基本的に C 言語の sprintf(3)
のものと同じです。ただし、short や long などの C 特有の......, %B)が存在すること、sprintf のすべての方言をサ
ポートしていないこと(%': 3桁区切り)などの違いがあります。
Ruby には整数の大きさに上限がないので、%b, %B, %o, %x, %X
に負の数を与えると (左側に無限に1が続くとみなせるの... -
Module
# autoload(const _ name , feature) -> nil (49.0) -
定数 const_name を最初に参照した時に feature を Kernel.#require するように設定します。
...定数 const_name を最初に参照した時に feature を Kernel.#require するように設定します。
const_name が autoload 設定されていて、まだ定義されてない(ロードされていない)ときは、
autoload する対象を置き換えます。
const_name が(autoload......つまり、self の直下に定義された定数しか指定できません。
@param feature Kernel.#require と同様な方法で autoload する対象を指定する。
//emlist[例][ruby]{
# ------- /tmp/foo.rb ---------
class Foo
class Bar
end
end
# ----- end of /tmp/foo.rb ----
c......Foo::Bar #=> Foo::Bar
//}
以下のようにモジュールを明示的にレシーバとして呼び出すこともできます。
//emlist[例][ruby]{
# ------- /tmp/foo.rb ---------
class Foo
class Bar
end
end
# ----- end of /tmp/foo.rb ----
class Foo
end
Foo.autoload :Bar, '/tmp/foo'
p Fo... -
Binding
# eval(expr , fname = _ _ FILE _ _ , lineno = 1) -> object (47.0) -
自身をコンテキストとし文字列 expr を Ruby プログラムとして評価しその結果を返します。 組み込み関数 Kernel.#eval を使って eval(expr, self, fname, lineno) とするのと同じです。
...自身をコンテキストとし文字列 expr を
Ruby プログラムとして評価しその結果を返します。
組み込み関数 Kernel.#eval を使って
eval(expr, self, fname, lineno) とするのと同じです。
@param expr 評価したい式を文字列で与えます。
@param......の先頭行の行番号が lineno であるかのように実行されます。
//emlist[例][ruby]{
def get_binding(str)
binding
end
str = "hello"
p eval("str + ' Fred'") #=> "hello Fred"
p get_binding("bye").eval("str + ' Fred'") #=> "bye Fred"
//}
@see Kernel.#eval... -
String
# to _ f -> Float (45.0) -
文字列を 10 進数表現と解釈して、浮動小数点数 Float に変換します。
...象とします。
途中に変換できないような文字列がある場合、それより先の文字列は無視されます。
//emlist[][ruby]{
p "-10".to_f # => -10.0
p "10e2".to_f # => 1000.0
p "1e-2".to_f # => 0.01
p ".1".to_f # => 0.1
p "1_0_0".to_f # => 100.0 # 数値リテラ......数とみなせるものがないため、0.0 を返します。
変換対象が空文字列のケースでも、0.0 を返します。
//emlist[][ruby]{
p "".to_f # => 0.0
p "nan".to_f # => 0.0
p "INF".to_f # => 0.0
p "-Inf".to_f # => 0.0
//}
変換後の Float が有限の値を取れ......と、警告が表示されます。
//emlist[][ruby]{
#!ruby -W2
p ("10" * 1000).to_f # => Infinity
# warning: Float 10101010101010101010... out of range
//}
なお、このメソッドとは逆に、数値を文字列に変換するには
Kernel.#sprintf, String#%, Integer#to_s
を使用し... -
String
# to _ i(base = 10) -> Integer (45.0) -
文字列を 10 進数表現された整数であると解釈して、整数に変換します。
...ruby]{
p " 10".to_i # => 10
p "+10".to_i # => 10
p "-10".to_i # => -10
p "010".to_i # => 10
p "-010".to_i # => -10
//}
整数とみなせない文字があればそこまでを変換対象とします。
変換対象が空文字列であれば 0 を返します。
//emlist[例][ruby......".to_i # => 0
//}
基数を指定することでデフォルトの 10 進以外に 2 〜 36 進数表現へ変換できます。
それぞれ Ruby の整数リテラルで使用可能なプリフィクスは無視されます。
また、base に 0 を指定するとプリフィクスから......0 進数)、0x (16 進数) です。
0, 2 〜 36 以外の引数を指定した場合は
例外 ArgumentError が発生します。
//emlist[例][ruby]{
p "01".to_i(2) # => 1
p "0b1".to_i(2) # => 1
p "07".to_i(8) # => 7
p "0o7".to_i(8) # => 7
p "1f".to_i(16) # => 31
p "0x1f".to_i(16)... -
String
# oct -> Integer (39.0) -
文字列を 8 進文字列であると解釈して、整数に変換します。
...て、整数に変換します。
//emlist[例][ruby]{
p "10".oct # => 8
p "010".oct # => 8
p "8".oct # => 0
//}
oct は文字列の接頭辞 ("0", "0b", "0B", "0x", "0X") に応じて
8 進以外の変換も行います。
//emlist[例][ruby]{
p "0b10".oct # => 2
p "10".oct # => 8
p "010"......ます。
//emlist[例][ruby]{
p "-010".oct # => -8
p "-0x10".oct # => -16
p "-0b10".oct # => -2
p "1_0_1x".oct # => 65
//}
@see String#hex, String#to_i, String#to_f,
Kernel.#Integer, Kernel.#Float
逆に、数値を文字列に変換するにはKernel.#sprintf,
String#%, Intege... -
String
# to _ r -> Rational (39.0) -
自身を有理数(Rational)に変換した結果を返します。
...自身を有理数(Rational)に変換した結果を返します。
Kernel.#Rational に文字列を指定した時のように、以下のいずれかの形
式で指定します。
* "1/3" のような分数の形式
* "0.3" のような10進数の形式
* "0.3E0" のような x.xEn の形......スコアで繋いだ形式
//emlist[例][ruby]{
' 2 '.to_r # => (2/1)
'1/3'.to_r # => (1/3)
'-9.2'.to_r # => (-46/5)
'-9.2E2'.to_r # => (-920/1)
'1_234_567'.to_r # => (1234567/1)
'1_234/5_678'.to_r # => (617/2839)
//}
Kernel.#Rational に文字列を指定した時......文字列は
無視されます。
//emlist[][ruby]{
'21 june 09'.to_r # => (21/1)
'21/06/09'.to_r # => (7/2) # 21/6 を約分して 7/2。
//}
変換できないような文字列を指定した場合は 0/1 を返します。
//emlist[][ruby]{
'foo'.to_r # => (0/1)
''.to_r... -
TracePoint
# defined _ class -> Class | module (39.0) -
メソッドを定義したクラスかモジュールを返します。
...emlist[例][ruby]{
class C; def foo; end; end
trace = TracePoint.new(:call) do |tp|
p tp.defined_class # => C
end.enable do
C.new.foo
end
//}
メソッドがモジュールで定義されていた場合も(include に関係なく)モジュー
ルを返します。
//emlist[例][ruby]{
module M......Kernel.#set_trace_func の 6 番目のブロックパ
ラメータは特異クラスではなく元のクラスを返します。
//emlist[例][ruby]{
class C; def self.foo; end; end
trace = TracePoint.new(:call) do |tp|
p tp.defined_class # => #<Class:C>
end.enable do
C.foo
end
//}
Kernel.#se... -
ARGF
. class # gets(limit) -> String | nil (33.0) -
ARGFの現在位置から一行ずつ文字列として読み込みます。EOF に到達した時に は nil を返します。
...# $ ruby test.rb test.txt
# test.rb
ARGF.gets # => "line1\n"
例:
# $ echo "line1\nline2\nline3\n\nline4\n" > test.txt
# $ ruby test.rb test.txt
# test.rb
ARGF.gets(2) # => "li"
例:
# $ echo "line1\nline2\nline3\n\nline4\n" > test.txt
# $ ruby t......est.txt
# test.rb
ARGF.gets("e") # => "line"
例:
# $ echo "line1\nline2\nline3\n\nline4\n" > test.txt
# $ ruby test.rb test.txt
# test.rb
ARGF.gets("") # => "line1\nline2\nline3\n\n"
@see Kernel.#gets, IO#gets, ARGF.class#getbyte, ARGF.class#getc... -
ARGF
. class # gets(limit , chomp: false) -> String | nil (33.0) -
ARGFの現在位置から一行ずつ文字列として読み込みます。EOF に到達した時に は nil を返します。
...# $ ruby test.rb test.txt
# test.rb
ARGF.gets # => "line1\n"
例:
# $ echo "line1\nline2\nline3\n\nline4\n" > test.txt
# $ ruby test.rb test.txt
# test.rb
ARGF.gets(2) # => "li"
例:
# $ echo "line1\nline2\nline3\n\nline4\n" > test.txt
# $ ruby t......est.txt
# test.rb
ARGF.gets("e") # => "line"
例:
# $ echo "line1\nline2\nline3\n\nline4\n" > test.txt
# $ ruby test.rb test.txt
# test.rb
ARGF.gets("") # => "line1\nline2\nline3\n\n"
@see Kernel.#gets, IO#gets, ARGF.class#getbyte, ARGF.class#getc...