New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
slack #1
base: master
Are you sure you want to change the base?
slack #1
Conversation
temp = jsonelement["messages"] | ||
channel_hash = Hash.new | ||
# typeがmessageのものを抽出しカウントを加算 | ||
temp.each do |tempdiv| | ||
if tempdiv["type"] == "message" | ||
message_count += 1 | ||
end | ||
end | ||
channel_hash.store(":channl_name",element) | ||
channel_hash.store(":message_count",message_count) | ||
channel_array.push(channel_hash) | ||
message_count = 0 | ||
end | ||
# カウント数の多い順にソートする | ||
result_array = channel_array.sort do |a,b| | ||
b[":message_count"] <=> a[":message_count"] | ||
end | ||
puts result_array[0..2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
解答例のようにmap
を使えばもっとスッキリ書けますね。
Ruby: eachよりもmapなどのコレクションを積極的に使おう(社内勉強会)|TechRacho(テックラッチョ)〜エンジニアの「?」を「!」に〜|BPS株式会社
その他の参考サイト
https://qiita.com/jnchito/items/dedb3b889ab226933ccf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
便利なメソッドがあるのですね!
安直にeachなどを使用せずに書けないかと考えるのは確かに大事ですね、どうしても冗長的になりがちです。
手続き的に書いてあることがメソッドに渡すことで処理できるのじゃないかという視点は常に持っておきたいと思いました。
|
||
# 実装してください | ||
def exec | ||
channel_array = Array.new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rubyではあまりArray.new
とかHash.new
のようにnew
は使わないですね。
channel_array = []
とかchannel_hash = {}
のように書くことが多いです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
了解しました!
今後書き方変えれるように心がけます。
last_array.push(dvdtmp["users"]) | ||
end | ||
end | ||
# 配列をフラットな配列へ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いアプローチですね。flat_map
というメソッドがあるので調べてみると良いです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この課題の中で一番躓いたところでした・・・w
とても便利なメソッドで逆引きでやりたいことピンポイントでできました。
了解です。
channel_hash.store(":text",temp["text"]) | ||
channel_hash.store(":reaction_count",tmparray.sum) | ||
# ハッシュを配列へ格納 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
store
なんてメソッドがあるんですね笑 知らなかった笑
channel_hash[:text] = temp["text"]
でいけますね〜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらも解答例を参考にしてもらえると!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
見やすいですね!
storeを書かないそのやり方の方がシンプルにわかりやすいです。
連想配列の扱いは経験不足もあって不慣れですがどんどん使っていきたいと思います。
slack課題を実施しました