Programming With Ruby Episode 8, Hashes

Covered In This Episode:

  • What are Hashes?
  • Hash Creation
  • Hash Accessing
  • Hash iteration
  • Hash Methods

Transcript:

Hello Everybody and welcome to Programming With Ruby Episode 8,
Hashes. I’m Tyler,and this video is brought to you by, manwithcode.com

In this episode I will be telling you what hashes are, how you can
create them, access them, and iterate over them. Finally at the end I
will show you some useful methods that hashes have.

What Are Hashes?

Hashes are like arrays, which you learned about in the previous
episode. Except that they have no order, and instead of being accessed
by an index number, they are accessed by what is called a key (which
can be anything). That key points to a value, which is the actual data
inside the hash

Arrays are used for lists of data, where hashes are used to relate
data. I may have a hash where the keys are my friend’s names, and the
values are their birthdays.

Hash Creation

Similarly to how arrays are created with square brackets, hashes are
created with curly brackets:

my_hash = {}

Items are defined like this:

birthdays = {"Amy" => "May", "Dakota" => "January"}

Each key-value pair are separated by a comma, and their relation is
defined with =>

Accessing Hashes

You can access hashes in almost the same way you access arrays, except
by using the key value, instead of an index number:

brithdays["Amy"] #=> May

You can define new keys and values:

birthdays["Zack"] = "April"

Iterating Over Hashes

Hashes have an each method like arrays do:

my_hash = {"cat" => "hat", "dog" => "log"}
my_hash.each do |pair|
	puts "Key: " + pair[0]
	puts "Value: " + pair[1]
end
#=> Key: cat
#=> Value: hat
#=> Key: dog
#=> Value: log

They also have each_key and each_value which let you iterate over each
key or value in a similar way.

Useful Hash Methods

delete(key) deletes a key-value pair
empty? True or False if the hash is empty
keys returns the keys
values returns the values
size how many key-value pairs there are

That wraps it up for this episode!

Please donate, or I will stop making these videos. There should be a
link to donate to the right of this video.

If you have any questions or comments leave a comment on this page or
email me at tyler@manwithcode.com

Thanks for watching, goodbye!

Programming With Ruby Episode 7, Arrays

Covered In This Episode:

  • What Aarrays Are
  • Array Creation
  • Accessing Arrays
  • Array Iteration
  • Other Array Methods

Transcript:

Hello everybody and welcome to Programming With Ruby Episode 7,
Arrays. I’m Tyler. And this video is brought to you by manwithcode.com

In this episode I will be telling you what arrays are, how you can
create an array in ruby. How to manipulate arrays by accessing them,
iterating over them, and by showing you a few useful methods they
have.

Lets get started!

What are Arrays?

Arrays are a type of variable. Think of an array as a list. This list
can hold anything, names, numbers, objects, anything. Objects in the
array have a number, depending on what place they are in the
list.

Because computers start counting at 0, the first element in the
array is 0, instead of one.

Array Creation

This is how a variable is defined in ruby:

x = []

This is an empty array, if we wanted an array with something in it:

todo_list = ["Cut Grass", "Buy Food", "Fix Tom's Computer"]

Each bracket represents the start and the end of the array,
respectively. Each item in an array is separated by a comma.

Accessing Arrays

Now that you have created an array, how do you go about accessing each
item?

I told you earlier that each item had a number, so to access the first
item in the array you would do:

todo_list[0] #=> "Cut Grass"

You can also add items to an array in a similar way

todo_list[3] = "Go Skydiving"

Another way to add items is to use +=, which you may recognize from
previous tutorials

todo_list += ["Eat Sandwich"]

Don’t forget that if you use += that the item your adding has to be
between brackets

You can also use ranges to access elements in the array. ranges are
used in a similar way that you normally access arrays, except ranges
look like this:

todo_list[0..2] #=> ["Cut Grass", "Buy Food", "Fix Tom's Computer"]

The 0 is the start number and the 2 is the end number. you can also
use -1, which is the end of the array:

todo_list[3..-1] #=> ["Go Skydiving", "Eat Sandwich"]

Array Iteration

If you want to loop over each element of an array you use the each
method:

numbers = [1, 2, 3, 4]
numbers.each do |number|
puts number * 2
end
#=> 2 4 6 8

You can do the same thing, but turn the output into an array with the
collect method:

numbers = [1, 2, 3, 4]
numbers.collect do |number|
number * 2
end
#=> [2,4,6,8]

Other Array Methods

Now I’m going to show you some useful methods arrays have!

empty? tells you if an array is empty
sort sorts the array (alphabetically)
reverse reverses the array
delete deletes a specified item from the array
delete_at deletes whatever is at that index in the array
find finds an item with the specified value
inspect returns the way an array looks in code, instead of its values, this is useful for puts my_array.inspect
length how long the array is

That’s it for today’s episode

Please don’t forget to donate, the link should be to the right of this video

If you have any questions or comments, leave a comment on this page or
email me at tyler@manwithcode.com

Thanks for watching, bye!

Programming With Ruby Episode 6, Strings

Covered in this episode:

  • String Literals
  • String Expressions
  • String Methods
  • Regular Expressions
  • Getting User Input (gets)

Transcript:

Hello Everyone and Welcome to Programming With Ruby Episode 6,
Strings. I’m Tyler, your presenter. This is brought to you by
manwithcode.com

In this episode I will be telling you what string literals are. I will
show you expressions you can use with strings, which are similar but
still different than expressions with numbers. I will show you useful
methods strings have. I will show you how to use regular
expressions. Finally I will teach you how to get input from the
user.

On to the Code!

String Literals

According to wikipedia, string literals are the representation of a
string value within the source code of a computer program. For
example:

puts "Hello World" # Hello World is the string literal

String Expressons

The only string expressions are the plus and multiplication sign. The
plus sign connects strings together, the multiplication sign repeats a
string a certain number of times.
Let me show you how it works:

"Hello " + "World!" #=> "Hello World!"
"Hello " * 3 #=> "Hello Hello Hello"

String Methods

Here are some useful String methods:
empty? tells you if you are dealing with an empty string
length tells you how long a string is
each_char lets you iterate over each character
capitalize capitalizes the first character
upcase makes all characters upper case
downcase makes all characters lower case

Regular Expressions

Regular expressions are a way to match elements in other strings. It
is easier to show you than to describe to you, so here we go!

The simplest is substitution:

"Hello World".sub("Hello", "Goodbye")
#=> "Goodbye World"

But if you have more than one hello:

"Hello Hello Hello".sub("Hello", "Goodbye")
#=> "Goodbye Hello Hello"

This happens because the sub method only replaces the first occurrence
of “Hello”. The gsub method fixes this:

"Hello Hello Hello".gsub("Hello", "Goodbye")
#=> "Goodbye Goodbye Goodbye"

What if you want to manipulate parts of a string using regular
expressions. The scan method is what you want!

# /n means new line
"Who are you".scan(/../) { |x| puts x }
#=> Wh\no \nar\ne \nyo
# With no whitespace:
"Who are you".scan(/\w\w/) { |x| puts x }
#=> Wh/nar/nyo

Regular Expressions are a vast topic that I can’t completely cover
here, so do a Google search to find out more.

Getting User Input

You can get user input with the “gets” method:

a = gets
# The user inputs: I like pie
puts a #=> "I like pie"

That wraps it up for todays episode.

Don’t forget to donate, the link is to the right of this video

If you have any questions or comments, leave a comment on this page or
email me at tyler@manwithcode.com

Bye!