This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/.gitignore b/.gitignore | |
index 2dfdf96..79d884c 100644 | |
--- a/.gitignore | |
+++ b/.gitignore | |
@@ -19,3 +19,5 @@ railties/test/initializer/root/log | |
railties/doc | |
railties/guides/output | |
railties/tmp | |
+.DS_Store | |
+nbproject |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fact(n) | |
return 1 if (0..1).include?(n) | |
n * fact(n - 1) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fact(6) = 6 * fact(5) | |
= 6 * 5 * fact(4) | |
= 6 * 5 * 4 * fact(3) | |
= 6 * 5 * 4 * 3 * fact(2) | |
= 6 * 5 * 4 * 3 * 2 * fact(1) | |
= 6 * 5 * 4 * 3 * 2 * 1 | |
= 720 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fact(n) | |
factorial = 1 | |
begin | |
factorial *= n-- | |
end while n > 1 | |
factorial | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts = [1, 2, 3].each.class # => Enumerator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = [1, 2, 3] | |
enum = x.each | |
puts enum.class # => Enumerator | |
puts enum.next # => 1 | |
puts enum.next # => 2 | |
puts enum.next # => 3 | |
puts enum.next # => StopIteration exception |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fact(n) | |
(1..n).inject(:*) || 1 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prime.lazy.select { |x| x.to_s.include?('3') }.take(20).to_a | |
Prime.lazy.select { |x| 100.to_s.chars.map(&:to_i).inject(:+) }.take(20).to_a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [] | |
Prime.each do |x| | |
next unless x.to_s.include?('3') | |
a << x | |
break if a.size == 20 | |
end |