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!

Programming With Ruby Episode 3, Getting Help/Tools

Covered in this episode:

  • How to get help
  • Google
  • Ruby’s Documentation
  • Forums/Mailing Lists
  • Blogs on Ruby
  • Text Editors
  • Integrated Development Environments (IDEs)

Links:

Ruby-Lang: http://www.ruby-lang.org
Ruby-Doc:  http://www.ruby-doc.org/

Forums:

http://www.ruby-forum.com/
http://www.rubyforums.com/

Text Editors:
Notepad++ http://notepad-plus.sourceforge.net/uk/site.htm
SciTE http://www.scintilla.org/SciTE.html
jEdit http://www.jedit.org/
Text Mate http://macromates.com/

IDEs
Netbeans http://www.netbeans.org/
Aptana Rad Rails http://www.aptana.com/
FreeRIDE http://rubyforge.org/projects/freeride/
Geany http://www.geany.org/
More… http://rubyforge.org/softwaremap/trove_list.php?form_cat=65

Transcript

Hello Everybody! And Welcome to Programming with Ruby Episode 3, Getting Help
and Tools. I’m your presenter, Tyler. This video is brought to you by
manwithcode.com

Covered in this Episode.
I’ll be going over how to get help.
If you ever get stuck while using Ruby you’re first stop should be Google,
to some this may sound obvious, but some people still don’t use Google.
Your next stop would be Ruby’s Documentation.
Then I will be showing you forums and mailing lists where you can ask for help
or help others.
Also I will be showing you some blogs by Ruby Developers.

On the tools side, I will be showing you some good Text Editors, and a few
IDEs (or Integrated Development Environments).

Just as a side note, I will be showing many websites today, but don’t worry,
all links are in the description

Lets Get Started!

Out first stop will be Google. Lets say I wanted to learn about Ruby Lambdas
just type in “ruby lamdas” and you get a list of relavant web pages. I’ll
pick the first link, and here is some information on how to use lambdas.

If Google can’t help you, lets look at the official Ruby Documentation.
This is ruby-doc.org. It has some articles and tutorials on Ruby. But what
we are interested in is the part that says “Core API” we are using the
1.8 version of Ruby, so we will visit the 1.8.6 core link
and here is the documentation for Ruby! lets say I wanted to look for lambdas
again, I’ll hit CTRL-F for the browsers find funtion, and type in “lambda” and
here is the information I want, I’ll click the link, and there is the
documentation!

Okay lets say that a Google search and a look through the documentation doesn’t
help you with your problem. What do you do? You ask a person, of course! This
is where the forums come in.

There are two forums that I like, there is ruby-forum.com
And rubyforums.com
both of which you can post on and will hopefully get answers

For general tips and news about Ruby, you may be interested in some blogs about
Ruby. To find some, lets go to ruby-lang.org
click on the community link.
scroll down, and click the “weblogs about ruby” link
and here there are some blogs, and aggregators listed

So hopefully if you have a problem all these resources should be able to help
you. Now we are going to move onto tools. First stop Text Editors.

There are a few good text editors available, so I will just highlight a few.
If any of these look interesting, remember that all links are in the description.
First is Windows only text editor notepad++, the editor I use on Windows
Then is SciTE, a scintilla based editor
Jedit is a popular text editor that is written in Java
Text mate is a very popular text editor for the Macintosh that costs $55
For Linux there are editors like gEdit and Kate which have some of the features
of the editors mentioned above.

Even if you like your featureless plain text editor like notepad, features the
previously mentioned editors have make writing code much easier, and I recommend
you get one.

First we have netbeans, which is my IDE of choice. Even though it was originally
for Java, it works very well with Ruby
Next is Aptana rad rails, an IDE which many people like, but was too buggy on my
computer, it is especially useful if you are using Ruby on Rails
freeRIDE is a popular editor for ruby
Geany is a GTK based editor for Linux that works with many different languages

These are all very good editors, but because you are currently learning I would
just recommend a text editor for now, when you start developing larger projects
an IDE can be very useful.

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

Hey! before you go, you may have realized that I am making these videos for free
if they have helped you at all please donate. If you viewing this on Youtube there is a
donation link to the right in the description box. If you are on my site there is
a donation button to the right

Thanks for watching! Bye!

Programming With Ruby Episode 1, Introduction


Covered in this episode:

  • What this series is about
  • A Short History of programming languages
  • What is Ruby (and who makes it)
  • What is Ruby used for
  • Who am I
  • Why am I teaching this

Transcript

Hello everybody and welcome to Programming with Ruby Episode 1, Introduction

What this series is about
By the end of this series you should be able to effectively use the programming language, Ruby

Short History of Programming Languages
when it all first started out, everything was just 1s and 0s, it was hard to use, and difficult to debug
then it got a little better when assembly came along. You had words like MOV that translated into the corresponding 1s and 0s. compiled by and assembler
then came COLBOL and FORTRAN
then C, which was better, but still sucked
then C++ which brought object orientation to the world
then Python and Ruby. they are both object oriented and very easy to use.

What is Ruby?
A programming language, that supports many different kinds of programming paradigms, and is fully object oriented
Ruby was created by Yuukihiro “Matz” Matsumoto
Matz designed Ruby to be natural, not simple. I think he achieved that.

What is Ruby Used For?
Ruby is used a lot for Web Applications
Computer Administration
Task Automation
Game Programming
And almost anything else
The only thing you might want to stay away from are things that are computationally expensive, like image processing.

Who am I?
I am Tyler J Church
I run the sites Man With Code (manwithcode.com) and ruby game dev (rubygamedev.wordpress.com)

Why am I Teaching This?
It is the tutorial I never had. I was look for something like this when I started learning Ruby. I didn’t want to read a bunch, I wanted to watch videos, but nothing like that existed
I think video is the best way to learn
I want to give back to the community. That brought me Ruby, that helped me learn. Thank You!

Questions or Comments
Leave a comment on this page
or email me at tyler@manwithcode.com

Thanks for reading!