Skip to content

Instantly share code, notes, and snippets.

@khrtz
Last active August 29, 2015 14:22
Show Gist options
  • Save khrtz/c473b8bdc0ed65d178ab to your computer and use it in GitHub Desktop.
Save khrtz/c473b8bdc0ed65d178ab to your computer and use it in GitHub Desktop.
初めてのRuby 読書メモ

動く擬似コード

(2..100).each do |candidate| # 2から100までのそれぞれ(each)について、順に素数の候補(candidate)として扱う
sqrt = Math.sqrt(candidate) # 候補の平方根(square root)を取る
factor_found = (2..sqrt).any? {|i| candidate % i == 0} # foctor_found = 2から平方根までのいずれかのanyが、candidate % i == 0となる

if factor_found then
  print "#{candidate}は合成数\n"
else
  print "#{candidate}は素数\n"
end
end

*メタプログラミング

class Duration
  %w[ days hors minutes seconds ].each do |name|
    attr_axxessor name
  end
end

d = Duration.new
d.days = 3
d.hours = 5
p d.days #=> 3
  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment