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?

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • Another way that irb can err on you http://stackoverflow.com/questions/39269032/why-rails-can-use-if-as-hash-key-but-not-in-ruby/39271791#39271791 – lulalala Jan 23 '17 at 06:46

1 Answers1

27

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

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 2
    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. – Alex Wayne Jan 18 '12 at 20:03
  • @AlexWayne: thanks for clarifying. Should have done it myself. – Sergio Tulentsev Jan 18 '12 at 20:05
  • 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. – tadman Jan 18 '12 at 20:06
  • Ah, that did it. Strange though, since the tabs were at the beginning of the line. Oh, wait I see it now. – jcollum Jan 18 '12 at 20:09
  • but there's a reason -- demonstrated here -- to hate tabs – jcollum Nov 01 '18 at 16:30