Skip to content

Commit

Permalink
ARGV
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-works committed Mar 7, 2021
1 parent 522c737 commit c163dbe
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Binary file added images/2021-03-06-at-5.05-PM-argv.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion lib/night_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class NightWriter

# write it to another file

def get_cli_arguments
require "pry"; binding.pry
ARGV
end

def print_message
file = File.open("message.txt").read
puts file
Expand All @@ -13,7 +18,8 @@ def print_message

end

NightWriter.new.print_message
NightWriter.new.get_cli_arguments
# NightWriter.new.print_message

# Test how this works with the following in your terminal:
# ruby lib/night_writer.rb
57 changes: 57 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,61 @@ Actually, I'll cycle through quotes from the above book summary in/out of my `me

Here's the current commit:

https://github.com/josh-works/night_writer/commit/522c737

## Update your existing program so that the name of the file that it prints out changes based on the second input that the user provides from the command line.

phew, there's a middle step. Instead of `puts`'ing to the terminal, write a new file to the terminal. something like this:

```ruby
def read_write
message = "i'm a cool message"
File.open("./message.txt", "+a") do |file|
file.write(message)
end
end
```

Whenever you run the above bit of code, it _should_ create a new file with the given title, write the message to the file, and... if you `ls` wherever it wrote the file, you should see the new file. Try changing the message.

So, we can upgrade this to make the message that we write to "dynamic", like:

```ruby
def read_write(desired_file_path)
message = "i am a message"
File.open(desired_file_path, "a+") do |file|
file.write(message)
end
end

NightWriter.new.read_write('write_to_this_file.txt')
```

I interpret this to mean:

> Update night_writer.rb so that:
> 1. the name of the file that it writes to prints out changes based on the second input that you give it, like so:
```shell
ruby ./lib/night_writer write_a_message_to_this_file.txt
```

For getting this input from the command line, check out:

- [https://stackoverflow.com/questions/6556280/read-input-from-console-in-ruby](https://stackoverflow.com/questions/6556280/read-input-from-console-in-ruby)
- [https://www.codecademy.com/articles/ruby-command-line-argv](https://www.codecademy.com/articles/ruby-command-line-argv)
- [https://careerkarma.com/blog/build-ruby-cli/](https://careerkarma.com/blog/build-ruby-cli/)


--------------------

Hint for the CLI stuff:

```
$ ruby lib/night_writer.rb "hi there"
```

![it works](/images/2021-03-06-at-5.05-PM-argv.jpg)

Hint:

0 comments on commit c163dbe

Please sign in to comment.