Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

slack #1

wants to merge 1 commit into from

Conversation

KTomoya-arch
Copy link
Owner

slack課題を実施しました

Comment on lines +19 to +36
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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

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

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 = {}のように書くことが多いです。

Copy link
Owner Author

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
# 配列をフラットな配列へ

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いアプローチですね。flat_mapというメソッドがあるので調べてみると良いです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この課題の中で一番躓いたところでした・・・w
とても便利なメソッドで逆引きでやりたいことピンポイントでできました。
了解です。

Comment on lines +29 to +31
channel_hash.store(":text",temp["text"])
channel_hash.store(":reaction_count",tmparray.sum)
# ハッシュを配列へ格納

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"]

でいけますね〜

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらも解答例を参考にしてもらえると!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

見やすいですね!
storeを書かないそのやり方の方がシンプルにわかりやすいです。
連想配列の扱いは経験不足もあって不慣れですがどんどん使っていきたいと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants