您的位置:首页 > 其它

ENV and IRB

2015-07-27 23:48 148 查看
1. ENV

ENV can be inherited by children, but what children do will be transparent to parent.

puts "In parent, term = #{ENV['TERM']}"
fork do
puts "Start of child 1, term = #{ENV['TERM']}"
ENV['TERM'] = "ansi"
fork do
puts "Start of child 2, term = #{ENV['TERM']}"
end
Process.wait
puts "End of child 1, term = #{ENV['TERM']}"
end
Process.wait
puts "Back in parent, term = #{ENV['TERM']}"


2. irb

2.1 complete facility

Open the tab completion facility in irb. If you press TAB partway through a word, irb will look for possible completions. If there is only one possible, irb will fill it automatically. If there is more than one option, irb does nothing. However if you press
TAB again, it will display the list of all the completions at the terminal.

Ways to enable the completion:

irb -r irb/completion
irb -r irb/completion


2. subsession and bind

irb support multiple and concurrent sessions. And each subsession has seperate environment. enter a "irb" to create a new subsession, and use 'fg' to change diffrent subsession.

The code bellow illustrate the subsession.

irb(main):001:0> a = 12
=> 12
irb(main):002:0> irb
irb#1(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb#1(main):002:0> jobs
=> #0->irb on main (#<Thread:0x007fdd328bc8d0>: stop)
#1->irb#1 on main (#<Thread:0x007fdd339fb6a0>: running)
irb#1(main):003:0> fg 0
=> #<IRB::Irb: @context=#<IRB::Context:0x007fdd339c82c8>, @signal_status=:IN_EVAL, @scanner=#<RubyLex:0x007fdd3281d690>>
irb(main):003:0> a
=> 12


While, if you pass an object to the irb command, then the object will be binding to the self, and you can involk all the method directly without the object name.

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> irb a
irb#1([1, 2, 3]):001:0> size
=> 3
irb#1([1, 2, 3]):002:0> reverse
=> [3, 2, 1]
irb#1([1, 2, 3]):003:0> self
=> [1, 2, 3]
irb#1([1, 2, 3]):004:0> irb_exit
=> #<IRB::Irb: @context=#<IRB::Context:0x007fd2fa2c4320>, @signal_status=:IN_EVAL, @scanner=#<RubyLex:0x007fd2fb809708>>
irb(main):003:0> a
=> [1, 2, 3]
irb(main):004:0> size
NameError: undefined local variable or method `size' for main:Object
from (irb):4
from /usr/bin/irb:12:in `<main>'
irb(main):005:0>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: