Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active September 12, 2018 04:27
Show Gist options
  • Save YumaInaura/cb408345413bbe789f2fa277d91efe1d to your computer and use it in GitHub Desktop.
Save YumaInaura/cb408345413bbe789f2fa277d91efe1d to your computer and use it in GitHub Desktop.
Ruby — Set variable to main with eval

Ruby — Set variable to main with eval

Example

set variable in main

alice = nil

binding and eval

binding.eval('alice = "liddle"')

variable binded to main

p alice # "liddle"

Ref

Versions

  • ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]

Links

#!/usr/bin/env ruby
alice = nil
binding.eval('alice = "liddle"')
p alice # "liddle"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment