How to count characters in Ruby

#-----------------------------------------------
# Counting Lines and Characters
#-----------------------------------------------

file= 'FILE PATH HERE'

=begin
Use File to implement the readlines method to read an entire file into an array line by line.
=end

lines = File.readlines(file)

#count the lines
line_count = lines.size 

#join them all into a single string
text = lines.join 

total_characters = text.length

puts "#{line_count} lines"
puts "#{total_characters} characters"

Ruby: Using File to implement the Readlines method to read an entire file into an array line-by-line

=begin
Using File to implement the readlines method to read an entire file into an array line by line.
=end

file= 'FILE PATH HERE'

lines = File.readlines(file)
line_count = lines.size #count the lines
text = lines.join #join them all into a single string

puts "#{line_count} lines"

How to load text files and count lines with Ruby

#-----------------------------------------------
# How to load text files and count lines
#-----------------------------------------------

file= 'FILE PATH HERE'

line_count = 0 # initialize line_count to store the count

=begin
Open the file and iterate over each line while incrementing line_count by 1 each time
=end

File.open(file).each { |line| 
line_count += 1 

}
puts line_count # Print the total to the screen 

Coding Journey Update: May 2017

I got completely burned out on the technical peculiarities of C Programming after finishing the book ‘Head First C Programming’ so I’ve decided to give my brain a bit of a break from C for the month of June and started reading ‘Beginning Ruby: From Novice to Professional’.  I am rather excited at the idea of coding a bot in Ruby and after all the grinding I’ve been doing with C it would truly be a welcome break at this time.

The Cliff of Confusion from Viking Code School Blog

I am currently in ‘The Desert of Despair‘ when it comes to my coding journey. It feels so weird knowing that I have finally survived ‘The Cliff of Confusion’.

I thought that how I would know that I had survived it was that I would just wake up one day and have the confidence that I was a programmer. It didn’t happen that way at all. I still don’t have the confidence that I am a programmer but instead, the fear that I would never understand all the information that I seemed to have just been constantly cramming in my head without the slightest understanding has now come together as if they were all puzzle pieces within a box.

My biggest obstacle while dealing with ‘The Cliff of Confusion’ was the fear that I was too stupid to ever make any sense of the fire hose of information that was coming at me. I was completely overwhelmed with all there was to learn and how many different paths there were to follow. There was just too much that I needed to absorb. It felt as if every day was exactly the same as the one before and that I was just grinding and grinding and that I couldn’t get any traction at all. I would talk about quitting a lot because I really felt that the fact that I wasn’t getting coding was because of an inherent flaw in either my character, my thinking style or my IQ.

In contrast, my biggest obstacle while dealing with ‘The Desert of Despair’ is my impatience. I can now see all the puzzle pieces in the box, but it feels like it is going to take forever to put them together to make a complete picture. Instead of being fearful all the time that I will never get where I want to go, I find that I am completely frustrated all the time with how long it is going to take me to get there.

I have so many projects that I want to do but now but I am having trouble figure out how to use the tools that I have now built up in my toolbox. I wanted to create a bot with C Programming but decided that it would be easier to just do it with Ruby since I already had a book where the final project was created a bot which I had struggled with and given up on in the past but now with what I’ve learned about C Programming under my belt I feel confident that I will now be able to understand it.

I will instead use my C Programming skills to work on Arduino projects since it would be a better fit for the language. Of course, that means I have to come up with the money to buy the hardware from somewhere and who knows when that would happen.

I was really afraid at first that trying to do Ruby at the same time as C Programming would end up completely overloading my brain to the point where I’d find that I wouldn’t be able to do anything at all but so far the two languages really seem to compliment each other.

I guess we will have to see at the end of June if this is another ‘Mirages of Mania’ and if I will just end up going in circles and once again end up where I started.

I just need to keep pushing and grinding and know that if I keep the faith that I will find myself in the ‘Upswing of Awesome’ someday. That day may come a year from now or five years or ten years but I have to keep the faith.

Faith is defined as belief without reason and at this point I have to admit to myself that it is the only compass I really have to get me where I am going at this point. The article, “Why Learning to Code is So Damn Hard” didn’t mislead me about ‘The Cliff of Confusion’ or ‘The Desert of Despair’  so I just need to keep the faith that the ‘Upswing of Awesome’ truly exists.

I just somehow need to keep the faith.

C Programming: How to calculates the length of string with strlen()

strlen function: size_t strlen(const char *str)

size_t represents unsigned short.

size_t is a data type at least 16 bits wide that is guaranteed to be able to safely store an array index or object size on your particular system.

Qualifiers alters the meaning of base data types to yield a new data type. Size qualifiers alters the size of a basic type. There are two size qualifiers, long and short.  Integers and floating point variables can hold both negative and positive values. However, if a variable needs to hold positive value only, unsigned data types are used.

The function returns the length of the string without including end character (terminating char ‘\0’).

#include <stdio.h>
#include <string.h>
int main()
{
     char str1[20] = "CodeGoat";
     printf("Length of string str1: %d ", strlen(str1));
     return 0;
}

Sources:
Beginners Book
C Data Types
What is size_t in programming

The most common beginner problem when working with random numbers in C programming

Many programs that you will write require the use of random numbers. For example, a game such as backgammon requires a roll of two dice on each move. Since there are 6 numbers on each die, you could calculate each roll by finding a random number from 1 to 6 for each die.

We can use the rand() function in C programming to generate a random number but there is a problem and that is randomness. Randomness is a problem in computing because digital computers are deterministic. If you give them the exact same instructions they always end up with the exact same result. It turns out to be mathematically impossible to generate true random numbers using a digital computer, but it is fairly easy to generate pseudo-random numbers

But when we are generating our random number with the rand() function, the sequence of numbers will be exactly the same on each run of the program. This is because the numbers are generated by a mathematical algorithm which when given its starting number (called the “seed”) will always generates the same sequence of numbers. This is convenient when you are debugging a program, but it is unhelpful if you want the program to behave unpredictably.

If you want a different pseudo-random series each time your program runs, you must specify a different seed each time.

To combat this problem of always ending up with the same ‘random’ number you use another function, srand(), which conveniently is also declared in the stdlib.h header file. Using this method of randomization, the program can use a different seed value on every run, causing a different set of random values every run.

But now we have another problem, how to get an arbitrary seed value? If we were to forcing the user to enter this value every time the program needed a random number that wouldn’t be very efficient at all. We will need another way to get it. The closest thing to a perfect source for an always-changing value, the system clock. When converted to an unsigned integer, a positive whole number, the program time (at execution of program) can make a very nice seed value. This works nicely because no two program executions will occur at the same instant of the computers clock. Since time is continually changing, the seed is forever changing. But in order to use it we need to remember to include the time.h header file. Then we can use the time function insided the srand() function like this:

srand(time(NULL));

SOURCES:
http://www.gnu.org/software/libc/manual/html_node/Pseudo_002dRandom-Numbers.html
http://www.cprogramming.com/tutorial/random.html
https://mathbits.com/MathBits/CompSci/LibraryFunc/rand.htm
http://www.grasshopper3d.com/forum/topics/what-are-random-seed-values