简体   繁体   中英

What is the ruby equivalent of python -i?

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. Eg,:

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> 

You could use pry :

simple.rb :

s = "hello"
binding.pry

in the console:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM