什么是python -i的ruby等价物?

时间:2022-03-17 13:48:18

python -i will execute a script then provide an interactive prompt which still has access to the variables declared in the script. Does ruby have an equivalent option? I've tried require but it seems the variables are no longer in scope after using it. E.g.,:

python -i将执行一个脚本,然后提供一个交互式提示,它仍然可以访问脚本中声明的变量。 ruby有相同的选择吗?我已经尝试过require,但似乎变量在使用后不再在范围内了。例如。,:

Steven$ cat simple.rb 
s = "hello"

Steven$ irb
irb(main):001:0> require_relative('simple')
=> true
irb(main):002:0> puts s
NameError: undefined local variable or method `s' for main:Object
    from (irb):2
    from /usr/bin/irb:12:in `<main>'
irb(main):003:0> 

2 个解决方案

#1


3  

You could use pry:

你可以使用pry:

simple.rb:

s = "hello"
binding.pry

in the console:

在控制台中:

$ pry simple.rb 
[1] pry(main)> puts s
hello
=> nil
[2] pry(main)> 

#2


-1  

If I understand your question, you access environment variables with the ENV hash with the variable name as the key. For example,

如果我理解您的问题,则使用ENV哈希以变量名作为键来访问环境变量。例如,

[max@max:~] $ export PIE=pecan
[max@max:~] $ irb
1.9.3-p385 :001 > print ENV['PIE']
pecan => nil

#1


3  

You could use pry:

你可以使用pry:

simple.rb:

s = "hello"
binding.pry

in the console:

在控制台中:

$ pry simple.rb 
[1] pry(main)> puts s
hello
=> nil
[2] pry(main)> 

#2


-1  

If I understand your question, you access environment variables with the ENV hash with the variable name as the key. For example,

如果我理解您的问题,则使用ENV哈希以变量名作为键来访问环境变量。例如,

[max@max:~] $ export PIE=pecan
[max@max:~] $ irb
1.9.3-p385 :001 > print ENV['PIE']
pecan => nil