Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:33:33+00:00 2026-05-26T13:33:33+00:00

I work with rails console and often i need to preload some ruby code

  • 0

I work with rails console and often i need to preload some ruby code to work with.

#file that i want to load in rails console
#my_file.rb
a = 1
b = 2
puts a + b 

When i run my console with ./script/console

rails-console :001 > load 'my_file.rb' 
3
 => []
rails-console :002 > a
NameError: undefined local variable or method 'a' for #<Object:123445>

How can i get access to my ‘a’ and ‘b’ variables in console?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:33:34+00:00Added an answer on May 26, 2026 at 1:33 pm

    When you load a file local variables go out of scope after the file is loaded that is why a and b will be unavailable in the console that loads it.

    Since you are treating a and b as constants how about just capitalizing them like so

    A = 1
    B = 2
    puts A+B
    

    Now in you console you should be able to do the following

    load 'myfile.rb'
    A #=> 1
    

    Alternately you could make the variables in myfile.rb global ($a, $b)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a rake task that does some DB work outside of Rails/ActiveRecord. Is
I have done some work with Ruby on Rails but am still not comfortable
After doing some work with Ruby, Rails, and RSpec last summer and I learned
So I am beginning to work with Rails and I get some of the
I'm doing maintenance work on a Rails site that's deployed using Phusion Passenger .
I'm doing maintenance work on an existing Rails site and am having some problems
I'm coming from a Rails background and doing some work on a ASP.NET project
I'm new to rails and making some sort of noob mistake: I frequently need
I tried doing this when in rails console: puts about_home_index_path It gave me an
I have a Rails 3 app using Ruby 1.9.2 configured to work on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.