Skip to content

Instantly share code, notes, and snippets.

@svoboda-jan
svoboda-jan / dice_betting_game.rb
Created November 3, 2024 19:50
A dice betting game using Opal Ruby to Javascript source-to-source compiler
# 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}"
@svoboda-jan
svoboda-jan / unix_timestamp.rb
Created December 20, 2024 02:52
Generate an Unix timestamp in Ruby
# Generate an Unix timestamp in Ruby
# If you don't want to use:
Time.now.strftime("%s")
# or
Time.now.to_i
# Then you can also alias the to_i method:
Time.alias_method :unix_timestamp, :to_i
@svoboda-jan
svoboda-jan / calendar3.rb
Created January 18, 2025 20:48
Console calendar in Opal
# https://opalrb.com/try/
# Console Calendar
DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
CURRENT_DAY_HIGHLIGHTER = " ✅"
HIGHLIGHT_CURRENT_DAY = true
def days_in_month(month)
return 29 if Time.now.month == 2
DAYS_IN_MONTH[month]
@svoboda-jan
svoboda-jan / dice_betting_game_js_int.rb
Created January 18, 2025 20:53
A dice betting game using Opal Ruby to Javascript source-to-source compiler with JS interaction
# Dice Betting Game, https://opalrb.com/try/
require "native"
bank = 100;
while bank > 0 do
bet = $$.prompt("Place your bet").to_i
dice1 = (rand * 6).ceil
dice2 = (rand * 6).ceil
outcome = dice1 == dice2