Rails console exits while using undefined variable in 'select' block #14309

Closed
humzashah opened this Issue Mar 7, 2014 · 9 comments

2 participants

@humzashah
[1,2,3].select{|a| a == n} # 'n' is undefined

I was expecting to be told that the 'n' in the end was undefined, like in IRB. However executing the above command instantly exits the Rails console without any explanation.

Ruby 1.9.2, Rails 3.2.3.

P.S. I've edited this issue quite a number of times. This is the final version.

Update: I'd like a confirmation on this because typing just 'n' (when it is undefined) and pressing the Enter key is apparently exiting Rails console as well. Perhaps the project I'm working on has overwritten 'n' somehow...?

@dmathieu
› bundle exec rails c
Loading development environment (Rails 4.1.0.rc1)
>> [1,2,3].select{|a| a == n}
NameError: undefined local variable or method `n' for main:Object
    from (irb):1:in `block in irb_binding'
    from (irb):1:in `select'
    from (irb):1
    from /Users/shazino/.rvm/gems/ruby-2.1.0-p0/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:90:in `start'
    from /Users/shazino/.rvm/gems/ruby-2.1.0-p0/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:9:in `start'
    from /Users/shazino/.rvm/gems/ruby-2.1.0-p0/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/shazino/.rvm/gems/ruby-2.1.0-p0/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/shazino/.rvm/gems/ruby-2.1.0-p0/gems/railties-4.1.0.rc1/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

The console does not exit here, using 4.1.0.rc1. Can you reproduce it on a new app?

@humzashah

On the current app I'm working on:

protonlabs@pl-pc:~/myrailsapp$ bundle exec rails c
Loading development environment (Rails 3.2.3)
irb(main):001:0> n
protonlabs@pl-pc:~/myrailsapp$

And I cannot reproduce it in a newly created app:

protonlabs@pl-pc:~/something_new$ bundle exec rails c
        SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
        This poses a security threat. It is strongly recommended that you
        provide a secret to prevent exploits that may be possible from crafted
        cookies. This will not be supported in future versions of Rack, and
        future versions will even invalidate your existing user cookies.

        Called from: /home/protonlabs/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.3/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `initialize'.

Loading development environment (Rails 3.2.3)
irb(main):001:0> n
NameError: undefined local variable or method `n' for main:Object
    from (irb):1
    from /home/protonlabs/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
    from /home/protonlabs/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
    from /home/protonlabs/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):002:0>

Any idea how 'n' could be overwritten like this within my project?

@dmathieu

This is not exiting your ruby console, and this is the normal behavior. You can actually reproduce it with irb.

› irb
>> [1,2,3].select{|a| a == n}
NameError: undefined local variable or method `n' for main:Object
    from (irb):1:in `block in irb_binding'
    from (irb):1:in `select'
    from (irb):1
    from /Users/shazino/.rvm/rubies/ruby-2.1.0-p0/bin/irb:11:in `<main>'

Local variables need to be defined before using them. If you want to use a variable which doesn't have to be defined previously, make it an instance variable.

› irb
>> [1,2,3].select{|a| a == @n}
=> []

I'm going to give this issue a close, since it's a ruby problem and not something related to rails.

@dmathieu dmathieu closed this Mar 7, 2014
@humzashah

Thanks for the quick response, Damien. The problem is with 'n' somehow. For example, look at the following:

protonlabs@pl-pc:~/myrailsapp$ bundle exec rails c
Loading development environment (Rails 3.2.3)
irb(main):001:0> [1,2,3].select{|a| a == p}
=> []
irb(main):002:0> 

Apparently my Rails console exits only when a non-'n' undefined variable is being used.

In IRB and in a new app, proper error messages are shown. But in my Rails console, the environment exits upon usage of 'n' undefined varilable.

@dmathieu

p is defined by ruby, it's an alias to puts.

@humzashah

Yes, of course. Bad example. Consider the following:

protonlabs@pl-pc:~/myrailsapp$ rails c
Loading development environment (Rails 3.2.3)
irb(main):001:0> c #this will show an 'undefined variable' message
NameError: undefined local variable or method `c' for main:Object
    from (irb):1
    from /home/protonlabs/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
    from /home/protonlabs/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
    from /home/protonlabs/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):002:0> n #this will exit the console. no idea why!
protonlabs@pl-pc:~/myrailsapp$

:/

@dmathieu

I would think, since you cannot reproduce it in a newly created app, this is due to a gem, or some code inside your application.

@humzashah

Yes, I'm thinking along those lines as well. I'll try detecting the source of this. But I'd welcome any suggestions on how to trace this weird occurrence back to its source.

@dmathieu

Try adding all the gem dependencies into the new app and see if that's not one of them.
Then, try adding all monkey-patches into the new app and see if that's not one of them.
Do the same for anything else in that app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment