Skip to content

Instantly share code, notes, and snippets.

@dpaola2
Created December 6, 2022 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpaola2/b2b5dfb20a1ef88fa57a7b74dd970673 to your computer and use it in GitHub Desktop.
Save dpaola2/b2b5dfb20a1ef88fa57a7b74dd970673 to your computer and use it in GitHub Desktop.
def javascript_to_elixir(js_code)
# Define a method that will convert JavaScript keywords to Elixir
define_method :convert_keyword do |keyword|
case keyword
when "function"
"def"
when "var"
"def"
when "let"
"def"
when "const"
"def"
when "return"
"return"
else
raise "Unknown keyword: #{keyword}"
end
end
# Split the JS code into individual lines
lines = js_code.split("\n")
# Iterate over each line and convert it to Elixir
elixir_lines = lines.map do |line|
# Split the line into words
words = line.split(" ")
# Check if the first word is a keyword and convert it if necessary
if words.first.end_with?("function")
words[0] = convert_keyword(words.first)
end
# Join the words back into a line and return the result
words.join(" ")
end
# Join the Elixir lines back into a single string and return the result
elixir_lines.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment