Viking Code School: the Coding Challenge Preparation

I receive an email with the link to do the final challenge after reading the instructions there are a list of items that the instructions mention that will be on the challenge:

  1. Basic data types
  2. Conditional Logic
  3. Looping
  4. Methods

I will take a couple of days to review my notes on this topics then I will do the Challenge.

I will create a post for each of this topics, and with no more delays. lest do the review.

Category: Daily | Comments Off on Viking Code School: the Coding Challenge Preparation

Vampire Numbers

Instructions

6 * 21 = 126 # 6 and 21 would be valid ‘fangs’ for a vampire number as the # digits 6, 1, and 2 are present in both the product and multiplicands

10 * 11 = 110 # 110 is not a vampire number since there are three 1’s in the # multiplicands, but only two 1’s in the product

 

Category: Daily, Ruby Examples | Comments Off on Vampire Numbers

Writing Methods

How many things should a method ideally do?

Just ONE


What types of things should a method modify?

Any type of data.


What should you name a method?

Method names in Ruby may contain upper-case and lower-case letters, numbers, underscores _ and the punctation signs !, ?, =.

A method name can’t begin with a number, and the characters !, ? and = can only appear at the end.

Non-ASCII characters can be used in a method name, but this can lead to very confusing situations and should not be common practice.


What does self mean?

This means that the intended code will apply only to the current instance of the class,


What do you need to do to create your own Ruby script file?

a computer that can save a text file with a .rb extension, basically any computer with the most basic text editor.


How would you run a Ruby script from the command line?

First you need to make sure that you have Ruby enviroment and requested gems install and updated.

Go (cd ….) to the directory where the .rb file is located.

Then type: ruby name_of_file.rb and hit enter.

Category: Daily | Comments Off on Writing Methods

Enumerable

Why are modules useful?

As its name explain it it a separate and individual part of the code, and here are three of the reasons why it is useful:

  • Because you can group together: Methods, Classes, and Constants. In a single module
  • It prevent a name space and prevent name clashes.
  • Modules implement the mixin facility.

What does #each do?

Calls the given block once for each element on it self. One example is to print the list of countries that I travel to:


What does #each return?

As you can see on the example above, it will pass the element as a parameter.


What does #map do?

This method takes an enumerable object and a block, and runs the block for each element, the original object is not change unless you use .map! for a better grasp of what this means see the code below.


What does #map return?

As you can see on the example, this will return the product of each element executed by the code on the block.


What does #select do? & What does #select return?

It takes an array and processed in a block with specific filters or rules coded on a block, it will return a new array containing all elements of the previous array for which the given block returns are true.

Now en English and with one example, Imagine that you have an array with number from 1..10 and you want to get those that are divisible by 2, in this example we will use the method .select to do just that.


What is the difference between #each #map and #select?

They are tools that do different things, when given the same array:

.each : will return each element of the array

.map : will process each element with some coded instructions and return the product.

.select : will filter each element to return those  that pass the coded requirements.

Category: Daily | Comments Off on Enumerable

Blocks

How is a block like a function?

The way that I see a block as a function is because both can be call and you can pas parameters to both.


How is a block different from a function?

One of the major differences that I see is that you can pass a block to a block but you can not pass a function to a function.


What are the two ways to declare a block?

The Ruby standard is to use braces for single-line blocks and do..end for multi-line blocks.


How do you return data from a block?

Ruby blocks will return the last line by default, so make sure that you put on the last line what you want to return.


What happens if you include a return statement in a block?

It will return that line instead of the last line


Why would you use a block instead of just creating a method?

Because blocks can be pass to other blocks and to other methods, and methods no


What does yield do?

Will pause the method and execute the code on the block, after that it will return to the method


How do you pass arguments to a block from within a method?

putting the parameters that you want to pass between pipes.


 

 

 

Category: Daily | Comments Off on Blocks

Iteration

What are the two ways to denote a block of code?

The standard in Ruby would be to use Braces for single-line blocks and do..end for multi-line blocks. keep in mind that the braces syntax has a higher precedence that the do..end syntax, here are some examples:

Using the do..end

but remember that you can pass parameters, so in the following example we will pass a parameter.

I will be honest with you, this do not look that interesting, because we can do the same thing with methods, but the problem with methods is that you can not pass a method to another method, but you can pass a block to another block, another use would be if you need to open a file write information and close the file, we will use this file case on the next example.

 

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

What is an index variable?

This are used on loops, as we know a loop is a chunk of code that will run a number of times until some conditions are met,  in most of the cases we use a variable to keep track in what run of the loop you are and the loop will stop when the variable is equal to the set value. in the example we have one taxi that can sit 5 passengers,  so we will seat 5, and then we will tell to the other passengers to wait for the next taxi.

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

How do you print out each item of a simple array [1,3,5,7] with:
while?

for?

#each?

#times?

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

What’s the difference between while and until?

While will run as long as the condition continues to be True, Until in the other side is false and will stop the loop when the condition became True.

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

How do you stop a loop?

The loop will stop when the condition is met. also you can stop it using break.

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

How do you skip to the next iteration of a loop?

Here we need to use an example, the following loop will print numbers from 1 to 10 but if the number is even, i don’t want to run the rest of the code of the loop, instead I want to skip to the next iteration of the loop.

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

How would you start the loop over again?

you have two options use the redo and retry, but be careful you can end up with an infinite loop.

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

What are the (basic) differences between situations when you would use while vs #times vs #each?

this are the answers that come to my head right now, if in the future I might expand on it:

While: I would use this one as long as the condition is false, and when the condition is true, then it will end the loop.

Times: I would use this when I know beforehand how many times I will need to run the code.

Each: I would use this when I know that the number of times is stored on an array, variable, ect

–  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  –  – – –  –  –  –  –  –  –

What does nesting loops mean and when would you use

Nesting loop means that you put a loop whiting a loop, and you use it when you need a loop whiting a loop. I know this answer is kind of stupid, jajaja, but let me give you an example, in the prep work we have one code that is the logic game with the following rules.

10 friends are sitting in a circle around a table and decide to play a new game. In it, they count up through the numbers from 1 to 100. The first person says “1”, the second says “2” and so on… but with a few catches:

  • Whenever the number is divisible by 7, they switch directions. So person 6 will say “6”, person 7 will say “7”, then person 6 again will say “8”.
  • Whenever the number is divisible by 11, they skip the next person.

Your job is to pseudocode a program which will determine which player says the number 100.

to do this I used two while’s nested whiting a while. see the code below.

To see all the outputs line by line, Un-Coment lines: 26, 50, 73. and then run the code again.


how to use UNLESS

 

Category: Daily | Comments Off on Iteration

Conditionals and Flow Control

What is a “boolean”?

A boolena is a value that can be True or False, this is a great tool to find out if things are or are not.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are “Truthy” values?

Mmmm as we know booleans are True or false. but sometimes we want to use a non-boolean value (integers, strings, arrays, etc.) in a boolean context, because programing languages like “RUBY” are software too, that means the people who built Ruby had to decide what is truthy, and what is falsey, in ruby only false and nill are false, everything else is truthy (even “0” is truthy)

here is a simple example

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Are nil, 0, “0”, “”, 1, [], {} and -1 considered true or false?

well we will do this the hard way, that  meas we will test each one.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

When do you use eleseif?

you use this when you have a second condition in case that the first was false. (there is no limit in how many elesif you can use) so it can work for 3rd and up conditions, lest think of one example.

the next program, ask you for what place did you cross the finish line, if you put 1 you won Gold, if you put 2 you won Silver, if you put 3 you won Bronze, any other number will tell you thanks for your participation.

I did not include the output because, it depends on the input that you provide, please run this code on your terminal to see how it works.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

When do you use unless?

this one executes the code when the conditional is false, or in other words, all values will be true but the one that you chose, and here an example.

This sofware ask what is your name, whatever the name is it will tell “Welcome to Hogwarts” but if you type “voldemort” you will will get the Expecto Patronus spell.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What do || and && and ! do?

This are logical operators, “||” means “or” this means that you have two (or more) options that will pass the test. In the example on company is looking for programmers, but in order to be hire you need to have a bachelor degree or work in programming before. see example below

now lest take a look to &&, this operator require that all the test passes if one do not pass well the whole thing = false. we will use the same example, we will change the || for && and you will see that in this case unless you type “y” in both questions you will be left out.

now lest use “!”, this operator is call logical NOT Operator, is used to reverse the logic stated, this means that if a condition is true this operator will make it false. yea yea yea I know this sound a little complicated and for that reason we have examples, in the following example I use the code form the && example, but I need you to take a look at the line that contains the IF, I add the “!” and trow some parenthesis. check the code in both examples and then, run it on you terminal. you will see what happen.

see this company whant people with NO experience and NO studies. if you answer y to one questions you are out.

Category: Daily | Comments Off on Conditionals and Flow Control

Other Random Stuff

What is nil?

nil is a special Ruby data type that means “nothing”. It’s equivalent to null or None in other programming languages

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

How do you check if something is nil?

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

What’s the difference between nil and blank and empty?

nil = is an object (a single instance of the class NilClass) so methods can be called on it. nil? is a standard method in Ruby that can be called on all objects and returns true for the nil object and false for anything else.

blank = Is not a standard Ruby method but is added to all objects by Rails and returns true for nil, false, empty, or a whitespace string.

empty = is a standard Ruby method on some objects like Arrays, Hashes and Strings. Its exact behaviour will depend on the specific object, but typically it returns true if the object contains no elements.

Note:

Because empty? is not defined for all objects you would get a NoMethodError if you called empty? on nil so to avoid having to write things like if x.nil? || x.empty? Rails adds the blank? method

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

Are the following nil or empty?

Well I will do this the Hard-Way, I will test it by my self and here are the results.

 

" " 

 

"" 

 

