0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails6】mapメソッドが少しわかる【チュートリアル4.3.2 演習問題2解説】

Posted at

###はじめに

yeller([’o’, ’l’, ’d’])と実行したとき、”OLD”を返すメソッドyallerを定義しなさいという演習の回答例とメソッドの流れを書いてみました

###回答例

irb(main):025:0> def yaller(hoge)
irb(main):026:1>   oohoge = hoge.map{|v| v.upcase}.join
irb(main):027:1> end
=> :yaller
irb(main):028:0> yaller(['o','l','d'])
=> "OLD"

1.hogeに配列['o','l','d']を渡す
2.mapupcasehogeの中身を一文字ずつ大文字に変えながら配列をoohoge = ['O','L','D']
3.joinでつなげて=> OLD
という順番です。

###map無しバージョン
ヒントでmapupcasejoinを使うといいですよ〜と書いてましたが、mapなしでも可能みたいです

irb(main):025:0> def yaller(hoge)
irb(main):026:1>   oohoge = hoge.join.upcase
irb(main):027:1> end
=> :yaller
irb(main):028:0> yaller(['o','l','d'])
=> "OLD"

1.hogeに配列['o','l','d']を渡す
2.['o','l','d']joinで繋げて返す"old"
3.upcaseで大文字にする=> "OLD"

以上です〜

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?