repl.it
@anonymous/

SilverCalculatingSorting

Ruby

No description

fork
loading
Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Object
    def self.all
        ObjectSpace.each_object(self).to_a
    end
end

class Man
  attr_accessor :mortal, :name
  def initialize(name)
    @name = name # IMPLICITLY: All men have a name
    @mortal = true # EXPLICITLY: All men are mortal
  end
end

# Lets create some men

Man.new('TimeSeeker')
Man.new('jacobbrownacro')
Man.new('Socrates')

puts "How many men are there?"
puts Man.all.count

puts "Are all men mortal?"
puts Man.all.find { |man| not man.mortal }.nil? # Look for falsifier. Any immortal man means the answer is 'No'

puts 'Is Socrates mortal?'
puts Man.all.find { |man| man.name == "Socrates" }.mortal

# Now lets do something sly. Because we are God - we can make Socrates immortal!
puts "And God said: Let Socrates be immortal! **waves magic wand**"
Man.all.find { |man| man.name == "Socrates" }.mortal=false

puts "Are all men mortal?"
puts Man.all.find { |man| not man.mortal }.nil? 

puts 'Is Socrates mortal?'
puts Man.all.select { |man| man.name == "Socrates" }.find { |man| not man.mortal }.nil?

puts "Are all men NOT named Socrates mortal?"
puts Man.all.select { |man| man.name != "Socrates" }.find { |man| not man.mortal }.nil?
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]