Last active
March 9, 2024 18:44
-
-
Save orbanbotond/46ded4d84a6de0a2401b8be22d9bfb29 to your computer and use it in GitHub Desktop.
Data Driven vs Event Driven
This file contains 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
# Data Driven Code | |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | |
=> 0 | |
irb(main):002> boti_pontszama +1 | |
=> 1 | |
irb(main):003> gergo_pontszama = 0 | |
=> 0 | |
irb(main):004> gergo_pontszama += 1 | |
=> 1 | |
irb(main):005> boti_pontszama += 1 | |
=> 1 | |
irb(main):006> boti_pontszama | |
=> 1 | |
irb(main):007> boti_pontszama += 1 | |
=> 2 | |
irb(main):008> pontszamok = [boti_pontszama, gergo_pontszama] | |
=> [2, 1] | |
irb(main):009> pontszamok = {boti: boti_pontszama, gergo: gergo_pontszama} | |
=> {:boti=>2, :gergo=>1} | |
irb(main):010> gergo_pontszama += 1 | |
=> 2 | |
irb(main):011> pontszamok = {boti: boti_pontszama, gergo: gergo_pontszama} | |
=> {:boti=>2, :gergo=>2} | |
irb(main):012> boti_pontszama += 1 | |
=> 3 | |
irb(main):013> gergo_pontszama += 1 | |
=> 3 | |
irb(main):014> pontszamok | |
=> {:boti=>2, :gergo=>2} | |
irb(main):015> | |
# Event Driven Code | |
$score_board = { } | |
def reset() | |
$score_board = { } | |
end | |
def add_player(name:) | |
$score_board.merge!({"#{name}" => 0}) | |
end | |
def score(name:) | |
current_score = $score_board.fetch(name){0} | |
$score_board.merge!({"#{name}" => (current_score + 1)}) | |
end | |
def display_score | |
puts $score_board | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment