Reload files in irb


















The text was updated successfully, but these errors were encountered:. If not, it sounds like a broken Ruby 2. I'm not sure how that could have happened during rbenv install 2. Did you notice anything off during the installation? In any case, this is not a bug with rbenv. If anything, it's a bug with Ruby's own installer and its default gems.

Try this to fix it:. Sorry, something went wrong. Thank you so much, Mislav! I didn't notice anything off in the installation, but installing it manually with the command your provided did the trick.

You can import libraries into your IRB session using the require statement, just as you would in a Ruby program. You can use those in your IRB session exactly like you would in a Ruby program. IRB indicates that this statement returns true , which tells you that the library was loaded successfully. Now type this code into IRB to make a request to icanhazip. Then use the gem command to install the httparty gem:.

This time, IRB will display true , letting you know it was able to load the library. Enter this code into IRB to try it out:. If you start an IRB session and use the -r switch, you can specify libraries or gems you want to load when IRB starts.

For example, irb -r httparty would launch an IRB session with the httparty gem already loaded, meaning you can skip the explicit require httparty statement. However, you can also use this to load your own code into a new session, which is helpful when you want to explore it or test it out. We would use this IPGrabber object in our own program to insulate our code from external changes; using our obect would let us switch out the underlying library and the site we use to resolve the IP address without having to change how the rest of our code works.

Then launch IRB and load this file. By loading your own code into an IRB session, you can inspect code and work with your own libraries before incorporating them into a full program. You can create a configuration file called. You can then add support for autocompletion, indentation, and command history. First, configure autocompletion support in IRB. With this enabled, the last statements you type will be logged to the.

Next, add this line to your configuration file to enable auto-indenting, which is handy when writing classes, methods, and blocks:. Your configuration file can include any additional valid Ruby code, which means you could define helper methods or use require to load additional libraries.

For example, to add a history helper to your IRB session which would show your history, add this code to. You may have quite a bit, so you can alter the history command so it takes an optional number of lines to show. Replace the code for the history function with this code, which takes an optional argument called count and uses it to limit the entries it displays:. Save the file and start a new IRB session. In the Rails console, there is a reload! It's a fairly complex process for Rails, but when you have a much simpler project, all you have to do is reload all your files and you're good to go.

We can add a reload! Let's see if it works. Using the previous calculator example, let's open a new console session:. It doesn't work because we don't have the multiply method written on the Calculator class.



0コメント

  • 1000 / 1000