[

 

[""

 

{

 

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

What’s the difference between puts and p and print?

I don’t know but lest find out, I will create a variable = “Ayumi Hamasaki” and I will print it 3 times and see what happen.

Puts

 

print

 

p

Well it looks like “Print” does exactly what is requested and NOTHING MORE, while “Puts” does the same but include an extra line at the end, I like it is like giving a little extra, and “P” prints the whole object.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

What does inspect do?

Acording whit what I found: Returns a string containing a human-readable representation of obj.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

What do +=, -=, *= and /= do?

This are very useful, because it allow you to save some time typing. Example, I have a bank account with 136 dollars and I make a deposit of 15 dollars the total would be 151, but this dollars are in the same account the balance just change. I will show two ways to do it, one using the Long method, and the other one using +=.

as you can see using the += you don’t have to type as much, the other ones work the same way.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Category: Daily | Comments Off on Other Random Stuff

Dates & Times

How do you get the current date and time?

More information in how to get Time related data.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you create a Time specifically for 12/25/2013?

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you find how many days have passed between two Time‘s?

This answer I need to fix it latte because I have a error of 0.0000000019 seconds, but this is how I did it.

 

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What’s the difference between UTC and GMT and Local times?

There is no time difference between Coordinated Universal Time (UTC) and Greenwich Mean Time, Local time in the other hand that is whatever the current local hour is at your current location, example Im in Salt Lake City UT. here the time is 9:35 Pm but guess what, in Greenwich England is 4:35 Am of tomorrow.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How would you find out the time that was 100 seconds ago? 10 days ago?

For this I will use a portion of my code used on the previous question. for the 100 seconds I will deduct 100 seconds from the current time, and for the 10 days I will subtract 864000 seconds, see example.

 

 

Category: Daily | Comments Off on Dates & Times

Hashes

What is a hash?

Well a hash is similar to an array, but in the array we use a numerical index that starts from “0” as a key locate  items stored, in the hashes we choose a “key word” to use as index to find the items (Values) stored.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are keys and values?

Keys are the index that we use to access the item (value) stored on that specific part of the hash. lest create a very simple hash with a single element as example:

More clear not even the Coffee.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How is a hash similar & different to an Array?

Both uses similar ways to storage information, but the main difference is that the Array uses set numerical index starting with “0”, and Hashes you can use a word of your chose as a key in the index.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are 3 ways to create a hash?

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is the “hash rocket”?

Is the simbol(s) used to link the key with the value: “=>”

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you access data in a hash?

I work better with examples, in the following example I create a Hash with some information about my favorite singer: name =  Ayumi Hamasaki, date of brith = 10/02/78,  country = Japan. after that we wan to access and print her name.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you change data in a hash?

Lest imagine that one day I decide to change my favorite singer (that could happen), well lest imagine that the chosen one is Kim Hyo-jung AKA “Hyorin” with a date of birth 01/11/1991, and form South Korea.

Now I need to change the values on my Hash with the information of Hyorin. well not a problem this is how we do it.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What types of data are good to store in a hash?

I thing that you can store any kind of data types.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are options hashes?

This are used when need to pass Optional arguments to a method, one way to do it is with the following example

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you delete data from a hash?

Lest Imagine that one day Ayumi Hamasaki call me to thank me for been a loyal fan (she do not know about Hyorin) and also would like me to delete her birth day information from my hash, you know ladies and the age thing, me been a gentleman decide to delete such inadequate information.

Here we will use the “.delete” method

and its done, the misery of her age has bean.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you add hashes together?

Now lest imagine that I have another hash with more information, it have one of my favorite songs from Ayumi Hamasaki. I want to add those two hashes in a single one, this is one way to do it.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you add New data to a Hash?

This question is not in the questionnaire but I think that should be include in this basics.

In the next example, we want to add the name of one of my favorite songs, “Moments”

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you list out all the keys or values?

this is a very simple one, in my hash with Ayumi Hamasaki information, I want to know what are the keys (titles) of the information. here is how is done.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you see if the hash contains a key or value?

This are two question on one, so lest answer them, Imagine that Hamasaki calls me and ask me: in the hash did you have a section for name? well this is how we find out:

WARNING – WARNING: hash keys are case sensitive. you know what it means.

Then she ask: Does the hash have a section for weigh? you know how ladies are about this

Now is time to find about values.

Then she ask: does the hash says that Japan is my country?   you know japanese very patriotic, you don’t want to get that one wrong. (this is how we check)

and finally she says: you know some people have my b-day wrong, some websites show my b-day as 07/07/77 but this is wrong, can you make sure that value is not in the hash?

she is happy all questions have been answer.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Printing all the information of the hash

This is not a question but I think that would be good to know this.

If Ayumi want to know all the information in the hash, and want it on an organize way, this is one way to get it done.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is a set?

Set are collections of unorder values whit no duplicates, this is a gem, is like an array but have some methods that makes the interactions a little easier, in several cases they can be a substitute for a hash, or an array.

Category: Daily | Comments Off on Hashes

Arrays

What are three ways to create an array?

As we know there are several ways to create an array and here are three of them.

this is the most simple way, to create one, this is an empty array

this is the same that the previous, but this one have only 20 slots of storage.

You can assign a value to each element in the array as follows:

Another way is:

here are other examples:

The Kernel module available in core Ruby has an Array method, which only accepts a single argument. Here, the method takes a range as an argument to create an array of digits:

well you got the idea, there are several ways to create an array, so choose wisely.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What should you store in arrays?

Arrays can hold objects of any data type, so you should store whatever you need.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How to know if the variable is an array?

Here I will use the    puts “{array_name}”    example below

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you prepopulate the array with default data?

To do this just set the values at the moment of the creation.

examples

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you access items in an array?

firs we might need to get some information for the array and we can do this the following way:

now that we know that we have some data there, if we want to access it we use the numeric index of the elements.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How can you see what is in one array?

Well this questions is not in the original questions but for beginers some times we want to see what is inside of an array

in the following example we create an array and insert the values from 7 to 12, then we see the whole array and then we print the each element of the array.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How can you access a specific group of items in an array?

here we will use the same Array than in the previous example, but we will access and print the two names in the middle.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you modify the items in an array?

Here we will use my favorite singers Array again but the last singer “Anahi” I want to change that for “Haruka Kodoma” one way to do that is with the following code.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is the difference between push/pop and shift/unshift?

They do the same, but push/pop access the last item on the array, while shift/unshift access the first item of the array.   and of course here are the examples

here we will show how push and unshift works using my favorite singers array:

Now pop and shift

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is the shovel operator?

Mmmm this is a fun one, basically this could be use as a easy way to concatenate two strings. see examples

Another example using my singers array, the first item on the array is Ayumi but her last name is Hamasaki now I need to make sure that her name is complete, or in other words we will shovel her last name.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How is > arr.pop different from > arr[-1]?

well this are very different because  arr.[-1] will access the last item in the array and arr.pop will delete the last item on the array here is an example with my favorite singers.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How is pushing or <<ing another array into your array different from just adding them together?

I don’t really know if this the correct answer but what I discover is that Shavoling (<<) works just fine while Push gives me an error, I will study more and update this answer. see example below

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you delete items in an array?

As always there are several way, here are three of them.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Why should you be careful deleting items in an array?

Well I think that this one is obvious, unlike the PC we do not have a garbage bin where we can go a retrieve anything that we delete by mistake,  here once that is deleted, well is GONE, no way to get it back.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How can you convert arrays to strings?

I use the .to_s method, in the follwoing example I create an Array with numbers 1, 2, 3, 4  and play with them. see the example

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How can you convert from other data types to arrays?

I know of the method    split()  in the first example we create a variable with a full name, name = “Ayumi Hamasaki Karinaya” then we make sure that this sure that is a string, then we split it ant put it back in the same variable, but now as an array. see how is done

on the last example the string was cut whenever find a space and take that as an individual element for the new array, but what if you have the name of some cities like Los Angeles CA  or Dallas TX and you want each city with its respective state as a single item on the new array, well we need to find a new breaking point for the string and also tell to the computer what character(s) will be the new cutting point, to make this more easy to understand here is an example.

See it, clear as Coffee,

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How can you figure out if an array contains a particular value?

This is a great excuse to bring my favorite singers array, jajaja

what we will do here is create an array with the name of some of the singers that I listen when I code, then we will run a simple test to see if the array have it or not, we will do two runs in one we will find what we are looking for in the second one we will not find it.

NOTE; (“WARNING”): This method is case sensitive, so if you are looking for “Yui” but the array have it as “yui” this will not find it. (you been warned)

Now lest look for “Koda Kumi”

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you find the biggest item in an array?

Here is one way to get it done, firs we create an array with the values form 10 to 17 so we already know that the max value is 17, then we create a new variable call max_element and we deposit the maximum value in that variable, then we print it, ready, let code it.

now lest find the small-est number using the same code just change “.max” for “.min”

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you remove any duplicates from your array?

Here we will use the method “.uniq”, please see the example to see how it works.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How to you find out how big an array is?

To do this we will create an array with the values from 1 to 10 so we know already that the correct answer, so the method that we will use is “.length” see how it works

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you put an array in order?

To do this one we will use the method “.sort”, first we will create an array wit mix numbers, print it, the we organized it and print it again.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are the naming conventions for arrays?

I did not find information specifically about arrays, bu I know that the way to name variables is using the rules of Snake_Case:

Snake case (or snake_case) is the practice of writing compound words or phrases in which the elements are separated with one underscore character (_) and no spaces, with each element’s initial letter usually lowercased within the compound and the first letter either upper or lower case—as in “foo_bar” and “Hello_world”. It is commonly used in computer code for variable names, and function names, and sometimes computer filenames. At least one study found that readers can recognize snake case values more quickly than camel case.

Category: Daily | Comments Off on Arrays

Strings

What’s the difference between single and double quotes?

There is no much difference, but when you start with one you should end with the same kind, and you can use them include the opposite one in a string without escaping it. example:

The other important difference is: only double-quoted strings perform string interpolation (i.e. value insertion). Single-quoted strings leave everything as it is:

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is string interpolation?

The way that i understand it is the ability to include a variable to be posted withing a string, one simple example is:

this code will print:  “I have 4 apples”

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are Escape Characters?

Is a way to tell to the computer that the following character do not execute it as a special character on Ruby, just read it as a regular character. mmmmmmmm????

Ok to make more understandable here is an example: you want to print: puts  ‘Alex I’ll be there in 5 minutes’

the problem here is that the come in  I’ll will end the string, but you want it to be printed just like that in that case you write:

did you notice that I use the character \ before the ‘  this tells to ruby, just print it for what it is.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are Line Breaks?

basically what its name says it break the text to the next line, one way to get it done is using: \n  example:

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you make other things into strings?

Use  to_s  examples:

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you concatenate strings?

there are several ways, the most basic is:

Here is another way

 

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you access a specific character or substring?

To do this we will use .index this will help us to find the location of the first occurrence of a specific character or sub-string. example:

once that we know the location we can do whatever we want with this.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you split up strings into arrays?

Imagine that we have the string name = “maximus decimus meridius” and you want to separate each part on a different line and the character will be he empty space you do it like:

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How are strings and arrays similar?

They are very different, but if similitude is what we are looking for, well both can hold words, letters, or characters.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you get and clean up user input on the command line?

So far the only way that I have do this is using the “gets.chomp”

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What does it mean that strings are “mutable” and why care?

Mutable strings are strings that can be changed, and if we do not pay attention we can alternate the string and adding more to the string.

one example is:

as you can see in the first example the string is mutating with each pass on the loop, so we care because we can end up with one string that has not what we intend.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is a symbol?

A Symbol is the most basic Ruby object you can create, it is just a name and an internal ID, basically is the name of something. They can be many things, like strings, arrays, objects. etc….

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How is a symbol different from a string?

The truth of the matter is that Symbols are Strings, just with an important difference, Symbols are immutable. Mutable objects can be changed after assignment while immutable objects can only be overwritten.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How can you center or right-justify a string?

The next example will show one way to accomplish it.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Category: Daily | Comments Off on Strings

Numbers, Operators, and Expressions

What is the difference between an integer and a float?

basically is that one have decimal point and the other not.  example:

interger = 1 or 5 or 715

Float   = 1.15 or 2.78 or 3.1416

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Why should you be careful when converting back and forth between integers and floats?

Because on the transition values can be lost and for that the equation will have an error.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What’s the difference between =, ==, and ===

=      –>   Set the value, example:   name = jose

==    –> Checks if the value of two operands are equal or not, if yes then condition becomes true. example:  (3 == 5) is not true.

===  –> Used to test equality within a when clause of a case statement. example: (1…10) === 5 returns true.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

How do you do exponents in Ruby?

using the the simbol ** see example:

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is a Range?

Ranges are used to express a sequence, sequences have: start point, end point, and a way to produce successive values in the sequence, in Ruby the sequence is created using “..” and “…” range operators, the three dots will exclude the highest value.  here are the examples.

This example explain, how to create a range and the ways to create a range.

Category: Daily | Comments Off on Numbers, Operators, and Expressions

Ruby

 

What attributes of Ruby help it encourage “programmer happiness”?

In an interview on Sep 29, 2003 Yukihiro Matsumoto provide the following answer:

Bill Venners: In an introductory article on Ruby, you wrote, “For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, So Ruby is designed to make programmers happy.” How can Ruby make programmers happy?

Yukihiro Matsumoto: You want to enjoy life, don’t you? If you get your job done quickly and your job is fun, that’s good isn’t it? That’s the purpose of life, partly. Your life is better.

I want to solve problems I meet in the daily life by using computers, so I need to write programs. By using Ruby, I want to concentrate the things I do, not the magical rules of the language, like starting with public void something something something to say, “print hello world.” I just want to say, “print this!” I don’t want all the surrounding magic keywords. I just want to concentrate on the task. That’s the basic idea. So I have tried to make Ruby code concise and succinct.

Bill Venners: Allowing programmers to write code that’s concise and succinct is one way to make them happy.

Yukihiro Matsumoto: Yes, so they can concentrate on the problem itself. Sometimes people jot down pseudo-code on paper. If that pseudo-code runs directly on their computers, it’s best, isn’t it? Ruby tries to be like that, like pseudo-code that runs. Python people say that too.

Bill Venners: Yes, Python people do say that Python is executable pseudo-code. What else is in Ruby to make programmers happy?

Yukihiro Matsumoto: In our daily lives as programmers, we process text strings a lot. So I tried to work hard on text processing, namely the string class and regular expressions. Regular expressions are built into the language and are very tuned up for use. We also need to call into the operating system a lot. Ruby can call every system call in Unix and most of the Windows API. This brings the power and function of the operating system to the interpretive language environment. So you can do daily systems administration and text processing programming. That’s the major domain of at least my life, so I worked hard on making that good.

Bill Venners: So basically Ruby helps me enjoy my life by helping me get my job done more quickly and with more fun?

Yukihiro Matsumoto: It helps me do that. I’m not sure Ruby works for you, but I hope so.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is an “Interpreted Language”?

An Interpreted Language is a programming language for which most of its implementations execute instruction directly, without previously compiling a program into machine-language instructions. The interpreter executes the program directly, translating each statement in a sequence of one of more subroutines already complied into machine code.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is IRB?

Interactive Ruby Shell (IRB) is a REPL for programming in the object-oriented scripting language RUBY, or in other words is a shell that is able to run the ruby code, one use for this is so you can run and test some lines of code without having to run the whole program, just to make sure that this specific part work how it should.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are Objects?

Mmmm this is a tricky one,

Objects are key to understanding object-oriented technology. Look around right now and you’ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.

Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: “What possible states can this object be in?” and “What possible behavior can this object perform?”. Make sure to write down your observations. As you do, you’ll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation — a fundamental principle of object-oriented programming.

By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.

Bundling code into individual software objects provides a number of benefits, including:

Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
Information-hiding: By interacting only with an object’s methods, the details of its internal implementation remain hidden from the outside world.
Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

under this logic almost everything ins Ruby is an Object.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are Methods?

Ruby methods aer very similar to functions in Javascript and other programming languages, Ruby methods are used to bundle one or more repeatable statements into a single unit.

The following would be a template for a Method:

Methods names should begin with a lowercase letter, and should be defined before calling them. One more thing about method is that they will return the last line that is on the method, and you can pas parameters to a method. in the following example the software will ask if you are happy? and you can answer with y/n and depending on your answer it will return a song that will fit your mood.

now lest get a little more creative, because methods context can be stored on a variable. and you can execute commands on that variable, I know it sounds ….. but how a bout an example, in the follow example we use the same code before, but we will save the method on a variable and then we will change the return value in Upercase.

Now I need to tell you that I know about two different kinds of methods: “Class Method”  & “Instance Method”  and here are the examples.

 

 

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are classes?

Classes in Ruby are First-Class Objects, and each is an instance (object) of the class “CLASS”, I know that sounds cheese, and redundant, ok to make it easy I should use an example:

a class is a template from where you can create unique and different copies, but you might think a copy should be an exact replica, and yes, but what if you live in New York City and your job is the creation of the new Driver Licenses for all the 4.5 Million Driver licence holders, ideally you would like to make all of them with similar characteristics like: size, size of the picture, color, etc, and information like: Name, Address, Birthday, Expiration. etc… you got the idea.

what it would be a complete disaster if all the Drivers Licenses are exactly the same whit the same picture, and same information, also a similar bad situation would be if you have to create all of them one by one, in this case having a Template (in ruby = CLASS) would be very useful.

in Ruby a class can contain Methods inside, and if you use instance variables, you can use the same variables but with different values each time.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 What are Blocks?

A block in Ruby is very similar to a Method, it can take some arguments and run code for those, but a block can be passed to a Function as an argument, and gets call when using the Key workd “yield”, in other words blocks are a way to create Proc Objects that contain code that can be use by other code,

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

what is an Array?

An array is a list (storage) of items in order, the order is given by a numerical index  “0, 1, 2, 3, etc..” this kind of storage can hold: Strings, Integers, Hash, Fixnum, Symbols, and even other arrays.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What is an Iterator?

Iterators are methods that return all elements of an Array or Hash, and for that reason they can be very useful to create loops.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are Hashes?

Hashes are like Arrays, for me the main difference is, Arrays are numerical index “0, 1, 2, 3, ….” and that number is the key to locate the what is storage on that specific location, in hashes instead of using number you can use words like: name, last name, age, etc… and to use the information on that specific container you call it by its key word.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

What are Gems and Libraries?

for me this two are almost the same thing, basically people more smart than me and with way more time, decide to create an extencion to the Ruby language that will make some projects way way easier.

one example would be the calculation of the square root one way would be to code the following example:

or you can use a library and write:   Math.sqrt(8)  and be done.

 – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Category: Daily | Comments Off on Ruby

Ruby: Loops & Iterators

The information posted here is not 100% mine, because I’m learning and getting examples from the internet, this is a compilation of examples that will help me to have a quick reference to this information.

 

Infinite Loop

I do not know why we have an infinite loop, but in case that I need it in the future here is the pimpliest of the loops, it might be useless but here it is:

This will just infinitely print out HELLO, not particularly useful, but if you ever need a simple way to create an infinite loop, there you go :).

 

 

Loop do

This is a very simple loop that will get the job done:

This will print out the numbers from 1 to 10 all on the same line. Since we are using the break keyword, the loop will exit when the value of i hits 10. and the result will be as follows:

 

 

While Loop

This will print out the numbers from 1 to 10:

 

Until Loop

Similar to the while loop, but with reverse logic.

and the result will be as expected:

 

Redo Keyword

Here is another variation that can create an infinite loop, basically this will allow to restart the loop from the begging  without evaluating the condition again, and will end up in an infinite loop:

This will keep incrementing the value of i and will keep printing it out:

 

Begin Loop

This assures that the code will run at least once even if the value do not have the requirements:

We can do something similar with until:

This will print out 10 and then exit the loop as the exit condition will be reached.

 

The For Loop

or values in an array:

Both of these will once again print out the numbers from 1 to 10.

 

The Each Iterator

example 1:

this will print the numbers from 1 to 10.

example 2:

same output.

Here I create the Fizz & Buss game as an example:

 

 

The Times Iterators

Use a times iterator with an iteration variable.

Another example:

and the result of the last one is:

 

The Upto And Step Iterator

Very similar to the For Loop, here is an example:

This will print the numbers from 1 to 10.

 

The Each_Index Iterator

Sometimes we have an array and we don’t want to loop over every value but rather want to loop over every index, this is where the each_index iterator comes in handy:

The block gets the index of the array on every iteration and we can then use that to print out the value (not useful but illustrates the point):

As expected we printed out all the values in the array, it is 1 to 10 but 10 times bigger for extra awesomeness :).

Category: Daily | Comments Off on Ruby: Loops & Iterators

Array List organizer

One more example on how the Ruby syntax works.

In this example we will use arrays, to storage information,  here we learn that we need to call the function after it has been Defined (Created), and then that can be call.

Let’s write a program which asks us to type in as many words as we want (one word per line, continuing until we just press Enter on an empty line), and which then repeats the words back to us in alphabetical order.

 

Category: Daily | Comments Off on Array List organizer

Leap Year

This is part of my learning to code on Ruby. so far is a good language, I code long time ago with: Java, C++, HTML, Javascrip, CSS. and one thing that Ruby has diferent is that we do not close the lines, in most other languages you need to use ” : ” or ” } ” or something to tell the computer that the line is done, but here, is not need, and this simple thing is taking time to settle ad the Ruby way.

Here we have a FUN ONE, basically write a program were you provide two years: it calculates and print the leap years in between including the years given.

This is the one that I create:

when using this code I got the followin results:

 

 

Category: Daily | Comments Off on Leap Year

Grandma – the code

Here we have to create a program with the following instructions:

Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?!  SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. (This part is optional, and would be much easier if you read the section on Ruby’s random number generator at the end of the methods chapter.) You can’t stop talking to grandma until you shout BYE.

here is what I CODE.