Coyping files

as the heading says , It’s possible to copy a whole file with:

from_file, to_file = ARGV     #the file we want to copy and the place to paste it

puts “Copying from #{from_file} to #{to_file}”

in_file = open(from_file)

indata =    #reads whats in the file and assigns it to a variable

puts “The input file is #{indata.length} bytes long”

puts “Does the output file exist? #{File.exist?(to_file)}”   #File.exist? checks if there’s                                                                                                               a file to paste stuff in (true/false)

puts “Ready, hit RETURN to continue, CTRL-C to abort.”


out_file = open(to_file, ‘w’)    #prepares and opens the file to write/copy data to the copy

out_file.write(indata)    #actually writes whats in the original file

puts “Alright, all done.”



File.exist?(to_file) = checks if there’s a file you called the function with and prints out true/false.

In easier words: File, use the exist function to check if there’s a to_file on my disk!

Names , Variables, Code and Functions ..

A function and what it does:

  1. They name pieces of code the way variables name strings and numbers
  2. They take arguments the way your scripts take ARGV, like: File.exist?(to_file)
  3. Using the steps above they let us make our own “mini-scripts” or “tiny commands”

# this one is like your scripts with ARGV
def print_two(*args)
arg1, arg2 = args
puts “arg1: #{arg1}, arg2: #{arg2}”

*args: take all the arguments to the function and the put them in args as a list. It’s like ARGV but for functions

# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2)
puts “arg1: #{arg1}, arg2: #{arg2}”

# this just takes one argument
def print_one(arg1)
puts “arg1: #{arg1}”

# this one takes no argument
def print_none()
puts “I got nothing”

print_two(“Mark”, “Nathalie”)
print_two_again(“Mark”, “Nathalie”)

#Here we call the function with arguments so that it prints them out

Functions = Methods?

So i googled for a difference between those two and from what i’ve read, there’s no difference at all, a lot of people still use both terms when they mean the same.

To reference a stackoverflow user:

“In Ruby, there are not two separate concepts of methods and functions. Some people still use both terms, but in my opinion, using “function” when talking about Ruby is incorrect. There do not exist executable pieces of code that are not defined on objects, because there is nothing in Ruby that is not an object.”

Functions and Variables

def cheese_and_crackers(cheese_count, boxes_of_crackers)   #this function takes in                                                                                                                                      local variables as arguments
puts “you have #{cheese_count} cheeses!”
puts “you have #{boxes_of_crackers} boxes of boxes of crackers!”
puts “Man that’s enough for a party!”
puts “Get a blanket.\n”

puts “We can just give the function numbers directly:”

cheese_and_crackers(20, 30)    #those are the numbers the function uses the first time

puts “OR, we can use variables from our script:”

amount_of_cheese = 10

amount_of_crackers = 50    #… and those are temporary versions with which the function                                                              runs another time

cheese_and_crackers(amount_of_cheese, amount_of_crackers)

puts “We can even do math inside too:”

cheese_and_crackers(10+20, 5+6)

puts “And we can combine the two, variables and math:”

cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

with cheese_and_crackers we basically run/call/use the function with several ways of using arguments


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s