why does irb freak out sometimes when I paste in a script?
Asked Answered
A

1

12
def load_lib
    path = File.join(File.dirname(__FILE__), 'lib')
    failures = []
    Dir.glob("#{path}/**/*.rb").each {  |file|
        puts "loading: #{file} ... "
    }
end 

There's the script. When I put each line in individually, the load_lib function is available and works fine. But when I paste it into irb in one big chunk (Ubuntu terminal, Sh Ctrl C) it freaks out at the Dir.glob(... line and shows this:

Display all 931 possibilities? (y or n)
!
!=
!~
<=>
.... [dozens of lines in this vein]

and then the method isn't created at all.

Here's what happens (success) when I paste it in one line at a time:

>>  def load_lib
>>     path = File.join(File.dirname(__FILE__), 'lib')
>>     failures = []
>> Dir.glob("#{path}/**/*.rb").each {  |file|
?> puts file
>> }
>> end
=> nil
>> load_lib
./lib/alpha_processor.rb
./lib/development_mail_interceptor.rb
./lib/service_processors/beta_processor.rb

Is there something about the [] or {} that irb doesn't like when they are pasted in?

Aeonian answered 18/1, 2012 at 19:59 Comment(1)
Another way that irb can err on you #39269532Picrotoxin
G
28

That's because of TAB characters you have in your source file. Indent with spaces. :-)

Gowk answered 18/1, 2012 at 20:0 Comment(5)
More accurately, the tab characters trigger irb's autocompletion. Which you obviously dont want when pasting in code. The Ruby community has pretty much standardized on 2 space indentation by now, so please just use that.Boren
@AlexWayne: thanks for clarifying. Should have done it myself.Gowk
Tab is used for auto-complete in irb so sometimes cut and paste will trigger this. I think there's an option for turning it off but I can only find references on how to turn it on.Lifton
Ah, that did it. Strange though, since the tabs were at the beginning of the line. Oh, wait I see it now.Aeonian
but there's a reason -- demonstrated here -- to hate tabsAeonian

© 2022 - 2024 — McMap. All rights reserved.