Skip to content

Conversation

@kakusuke
Copy link

@kakusuke kakusuke commented Nov 7, 2013

配列のメソッドがまだイマイチうまく使いこなせません。。。

@Sixeight
Copy link
Member

Sixeight commented Nov 8, 2013

LGTM!
LGTM
👍 👎

Copy link
Member

Choose a reason for hiding this comment

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

hit_count の処理が2回実行されることになりますね。

Copy link
Member

Choose a reason for hiding this comment

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

メモイズすれば良さげです

Copy link
Member

Choose a reason for hiding this comment

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

メモイズって初めて聞きました。
そういう実装があるんですねー。勉強になります

Copy link
Author

Choose a reason for hiding this comment

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

メモイズするとgessメソッド中で初期化処理書かないといけない気がするので、
あまり処理速度気にする必要がなければあえてメモイズしないのもアリじゃないでしょうか?

Copy link
Member

Choose a reason for hiding this comment

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

現状 readable ですしね。
僕だと、↓ みたいな感じで書いちゃいますけど、readable じゃなくなるんですよね。。。

'+' * (h = hit_count) + '-' * (total_count - h)

Copy link
Author

Choose a reason for hiding this comment

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

なるほど、文字列の上書きができればそんな感じのやり方でもうちょっとうまく書ける気がしてきました

Copy link
Author

Choose a reason for hiding this comment

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

こんな感じに書けばメソッド呼び出し1回ずつでいけそうです

('+' * total_count).each_char.to_a.tap {|marks|
    marks.fill('-', hit_count..-1)
}.join

うーん、でもやっぱり読みにくいですね。。。

Copy link
Member

Choose a reason for hiding this comment

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

('+' * total_count).each_char.to_a

は、↓ で良さそうです。

total_count.times.map { '+' }

この方法も面白いですね。

Copy link
Author

Choose a reason for hiding this comment

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

なるほど、ちょっとすっきりしますね

total_count.times.map { '+' }.tap {|marks|
    marks.fill('-', hit_count..-1)
}.join

文字列を一旦配列にしてうんぬんが無駄な気がしますね
文字列の一部を直接指定した文字で埋められたら良さそうですが。。。

('+' * total_count).fill('-', hit_count..-1)

みたいなのが理想です

Copy link
Member

Choose a reason for hiding this comment

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

この初期化は不要です。インスタンスメソッド存在しなくてもnilが返るので hit_count の中でいきなり書けばOKです。

Copy link
Author

Choose a reason for hiding this comment

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

2回目gessるとバグりませんか?

ちょっとテスト書いてみます

Copy link
Member

Choose a reason for hiding this comment

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

おお、ほんとですね。すみません。
そうするとメモイズがだいぶダサいので最初のコードが美しいと思います。

Copy link
Author

Choose a reason for hiding this comment

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

ありがとうございます
戻しました

Copy link
Member

Choose a reason for hiding this comment

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

この実装面白いですね。なるほどー、って感じです。

Copy link
Author

Choose a reason for hiding this comment

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

頭ひねってこのやり方しか出てこなかったです
どう実装するのが自然でしょうかね。。。

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.

3 participants