<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pmckernin</title>
    <description>The latest articles on DEV Community by pmckernin (@pmckernin).</description>
    <link>https://dev.to/pmckernin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F452205%2F1cc8c22e-8ead-496f-a323-cb729aac1c4b.jpeg</url>
      <title>DEV Community: pmckernin</title>
      <link>https://dev.to/pmckernin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pmckernin"/>
    <language>en</language>
    <item>
      <title>Writing a ruby script to find and replace a file</title>
      <dc:creator>pmckernin</dc:creator>
      <pubDate>Thu, 20 Aug 2020 16:16:20 +0000</pubDate>
      <link>https://dev.to/pmckernin/writing-a-ruby-script-to-find-and-replace-a-file-5bp3</link>
      <guid>https://dev.to/pmckernin/writing-a-ruby-script-to-find-and-replace-a-file-5bp3</guid>
      <description>&lt;p&gt;This article is targeted for teachers and someone who works with beginners. If someone forked a project and started to work on it, but you have found a mistake in it and need to fix it, here is a way of fixing a specific file by replacing a file in a rails project by creating a command-line script for students and beginners to run. &lt;/p&gt;

&lt;p&gt;First, create your fix. An example of this would be if I wrote a bad test and wanted to replace my test file with my new one.  I would link directly to the raw ruby file. It is important to keep track of this URL.  Here is a link to a &lt;a href="https://raw.githubusercontent.com/pmckernin/dotfiles/master/ruby_file.rb"&gt;ruby file&lt;/a&gt;. The one I have linked here is just a simple script that prints "Hello World".  Usually, your script on Github would be the exact file you are working on with the fix.  Also, to be clear, this script will replace a file, not amend it.&lt;br&gt;&lt;br&gt;
Second, we will need to write our script. Here is an example script, let's break down what each line does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env ruby
#wget  -O bin/hello_world "url" &amp;amp;&amp;amp; chmod 777 bin/hello_world &amp;amp;&amp;amp; bin/hello_world
require "open-uri"
spec_path = "spec/features/user_auth_spec.rb"

format_file = File.open(spec_path, File::RDWR)

text = File.open(spec_path).read
url="https://raw.githubusercontent.com/pmckernin/dotfiles/master/ruby_file.rb"
new_content = open(url).read

File.open(format_file, "w") { |file| file &amp;lt;&amp;lt; new_content }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;#!/usr/bin/env ruby&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is commented out because it does not run in the script but should run in the terminal. I keep this in the script so I don't have to continually look up what to run. This line will allow us to call the name of our script without having to type &lt;code&gt;ruby&lt;/code&gt; in front of it.  Just as when we run &lt;code&gt;bin/setup&lt;/code&gt; we don't have to call &lt;code&gt;ruby bin/setup&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;#wget  -O bin/hello_world "url" &amp;amp;&amp;amp; chmod 777 bin/hello_world &amp;amp;&amp;amp; bin/hello_world&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This line is also commented out because it is not meant to run in the script, but is meant for the students to run at their command line. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wget -0&lt;/code&gt; is a bash command for downloading files from the internet, with &lt;code&gt;-0&lt;/code&gt; will allow us to name the file that you download from the URL. &lt;a href="https://stackoverflow.com/questions/9830242/what-does-wget-o-mean/16765470#16765470"&gt;Here is more on wget&lt;/a&gt;.
&lt;/li&gt;
&lt;li&gt;The first &lt;code&gt;bin/hello_world&lt;/code&gt; is creating a file in our bin folder. We can name this whatever we would like, but eventually what we run at the command line to execute the script.
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;url&lt;/code&gt; is the URL that we are going do download from, in our case, the URL would be replaced with the one I have linked above. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;chmod 777 bin/hello_world&lt;/code&gt; will make sure that we have admin access to the &lt;code&gt;hello_world&lt;/code&gt; file. The &lt;code&gt;chmod&lt;/code&gt; command has other numeric values that determine what you can execute from the command line, &lt;a href="https://www.computerhope.com/unix/uchmod.htm"&gt;here is further reading&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;The second &lt;code&gt;bin/hello_world&lt;/code&gt;, will run the name of the file you have created. &lt;/li&gt;
&lt;li&gt;Just a reminder but the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; allow you to run multiple bash commands on the same line. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;require "open-uri"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; We need to require "open-uri" in order to be able to open files from URLs as well as files in our project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;spec_path = "spec/features/user_auth_spec.rb"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will set the &lt;code&gt;spec_path&lt;/code&gt; variable as the path to the file we want to replace, in this case, it would go to our spec folder, into the features folder and look for a file called &lt;code&gt;user_auth_spec.rb&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;format_file = File.open(spec_path, File::RDWR)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This line creates the &lt;code&gt;format_file&lt;/code&gt; variable using the built-in ruby class &lt;code&gt;File.open&lt;/code&gt; passing in our &lt;code&gt;spec_path&lt;/code&gt; and giving the file read and write privileges.  Here is more about the ruby &lt;a href="https://ruby-doc.org/core-2.5.0/File.html#method-c-open"&gt;File class&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;text = File.open(spec_path).read&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This will use the ruby &lt;code&gt;File.open&lt;/code&gt; to go to our path for our project and save what it reads as the &lt;code&gt;text&lt;/code&gt; variable. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;url="https://raw.githubusercontent.com/pmckernin/dotfiles/master/ruby_file.rb"&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;url&lt;/code&gt; variable will point us to the file we want to input into our project. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;new_content = open(url).read&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;new_content&lt;/code&gt; will go to the &lt;code&gt;url&lt;/code&gt; we have defined and read the content&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;File.open(format_file, "w") { |file| file &amp;lt;&amp;lt; new_content }&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This line will replace the content from our defined path with the content we downloaded from the URL. The shovel operator will overwrite the content of the file we just opened with our new content. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, at the command line, we will have the student run the script. The script is &lt;code&gt;wget  -O bin/hello_world "https://raw.githubusercontent.com/pmckernin/dotfiles/master/ruby_file.rb" &amp;amp;&amp;amp; chmod 777 bin/hello_world &amp;amp;&amp;amp; bin/hello_world&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Adding a runtime dependency to run gems in your gem</title>
      <dc:creator>pmckernin</dc:creator>
      <pubDate>Thu, 13 Aug 2020 22:32:17 +0000</pubDate>
      <link>https://dev.to/pmckernin/adding-a-runtime-dependency-to-run-gems-in-your-gem-3poe</link>
      <guid>https://dev.to/pmckernin/adding-a-runtime-dependency-to-run-gems-in-your-gem-3poe</guid>
      <description>&lt;p&gt;If you are having trouble getting a gem to run in your own gem here is a quick troubleshooting guide.  &lt;/p&gt;

&lt;p&gt;Is the gem you want to use showing up in your &lt;code&gt;Gemfile.lock&lt;/code&gt;?  Your &lt;code&gt;Gemfile.lock&lt;/code&gt; file will display gems that are being called by your &lt;code&gt;Gemfile&lt;/code&gt; and &lt;code&gt;your_gem.gemspec&lt;/code&gt;.  If the gem you want to use is not being shown here we will need to get it to show up here.  &lt;/p&gt;

&lt;p&gt;First, check your &lt;code&gt;Gemfile&lt;/code&gt; to see if the gem you want to be used has been put in this file.  &lt;/p&gt;

&lt;p&gt;Second, check your gem.gemspec to &lt;code&gt;add_runtime_dependency&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Gem::Version.new(Gem::VERSION) &amp;gt;= Gem::Version.new('1.2.0') then
      s.add_runtime_dependency(%q&amp;lt;gem_name&amp;gt;.freeze, ["&amp;gt;= 0"])
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;your_gem.gemspec&lt;/code&gt; file is pretty much a readme for your gem, &lt;a href="https://guides.rubygems.org/specification-reference/#add_runtime_dependency"&gt;here is more info about gemspecs&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;Third, make sure to &lt;code&gt;require "gem_name"&lt;/code&gt; in the file you want to use the gem. &lt;/p&gt;

&lt;p&gt;This should allow you to use another gem in your gem.  &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
