11 - evaluating code in an earlier context.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 22 行

RB
22
字号
def broken_print_variable(var_name)  eval %{puts "The value of #{var_name} is " + #{var_name}.to_s}end#---tin_snips = 5broken_print_variable('tin_snips')# NameError: undefined local variable or method `tin_snips' for main:Objectvar_name = 'tin_snips'eval %{puts "The value of #{var_name} is " + #{var_name}.to_s}# The value of tin_snips is 5#---def print_variable(var_name, binding)  eval %{puts "The value of #{var_name} is " + #{var_name}.to_s}, bindingendvice_grips = 10print_variable('vice_grips', binding)# The value of vice_grips is 10#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?