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!

Programming With Ruby Episode 5, Numbers


Covered in This Episode

  • Numbers
  • Expressions
  • Types of Numbers
  • Constants (New type of variable!)
  • Doing something a number of times

Transcript

Hello Everyone and Welcome to Programming With Ruby Episode 5,
Numbers. Its sitll presented by me, Tyler. And brought to you by
manwithcode.com

In this episode I will be talking about numbers in Ruby. I will go
over expressions, the different types of numbers, I will introduce a
new variable type called a constant, and will show you how to repeat
an action a certain number of times in Ruby.

Lets get started!

Pop open you Command prompt or Terminal and start Interactive Ruby. If
you have forgotten, just enter ‘irb’.

First item is expressions.

2 + 2 #=> 4
2 - 2 #=> 0
3 * 3 #=> 9
6 / 3 #=> 2
2 ** 5 #=> 5

expressions with variables work in the same way

x = 5
5 * x #=> 25
x ** 4 #=> 625

If you want to do math on a variable and set the value of the variable
to the result use:

x = 5
x *= 2 #=> x = 10
x -= 4 #=> x = 6
x /= 6 #=> x = 1

In Ruby there are 3 different types of numbers. Fixnum, which are
32-bit numbers. Floats, which are numbers with a value after the
decimal point. Bignums which are numbers larger than 32-bits.

5.class #=> Fixnum
0.421.class #=> Float
999999999999 #=> Bignum

In Ruby it is possible to separate up larger numbers to improve
readability in the code. Simply place in underscores

1_000_000 #=> 1000000

Lets say you are writing some code and you need to do something a
certain number of times. Use this code:

2.times do |x|
puts x
end
#=> 0
#=> 1

That wraps it up for this episode. If you have any questions or
comments, leave a comment in the comment box below. Or email me at
tyler@manwithcode.com

Don’t forget to donate. The button is to the right of this video.

Thanks for watching. Goodbye

Programming With Ruby Episode 4, Main Ruby Concepts


Covered In This Episode:

  • Basic Variables
  • Basic Methods
  • Basic Classes
  • Interactive Ruby (irb)

Code:

# Define the class Greeter
class Greeter
	# Define the method hello
	# This method greets the
	# user
	def hello(name)
		puts "Hello " + name
	end # End of the method hello
end # End of the class Greeter

Greeter.new.hello("Tyler")


Transcript:

Hello everybody and welcome to Programming With Ruby Episode 4, Main
Ruby Concepts.

Covered in this episode. We’ll be playing with interactive ruby (also

called irb). I will teach you about variables, basic methods, and

classes in Ruby. Lets get started!

Basics Description

Ruby is an object oriented programming language. object oriented
languages use objects. Ruby goes beyond most other object oriented
languages, because in Ruby, everything is an object. (I’ll show you
exactly what that means in a few minutes)

Object oriented programming sort of models real life. Look around you,
everything around you is an object. Your computer, your desk, books,
the moon, and people are all objects.

In programming, all objects have properties called variables. These
could be the color of the object, the weight, size, or any other kind
of property.

Objects also have methods (which are sometimes called functions). A
camera object would have a method to take pictures. A car would have a
method to drive. A printer would have a method to print.

As well as making code easier to understand, you will also continue to
appreciate other benefits of object oriented programming further on in
your programming career.

Example

I have written a very basic Ruby program. This is about what most
programs look like, just a lot simpler. Let’s break it down line by
line.

Everything followed by a hash mark (#), is ignored by Ruby. These are
called comments. In these comments you can describe what your code is
doing, make notes to yourself, and other such things.

The first line says “class Greeter”. This line tells Ruby that we are
now defining a class named “Greeter”. Please remember that all classes
in Ruby start with a capital letter!

The next line says “def hello(name)”. This line means we are defining
a function (def), named “hello”, that takes the parameter “name”.

The following line is a little trickier. ‘puts “Hello ” +
name’. “puts” means “put string”. our string is “Hello ” + name. Now
you’re probably wondering what the “+ name” is for. We’re aren’t doing
math on strings, but we are connecting the variable “name” to our other
string “Hello “.

The next two lines have the keyword “end”. The first end means we are
done defining the function “hello”. The second “end” means we are done
defining the class “Greeter”.

Below where the class “Greeter” ends we have the line ‘myname =
“Tyler”‘. This means are creating the variable myname and placing
“Tyler” inside of it.

Next is, the line “person = Greeter.new”. This means we are
instantiating a new greeter object named person.

Finally we have the line “person.hello(myname)”. This calls the
person object’s method “hello” and passes the variable “myname” as an
argument.

Now lets run the program. If you remember, to run a program you open
Terminal or Command Prompt, change directories into where you saved
your program and type “ruby programname.rb”. In my case I will type
“ruby episode4.rb”.
And there you have it, it says “Hello Tyler”

The code in this example will be available in the YouTube video
description, and below this video on manwithcode.com

Example 2
Back to the code. This code is actually a little longer
than it has to be. I did this so things would hopefully make more
sense. In reality we can change the last three lines to
‘Greeter.new.hello(“Tyler”)’

We can do this because in Ruby, everything is an expression. Here’s how
it breaks down. “Greeter.new” creates a greeter object. “.hello” calls
the method “hello” on that new object. And then we pass in the string
“Tyler” as an parameter.

Ruby has many other tricks like this to make your code shorter. Some
have disadvantages and others do not. One of the most important
disadvantages can be clarity or readability. You want your code to be
as easy to understand as possible.

irb
Interactive Ruby (or irb for short). irb makes it very easy to
quickly test out code, find out if/how something works, do basic math,
and write throwaway code you will only use once.

To open irb, open up your Command Prompt or Terminal and enter
irb. Alternatively, if you are on Windows, go into All Programs and
under Ruby will be a program name fxri which is an equivalent to irb.

In irb you can do basic math:

2 + 2 #=> 4
2**6 #=> 64
2 * 5 #=> 10

We can write any valid Ruby code. So we could write the greet method again.

def greet(name)
    puts "Hello " + name
end

And then call it:

greet("Tyler")

Remember earlier I said everything in Ruby was an object? I’ll show
you what I mean. if you type in the name of an object and call the
class method it will tell you its class.

0.class #=> Fixnum
"Hello".class #=>; String
String.class #=> Class

I encourage you to play around with irb for a little while, try
writing your own methods, and have some fun with it.

End
This brings us to the end of this episode, I hope it helped
you.  If you need any help, have questions or comments, leave a
comment below or contact me at tyler@manwithcode.com

Don’t forget to donate! There is a donation link to the right of this
video. On YouTube its in the description box. On my website, it is to
the right of the video.

Thanks for watching! Bye!