種類
- インスタンスメソッド (121)
- 特異メソッド (22)
- モジュール関数 (11)
- ライブラリ (11)
ライブラリ
- ビルトイン (121)
- bigdecimal (11)
-
irb
/ xmp (22)
モジュール
- BigMath (11)
- Enumerable (44)
- Kernel (11)
キーワード
- === (11)
- =~ (11)
- bigdecimal (11)
-
last
_ match (22) - match (22)
-
max
_ by (44) - xmp (11)
- ~ (11)
検索結果
先頭5件
-
XMP
# puts(exps) -> nil (18301.0) -
引数 exps で指定されたRuby のソースコードとその実行結果を、標準出力に行 ごとに交互に表示します。
...引数 exps で指定されたRuby のソースコードとその実行結果を、標準出力に行
ごとに交互に表示します。
@param exps 評価するRuby のソースコードを文字列で指定します。... -
BigMath
. # exp(x , prec) -> BigDecimal (18213.0) -
x の指数関数を prec で指定した精度で計算します。
...象の数値を Integer、BigDecimal、
Float、Rationalオブジェクトのいずれかで指定します。
@param prec 計算結果の精度を指定します。
@raise ArgumentError x に Integer、BigDecimal、
Float、Rational以外のオブジェクトを指......定した場合に発生します。
@raise ArgumentError prec に 0 以下の数値が指定された場合に発生します。
//emlist[][ruby]{
require "bigdecimal/math"
puts BigMath::exp(BigDecimal('1'), 10) #=> 0.2718281828e1
//}... -
Regexp
. last _ match(nth) -> String | nil (9106.0) -
整数 nth が 0 の場合、マッチした文字列を返します ($&)。それ以外では、nth 番目の括弧にマッチ した部分文字列を返します($1,$2,...)。 対応する括弧がない場合やマッチしなかった場合には nil を返し ます。
...かった場合には nil を返し
ます。
//emlist[例][ruby]{
/(.)(.)/ =~ "ab"
p Regexp.last_match # => #<MatchData:0x4599e58>
p Regexp.last_match(0) # => "ab"
p Regexp.last_match(1) # => "a"
p Regexp.last_match(2) # => "b"
p Regexp.last_match(3) # => nil
//}
正規表現全体......の
Regexp.last_match はnil を返すため、
last_match[1] の形式では例外 NoMethodError が発生します。
対して、last_match(1) は nil を返します。
//emlist[例][ruby]{
str = "This is Regexp"
/That is Regexp/ =~ str
p Regexp.last_match # => nil
begin
p Regexp.last_match[1......] # 例外が発生する
rescue
puts $! # => undefined method `[]' for nil:NilClass
end
p Regexp.last_match(1) # => nil
//}
@param nth 整数を指定します。
整数 nth が 0 の場合、マッチした文字列を返します。それ以外では、nth 番目の括弧にマッチした... -
Regexp
. last _ match -> MatchData (9101.0) -
カレントスコープで最後に行った正規表現マッチの MatchData オ ブジェクトを返します。このメソッドの呼び出しは $~ の参照と同じです。
...のメソッドの呼び出しは $~
の参照と同じです。
//emlist[例][ruby]{
/(.)(.)/ =~ "ab"
p Regexp.last_match # => #<MatchData:0x4599e58>
p Regexp.last_match[0] # => "ab"
p Regexp.last_match[1] # => "a"
p Regexp.last_match[2] # => "b"
p Regexp.last_match[3] # => nil
//}... -
bigdecimal (6048.0)
-
bigdecimal は浮動小数点数演算ライブラリです。 任意の精度で 10 進表現された浮動小数点数を扱えます。
...bigdecimal は浮動小数点数演算ライブラリです。
任意の精度で 10 進表現された浮動小数点数を扱えます。
//emlist[][ruby]{
require 'bigdecimal'
a = BigDecimal("0.123456789123456789")
b = BigDecimal("123456.78912345678", 40)
print a + b # => 0.123456912580245903456......// -3 : -Infinity
unsigned short flag; // 各種の制御フラッグ
int exponent; // 指数部の値(仮数部*BASE**exponent)
unsigned long frac[1]; // 仮数部の配列(可変)
} Real;
例えば BASE=10000 のとき 1234.56784321 という数......イル file の合計数値を求める例です。
//emlist[digits.txt][ruby]{
0.1
0.1
0.1
//}
//emlist[][ruby]{
require "bigdecimal"
File::open("digits.txt", "r") do |file|
s = BigDecimal("0")
while line = file.gets
s = s + BigDecimal(line)
end
puts s # => 0.3e0
end
File::open("dig... -
Kernel
# xmp(exps , bind = nil) -> XMP (3206.0) -
引数 exps で指定されたRuby のソースコードとその実行結果を、標準出力に行 ごとに交互に表示します。
...引数 exps で指定されたRuby のソースコードとその実行結果を、標準出力に行
ごとに交互に表示します。
@param exps 評価するRuby のソースコードを文字列で指定します。
@param bind Binding オブジェクトを指定します。省略した場......合は、最
後に実行した XMP#puts、Kernel#xmp の
Binding を使用します。まだ何も実行していない場合は
Object::TOPLEVEL_BINDING を使用します。... -
Regexp
# ~ -> Integer | nil (3136.0) -
変数 $_ の値との間でのマッチをとります。
...です。
//emlist[][ruby]{
self =~ $_
//}
//emlist[例][ruby]{
$_ = "hogehoge"
if /foo/
puts "match"
else
puts "no match"
end
# => no match
# ただし、警告がでる。warning: regex literal in condition
reg = Regexp.compile("foo")
if ~ reg
puts "match"
else
puts "no match"
end......# => no match
if reg
puts "match"
else
puts "no match"
end
# => match
# reg は nil でも false でも無いので常にtrue
//}... -
Regexp
# ===(string) -> bool (3118.0) -
文字列 string との正規表現マッチを行います。 マッチした場合は真を返します。
...合には false を返します。
このメソッドは主にcase文での比較に用いられます。
@param string マッチ対象文字列
//emlist[例][ruby]{
a = "HELLO"
case a
when /\A[a-z]*\z/; puts "Lower case"
when /\A[A-Z]*\z/; puts "Upper case"
else; puts "Mixed case"
e......nd
# => Upper case
/\A[a-z]*\z/ === "HELLO" # => false
/\A[A-Z]*\z/ === "HELLO" # => true
//}
@see Enumerable#grep, Object#===... -
Enumerable
# max _ by {|item| . . . } -> object | nil (3112.0) -
各要素を順番にブロックに渡して実行し、 その評価結果を <=> で比較して、 最大であった値に対応する元の要素、もしくは最大の n 要素が降順で入った配列を返します。
...しなければ nil を返します。
引数を指定する形式では、空の配列を返します。
該当する要素が複数存在する場合、どの要素を返すかは不定です。
Enumerable#max と Enumerable#max_by の
違いは Enumerable#sort と Enumerable#sort_by の違いと......Pavlos S. Efraimidis, Paul G. Spirakis
# Weighted random sampling with a reservoir
# Information Processing Letters
# Volume 97, Issue 5 (16 March 2006)
def wsample(n)
self.max_by(n) {|v| rand ** (1.0/yield(v)) }
end
end
e = (-20..20).to_a*10000
a = e.wsample(20000) {|x|
Math.exp(-(......x/5.0)**2) # normal distribution
}
# a is 20000 samples from e.
p a.length #=> 20000
h = a.group_by {|x| x }
-10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] }
#=> *
# ***
# ******
# ***********
# ******************
# *****************************
# **************************... -
Regexp
# =~(string) -> Integer | nil (3106.0) -
文字列 string との正規表現マッチを行います。マッチした場合、 マッチした位置のインデックスを返します(先頭は0)。マッチしなかった 場合、あるいは string が nil の場合には nil を返 します。
...nil の場合には nil を返
します。
//emlist[例][ruby]{
p /foo/ =~ "foo" # => 0
p /foo/ =~ "afoo" # => 1
p /foo/ =~ "bar" # => nil
//}
組み込み変数 $~ もしくは Regexp.last_match にマッチに関する情報 MatchData が設定されます。
文字列のかわりにSymbol......nil でも String オブジェクト
でも Symbol でもない場合発生します。
//emlist[例][ruby]{
p /foo/ =~ "foo" # => 0
p Regexp.last_match(0) # => "foo"
p /foo/ =~ "afoo" # => 1
p $~[0] # => "foo"
p /foo/ =~ "bar" # => nil
unless....../foo/ === "bar"
puts "not match " # => not match
end
str = []
begin
/ugo/ =~ str
rescue TypeError
printf "! %s\t%s\n", $!, $@ # => ! can't convert Array into String r5.rb:15
end
//}...