File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ sections = [
2
+ {
3
+ " title" : " Getting started" ,
4
+ " reset_lesson_position" : false ,
5
+ " lessons" : [
6
+ {" name" : " Welcome" },
7
+ {" name" : " Installation" }
8
+ ]
9
+ },
10
+
11
+ {
12
+ " title" : " Basic operator" ,
13
+ " reset_lesson_position" : false ,
14
+ " lessons" : [
15
+ {" name" : " Addition / Subtraction" },
16
+ {" name" : " Multiplication / Division" }
17
+ ]
18
+ },
19
+
20
+ {
21
+ " title" : " Advanced topics" ,
22
+ " reset_lesson_position" : true ,
23
+ " lessons" : [
24
+ {" name" : " Mutability" },
25
+ {" name" : " Immutability" }
26
+ ]
27
+ }
28
+ ]
29
+
30
+ def indexing_lessons (lessons : Array )
31
+ lessons.map_with_index(1 ) { |lesson , index | lesson.merge({ position: index}) }
32
+ end
33
+
34
+ selected_lessons = sections.map { |section | section[:lessons ] }.flatten
35
+ indexed_lessons = indexing_lessons(selected_lessons)
36
+
37
+ formated_section = sections.map do |section |
38
+ formated_lessons = indexed_lessons.shift(section[:lessons ].size)
39
+ if section[:reset_lesson_position ]
40
+ formated_lessons = indexing_lessons(section[:lessons ])
41
+ end
42
+ section.merge(lessons: formated_lessons)
43
+ end
44
+
45
+ p formated_section
You can’t perform that action at this time.
0 commit comments