0
0

More than 3 years have passed since last update.

ド忘れしていたRubyの基礎5選④

Posted at

どうも、三町哲平です。

ド忘れしていたRubyの基礎5選の第4弾です。

今回は、コチラの問題を解く際を想定
Reverse Integer - LeetCode

解き方は、コチラを参照
Ruby Solution - LeetCode Discuss

1. 後置if

# 普通のif文
if foo
  bar
end

# 後置ifを使ったif文
bar if foo

可読性の観点からも後置ifの多様には注意が必要です。

引用及び参考記事:【Ruby】乱用厳禁!?後置ifで書くとかえって読みづらくなるケース - Qiita

2. ** 四則演算

p 5**3
# => "125"

べき乗されます。
※上記の場合、5の3乗

引用及び参考記事:四則演算 - 数値と四則演算 - Ruby入門

3. ppライブラリ

オブジェクトなどを見やすく出力するためのライブラリです。

pによるpretty-printされてない出力
#<PP:0x81a0d10 @stack=[], @genspace=#<Proc:0x81a0cc0>, @nest=[0], @newline="\n",
 @buf=#<PrettyPrint::Group:0x81a0c98 @group=0, @tail=0, @buf=[#<PrettyPrint::Gro
up:0x81a0ba8 @group=1, @tail=0, @buf=[#<PrettyPrint::Text:0x81a0b30 @tail=2, @wi
dth=1, @text="[">, #<PrettyPrint::Group:0x81a0a68 @group=2, @tail=1, @buf=[#<Pre
ttyPrint::Text:0x81a09f0 @tail=1, @width=1, @text="1">], @singleline_width=1>, #
<PrettyPrint::Text:0x81a0a7c @tail=0, @width=1, @text=",">, #<PrettyPrint::Break
able:0x81a0a2c @group=2, @gensace=#<Proc:0x81a0cc0>, @newline="\n", @indent=1, @
tail=2, @sep=" ", @width=1>, #<PrettyPrint::Group:0x81a09c8 @group=2, @tail=1, @
buf=[#<PrettyPrint::Text:0x81a0950 @tail=1, @width=1, @text="2">], @singleline_w
idth=1>, #<PrettyPrint::Text:0x81a0af4 @tail=0, @width=1, @text="]">], @singleli
ne_width=6>], @singleline_width=6>, @sharing_detection=false>
ppによるpretty-printされた出力
#<PP:0x40d0688
 @buf=
  #<PrettyPrint::Group:0x40d064c
   @buf=
    [#<PrettyPrint::Group:0x40d05d4
      @buf=
       [#<PrettyPrint::Text:0x40d0598 @tail=2, @text="[", @width=1>,
        #<PrettyPrint::Group:0x40d0534
         @buf=[#<PrettyPrint::Text:0x40d04f8 @tail=1, @text="1", @width=1>],
         @group=2,
         @singleline_width=1,
         @tail=1>,
        #<PrettyPrint::Text:0x40d053e @tail=0, @text=",", @width=1>,
        #<PrettyPrint::Breakable:0x40d0516
         @genspace=#<Proc:0x40d0656>,
         @group=2,
         @indent=1,
         @newline="\n",
         @sep=" ",
         @tail=2,
         @width=1>,
        #<PrettyPrint::Group:0x40d04e4
         @buf=[#<PrettyPrint::Text:0x40d04a8 @tail=1, @text="2", @width=1>],
         @group=2,
         @singleline_width=1,
         @tail=1>,
        #<PrettyPrint::Text:0x40d057a @tail=0, @text="]", @width=1>],
      @group=1,
      @singleline_width=6,
      @tail=0>],
   @group=0,
   @singleline_width=6,
   @tail=0>,
 @genspace=#<Proc:0x40d0656>,
 @nest=[0],
 @newline="\n",
 @sharing_detection=false,
 @stack=[]>

引用及び参考記事:library pp (Ruby 3.0.0 リファレンスマニュアル)
引用及び参考記事:【ruby】p pp puts print 違い。 - Qiita

4. * 可変長引数

*をつければ引数を、配列に指定できます。

#複数の引数を配列にする
def array(*a)
  pp a
end

> array(1,2)
#=> [1, 2] 
> array(1, 2, 3)
#=> [1, 2, 3]

引用及び参考記事:メソッドの引数にアスタリスク - Qiita

5. ** オプション引数

**をつければ引数を、ハッシュに指定できます。

#複数の引数をハッシュにする
def array(**a)
  pp a
end

> array(b: 1, c: 2)
#=> {:b=>1, :c=>2} 
> array(b: 1, c: 2, d: 3)
#=> {:b=>1, :c=>2, :d=>3}

引用及び参考記事:メソッドの引数にアスタリスク - Qiita

0
0
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
0
0