Skip to content

Instantly share code, notes, and snippets.

@adam12
Last active July 17, 2024 14:11
Show Gist options
  • Save adam12/ae6cfef082025f9cc7e0dae95377546c to your computer and use it in GitHub Desktop.
Save adam12/ae6cfef082025f9cc7e0dae95377546c to your computer and use it in GitHub Desktop.
Example for dot-assignment from Ruby object (used with Ruby Next)
RubyNext.define_text_rewriter "dot_assign" do
def safe_rewrite(source)
source.gsub(/^.+ \.= .+$/) do |m|
m.match(/(.+) .= (.+)/) do |y|
variables = y[1].split(/,\s?/)
variables.map { |var| "#{var} = #{y[2]}.#{var}" }.join(";")
end
end
end
end
class User
def self.first
new
end
def first_name
"Bob"
end
def email
"[email protected]"
end
end
first_name, email .= User.first
puts "The values #{first_name.inspect} and #{email.inspect} were extracted from the User"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment