Skip to content

Instantly share code, notes, and snippets.

@844196
Created May 18, 2015 03:06
Show Gist options
  • Save 844196/8e1fbc4ae65bde5b05dd to your computer and use it in GitHub Desktop.
Save 844196/8e1fbc4ae65bde5b05dd to your computer and use it in GitHub Desktop.
短く書けた
if options[:n].nil?
renge.say(rand(renge.quotes.length))
else
renge.say(options[:n].to_i)
end
renge.say(options[:n] || rand(renge.quotes.length))
@844196
Copy link
Author

844196 commented May 18, 2015

-n INTを受け取る段階で.to_iした

opt.on('-n', '--number INT', 'Specify quote number.') {|v| options[:n] = v.to_i }

-nが指定されない場合はoptions[:n]nilなのでこれを持って条件判定した

falseもしくはnilの場合は偽となる

@844196
Copy link
Author

844196 commented May 18, 2015

こうも書けるが無駄に変数を宣言したくなかった

if options[:n]
    n = rand(renge.quotes.length)
else
    n = options[:n]
end
renge.say(n)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment