-
-
Save svoboda-jan/1a6e6ac273cdc7988aae664c1e3bf756 to your computer and use it in GitHub Desktop.
# Dice Betting Game, https://opalrb.com/try/ | |
bank = 100; | |
while bank > 0 do | |
bet = `prompt("Place your bet")`.to_i | |
dice1 = (rand * 6).ceil | |
dice2 = (rand * 6).ceil | |
outcome = dice1 == dice2 | |
bank += bet if outcome | |
bank -= bet unless outcome | |
puts "Numbers are: #{dice1} #{dice2}" | |
puts outcome_txt = outcome ? "You Win" : "You Lose" | |
outcome_txt += ", bank is now #{bank}" | |
puts "Bank is now #{bank}" | |
`alert(outcome_txt)` | |
end |
My idea was that... this syntax is slowly going to be deprecated. Already it drops a warning and asks to add a magic comment.
This may not work in future major versions of Opal (I mean, it will, but eg. String may need special conversion). Therefore I would suggest that this syntax is only presented to advanced users.
Created a new one using native:
https://gist.github.com/svoboda-jan/cc4d675bac8a966038c8aeb0a495b3ad
The motivation behind these scripts is to create an easy way to start learning ruby without having to install it.
So in the betting game a task may be:
- disallow betting to negative numbers
- warn the user and ask if he wants co continue the game with certain bank treshold
In the calendar it might be:
- to add support for leap years
- to highlight holidays
Another script I've created is to display Czech name days, using Base64 encoded data at the end of the file:
https://gist.github.com/svoboda-jan/25144fb6b734893dc4742ac52b54b869
Here the task could be to combine these scripts in some way, like displaying the name day in the calendar, think that wold be a good task to teach refactoring, because both scripts work with date.
Is there a standalone example? E. g. can I just put the ruby code into a .html file too, or do
I need to use "opal --compile"?
Would be nice to add it to the official examples, if it works. Right now I am not sure how to
get it to work (I am an opal-noob at this point in time).
This is intentional, to demonstrate another way to call JS since an example using native is already in the examples.