LoginSignup
2
2

More than 5 years have passed since last update.

Yokohama.rb #38 メモ

Last updated at Posted at 2013-11-16

Yokohama.rb で話題になったことと、別に話題にはなってないけど勝手にやってたこと。

inject に :+

[1,2,3].inject(:+) => 6

inject だけは &:+ ではなく :+ でよいらしい。

each_with_index と each.with_index

%w( a b c ).each_with_index(2).to_a #=> ArgumentError
%w( a b c ).each.with_index(2).to_a #=> [["a", 2], ["b", 3], ["c", 4]]

each_with_index には初期値が入れられないけど、with_index には初期値を入れられる。

Ratinalの発音

「ラショナル」のような感じ。「レイショナル」だと思ってた。とほほ。

remainder

10.remainder( -3 ) # => 1
(-10).remainder( -3 ) # => -1
(-10).remainder( 3 ) # => -1
(-10) % 3 #=> 2

常に正の剰余を返すのが便利なので、普通は remainder ではなく % を使えばいいのか。

quo

10.quo 2 #=> (5/1)
10.0.quo 2 # => 5.0
Complex(12,3.4).quo(56) # => ((3/14)+0.060714285714285714i)

Float の quo が役に立つ場面が思いつかない。
Complex については、まあそうするしかないと思うけど、役に立つ場面が思いつかない。

単項マイナス演算子

x=2
-x.abs #=> -2 ( - は単項演算子 )
-2.abs #=> 2 ( - はリテラルの一部 )
- 2.abs #=> -2 ( マイナスのあとに空白があると、単項演算子 )
-0x2.abs #=> 2 ( 16進数リテラルでも - はリテラルの一部 )

-x.abs-(x.abs) だけど、 -2.abs(-2).abs になるという罠。

Symbol にも succ

:a.succ # => :b
:x.succ # => :y
:z.succ # => :aa

役に立つ場面はあまりないと思う。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2