Can rails load .irbrc from the rails project root such as ~/rails-projct/.irbrc. Rather then ~/.irbrc. Since it would be nice to add configuration that's specific to a project.
Per the docs:
IRB reads from ~/.irbrc when it's invoked.
If ~/.irbrc doesn't exist, irb will try to read in the following order:
.irbrc
irb.rc
_irbrc
$irbrc
Meaning that this is possible, but only if you don't have a .irbrc file in your home directory. Tested and confirmed in Ruby 2.7.
If that's not possible, you'll have to rely on command line options and/or environment variables.
Related
I would like to use information stored within a the credentials.yml.enc file for a Rails 5.2 app. However, I am struggling to get a command which works perfectly within the console to behave in the same way when inserted into a .rb file.
In the Rails console (on my local development computer) Rails.application.credentials.username returns "my_username"
If I insert this line within a very simple db_backup.rb file as shown below, I get the error:
NameError: uninitialized constant #<Class:#<Backup::Config::DSL:0x00007fb0db941d10>>::Rails
db_backup.rb:
Model.new(:db_backup, 'Description for db_backup') do
##
# PostgreSQL [Database]
#
database PostgreSQL do |db|
db.username = Rails.application.credentials.username
end
end
Please could you explain why I get the different behaviour when using exactly the same line of code in the Rails console / within a .rb file?
The context in which the code is executed is not the same. One is the rails console and the other is the backup command
What happens when you load the Rails console
Launching the rails console means you launch all of the rails stack before executing your code against it. The Rack applications like Sinatra, Rails etc. use the config.ru file as a convention for which file should be run to boot. (You can explore the rabbit hole if you want to have a deep understanding of this)
It means that the vast majority of errors you can encounter when will occur during the console boot, preventing you from executing anything in the console (because boot failed). Instead it will print the stack trace errors for you to figure out what went wrong so you can correct and give it another try.
TL; DR Rails.application.credentials.username in console is executed after all of the Rails stack (models, dependencies, initializers) has loaded in a particular order
What happens when you run the backup command
The backup command is defined here in the bin repo of the backup repo
It goes like this
#!/usr/bin/env ruby
# encoding: utf-8
require File.expand_path("../../lib/backup", __FILE__)
Backup::CLI.start
If you open the required file lib/backup.rb and look around in the Gemfile, you won't fine a place where you have a dependency or a define for the Rails constant.
Thus when you run the backup command and execute your db_backup.rb, the constant Rails called here is ... not defined. Ruby being kind will try to find a nested version of this constant in the current scope which is the Model.new do; end block.
It is still not defined which ruby tells you about with NameError: uninitialized constant #<Class:#<Backup::Config::DSL:0x00007fb0db941d10>>::Rails.
#giglemad gives a great explanation of the issue of class resolution in the execution context (rails console vs. running of the backup ruby file).
To fix your error, just let the code know to use the top-level class lookup (::Rails):
Model.new(:db_backup, 'Description for db_backup') do
##
# PostgreSQL [Database]
#
database PostgreSQL do |db|
db.username = ::Rails.application.credentials.username
end
end
If you're still seeing the missing Rails constant, you'll need to put your script in either a rake task, or require your rails environment.
I ended up resolving this by simply adding the line below to the top of my db_backup.rb:
require './config/environment' # added to enable credentials to be read from Rails environment
I am new to Ruby. I am dealing with a codebase of the following nature.
I have a main/ directory containing my entire codebase, and inside it I have files like:
main/lib/foo/test1.rb and
main/app/bar/test2.rb
Inside main/app/bar/test2.rb there is the line: require 'test1'
However, if I am in the main/ directory and I run ruby main/app/bar/test2.rb I get the following error: require': cannot load such file -- access_control (LoadError)
Now, upon Googling, I think this has something to do with /config/application.rb and adding the line: config.autoload_paths += Dir["#{config.root}/lib", "#{config.root}/lib/**/". I do this, but it doesn't seem to make any difference.
Presumably my production environment knows where to look in the require 'test1.rb' statement, while my environment does not. How can I fix this?
Thanks!
It sounds like you have a Ruby on Rails application.
Given that, you probably don't want to run any contained script directly with ruby. If it's designed to be executed directly, try running it with rails runner path/to/file.rb (from inside the directory that contains app/, lib/, config/, etc).
If it's inside app/ or lib/, though, it's more likely intended to be loaded as a library from some other script elsewhere in the application... those would both be unusual places to keep a stand-alone executable.
When I'm using the rails console I like having a clear! command along side the reload! command, so every time I launch the rails console I write
def clear!
system('clear')
end
When I repeat behavior in my bash shell I add it to my ~/.bashrc file. Is there a similar way for me to do this for my rails console?
Create a file in your home directory named ~/.irbrc. Inside, define any functions or settings you want to be applied to your irb.
Here's an example that explains what I mean.
You can do this with Pry if you use that instead of irb. You can configure custom commands in a ~/.pryrc
Pry.config.commands.command "clear!", "Clears the display" do |*args|
system("clear")
end
See pry-rails
I am starting out with Ruby on Rails. I am currently going through a tutorial where it says that I have to run a .rb file from IRB and that that will create a .xml file in my current directory.
My question is how do I run a .rb file in IRB?
And do I have to be in the directory where this .rb file lives when I run it in IRB?
I tried the following: just typing irb on the command line in the directory of the file. That starts an IRB session as far as I understand.
Then I typed irb "filename.rb" which went through but didn't create anything in the current directory but at least it didn't give any errors.
I also tried a whole bunch of other stuff that plain gave me errors. So I don't think I can solve this myself and googling the matter didn't help at all.
I am running Leopard.
You can "run" a file in irb by just requiring or loading it.
$ irb
>> load './filename.rb'
To change your current working directory within irb, you can use FileUtils:
>> require 'fileutils'
>> FileUtils.pwd # prints working directory
>> FileUtils.cd '/path/to/somewhere' # changes the directory
In case you want to have your file loaded in the irb session and you are using Ruby 2+ you can load a file in irb like this:
irb -r ./the_name_of_your_file.rb
This opens an irb session with the given file loaded.
Imagine you have file with a class like this:
class Example
def initialize(name)
#name = name
end
def print__example
p name
end
end
You will be able to use the Example class in the irb session.
We can just create a .rb file in the directory which you are currently working in using any text editor and type all the code in that and then use the command ruby filename.rb in the terminal, not in the irb, then it shows the output in irb.
I am going to learn Ruby on Rails (ROR) , can any one help me how to write a "hello world" program and How can I run the program.
First, you have to install ruby http://www.ruby-lang.org/en/ .
All your things you have to install you find here http://rubyonrails.org/download
Well, i suggest to start with http://rubyonrails.org/screencasts, the 15 minutes weblog tutorial.
Check out _why's poignant guide to ruby.
I got the answer....
we need to use the following steps.
rails appliaction_name
- It will create a directory.
In that directory contains list of files and directories.
app config doc log Rakefile script tmp
components db lib public README test vendor
Change the file mode "777" for public/ log/ and tmp/ file. because the program will write the datas in to the files.
./script/generate controller controller_name
Then it will create the controller_name.rb (ruby file in app/ directory)
Then we can write the codeing in to the app/controllers/controller.rb file.
Then we need to call the function from browser.
Thanks................