From: "ko1 (Koichi Sasada)" Date: 2022-09-22T17:56:11+00:00 Subject: [ruby-core:109998] [Ruby master Feature#19015] Language extension by a heredoc Issue #19015 has been updated by ko1 (Koichi Sasada). This is tiny sample of SQL. Note that I'm newbe of SQL (I goooooooogled the syntax/api and the example is never tested). ```ruby module SQLite3HeredocExtension def compile str vs = [] compiled = str.gsub(/\?([a-z_][a-zA-Z\d]+)/){|m| vs << $1 ?? } [vs, compiled] end # syntax # * SQL statement # * but ?var is replaced with the Ruby's variable def heredoc_exntesion_SQL str, b vs, compiled_str = compile str stmt = @db.prepare compiled_str @db.execute stmt, vs.map{|v| b.local_variable_get(v).to_s} end end require 'sqlite3' @db = SQLite3::Database.new "test.db" include SQLite3HeredocExtension results = < ?lowest_age SQL ## hmm, "SQL" seems to return a SQL statement...? # AR results = People.select(:name).where("age >= ?", lowest_age) # AR/execute results = ActiveRecord::Base.connection.execute("select person.name from perople where person.age > ?", lowest_age) ``` ---------------------------------------- Feature #19015: Language extension by a heredoc https://bugs.ruby-lang.org/issues/19015#change-99249 * Author: ko1 (Koichi Sasada) * Status: Open * Priority: Normal ---------------------------------------- This propose new heredoc extension with `<Hello <%= name %> erb puts html #=>
Hello ko1
``` ## Background / considerations * Sometimes we write Ruby syntax string with `<