Project

General

Profile

Bug #12453 ยป example5.rb

HarlinSeritt (Harlin Seritt), 06/02/2016 04:02 AM

 
1
#!/usr/bin/env ruby2
2

    
3
=begin
4
This is a multiline comment
5
=end
6

    
7
"
8
Just like Python multiline comments
9
"
10

    
11
require 'set'
12

    
13
def display_names name_list
14
  puts '****************************'
15
  name_list.each do |name|
16
      puts name
17
  end
18
  puts "\n"
19
end
20

    
21
names = ['Bob', 'Jack', 'Jill', 'Mike', 'John', 'Jim', 'Lisa']
22

    
23
name_list = []
24

    
25
names.each do |name|
26
  name_list.push name
27
end
28

    
29
display_names name_list
30

    
31
more_names = ['Mark', 'Matt', 'Luke', 'Juan', 'Jose']
32
name_list.concat more_names
33
display_names name_list
34

    
35
puts 'Put Alfa at the front of the line ...'
36
name_list.insert 0, 'Alfa'
37
display_names name_list
38

    
39
puts 'Remove Bob from the list ...'
40
name_list.delete 'Bob'
41
display_names name_list
42

    
43
puts 'Remove the first and last ...'
44
name_list.delete_at 0
45
name_list.pop
46
display_names name_list
47

    
48
puts 'Clear the whole list ...'
49
name_list.clear
50
display_names name_list
51

    
52
puts 'Fill it with names again ...'
53
name_list = Array.new names
54
display_names name_list
55

    
56
puts 'Get the index of "Mike" ...'
57
puts name_list.index 'Mike'
58

    
59
puts 'Get the count of "Mike" in name_list ...'
60
puts name_list.count 'Mike'
61

    
62
puts 'Sort name_list by alphabetical order ...'
63
name_list.sort
64
display_names name_list
65

    
66
puts "Now let's reverse them ..."
67
name_list.reverse
68
display_names name_list
69

    
70
puts 'Slice the 2nd through 4th values ...'
71
display_names name_list.slice 1, 4
72

    
73
puts 'Inserting Charles at the 3rd position ...'
74
name_list.insert 2, 'Charles'
75
display_names name_list
76

    
77
puts 'Now removing Charles ...'
78
name_list.delete_at 2
79
display_names name_list
80

    
81
puts "Let's switch to numbers ..."
82
num_list = [5, 7, 9, 2, 4, 6]
83
display_names num_list
84

    
85
puts "Let's double them ..."
86
doubles = num_list.map{|i| i * 2}
87
display_names doubles
88

    
89
puts "Ok, now to sets ..."
90
fruits = Set.new ['banana', 'orange', 'apple', 'apple', 'grapes']
91
display_names fruits