SlideShare a Scribd company logo
1 of 49
Download to read offline
ACM init() 
Day 1: October 23, 2014
Kelly
Akshay
Daniel
Welcome! 
Welcome to ACM init(), a Computer Science class for 
beginners. 
Over the course of the next few weeks, you will be 
learning Ruby, a flexible, widely used programming 
language.
About the Course 
• This course assumes no prior programming 
experience 
• Hosted by the Association for Computing 
Machinery (ACM) 
• This course will be teaching you how to code in 
a language called Ruby 
• NO EXAMS!!!!
Resources for You 
Our website: acm.cs.ucla.edu 
Slides: slideshare.net/uclaacm 
Facebook: https://www.facebook.com/groups/717821374976381/ 
Want to contact us? 
Kelly: omalleyk@ucla.edu 
Daniel: dduan@yahoo.com 
Akshay: akshaybakshi@ucla.edu
Remember: If you 
have any questions, 
ask! Chances are 
someone else has the 
same question and is 
hesitant to ask.
If you don’t 
understand 
something, it is more 
important that you 
learn it than we finish 
everything today.
Why Computer Science? 
• Computer Science is a fast 
growing field with a surplus of 
high-paying jobs 
• Learning a programming 
language will enhance your 
resume and make you more 
desirable to companies 
• Furthermore, learning how to 
code will better your problem-solving 
skills and help you in 
all of your classes 
Source: http://www.ovrdrv.com/blog/making-computer-science-cool/
The numbers speak for 
themselves... 
Source: http://code.org/promote
Who knows what a 
programming 
language is?
Why Learn Ruby? 
• Ruby is easy to read and use, 
and is considered to be one of 
the best first languages to 
learn 
• Ruby developers are in high 
demand 
• Ruby is useful -- Ruby on Rails 
is one of the most popular 
web programming platforms in 
the startup community
Let's Get Started!
Computing the Factorial of a 
Number 
• Factorial: written as #! 
• For example, 5! is 5*4*3*2*1 = 120 
• I wrote a simple program called fact.rb that will 
compute the factorial for us 
• Let's try it out! Compute the factorial of 6. 
• ruby fact.rb 6
A Bit About Computers 
Computers think in 0s and 1s. Everything that you input into a 
computer gets stored in this manner. For example: 
You don't need to know how this conversion works. However, the concept is 
important because it is the reason why we need programming languages. 
Human-readable programming languages go through a process that 
converts them to binary, which can be understood by the compter.
How do we make our code 
work? 
Ruby is what is called an interpreted language. That means 
that the computer runs one line of code at a time. 
We can't just type code into a word document and hope it will 
magically do something. We need an interpreter that will help 
the computer understand our code.
koding.com 
koding.com is an easy way to run code that requires no 
downloading or storage space.
Enter your email and choose a username
This is what you should see once you log in.
Let's learn how to run a program. Type 
puts "I love computers!" 
into the top window in Koding.
Now, save the file. Select the option Save As.
Select a directory to save the file in. I choose a directory I 
created called ACMclass. Now, name the file myfirstfile.rb
Press Save
The file has been saved! Now, let's run it.
First, we need to make sure we are in the right directory. Type 
in cd 'theDirectoryYouSavedIn' then press Enter. 
It changed!
Now we can access our file! Time to run it. Type in 
ruby myfirstfile.rb 
This tells Koding that we are about to run a Ruby file.
Press Enter, and it runs! The screen printed out "I love 
computers!"
Time to start learning 
Ruby.
Data Types 
• Why? Say "five". Did you say 5 or five? 
• There are three main data types in Ruby: numbers, 
booleans, and strings. 
• Numbers: Exactly what it sounds like. (example: 1, 
400, 45.7) 
• Booleans: Something that is either true or false. 
• String: A word or phrase. (example: "Hello World!")
Variables 
We can store these data types in variables so we can 
come back and use them again later. 
Take a look: 
Now if we want to use any of these stored values 
again all we have to do is type the variable name!
Math 
There are six main math operations we are going to go over 
today.
+ - * / 
• + is addition 
• - is subtraction 
• * is multiplication 
• / is division 
• These work the same way 
your calculator does -- no 
surprises here
** and % 
• ** (exponent) raises one 
number to the power of 
another. For example, 2**3 
would be 8 because 2*2*2=8 
• % (modulus) returns the 
remainder of division. For 
example, 25%7 would be 4 
because 25/7 is 3r4.
Printing to the Screen 
There are two main commands that will allow you to 
print text out to the screen. 
These commands are 'puts' and 'print'.
'puts' vs 'print' 
The 'print' command just takes whatever you give it and prints 
it to the screen. 
'puts' (for "put string") is slightly different: it adds a new (blank) 
line after the thing you want it to print.
'puts' vs. 'print' example
String Methods 
• Remember, a string is is word or a phrase. Ruby 
provides several methods for us to make it easier to 
use strings 
• '.length' : finds the length of the string 
• '.reverse' : reverses the string 
• '.upcase' : changes all letters to uppercase 
• '.downcase' : changes all letters to lowercase
String Methods Example
We've covered all the Ruby syntax 
we're going over for today. Now, 
let's learn a bit about writing good 
code.
Comments 
What if you write a really long program then not look at it for a 
year? What if you're working on a project with other people 
and you have to share your code? 
We need a way to explain what is going on in the programs we 
write. 
The solution? Comments.
How to add comments 
Comments are a tool we have to communicate what our code 
is intended to do. 
To put a comment in your code all you have to do is type 
# your comment here 
Anything that comes after '#' and is on the same line will be 
ignored when the code is being run.
Comment Example
Naming Conventions 
• Remember that we can give values to variables so we can use the same 
value again later 
• We can name these variables whatever we want 
• Doesn't it make sense to give variables descriptive names? 
• Some naming rules: 
• Separate words with underscores '_' 
• Do not start a variable name with a number, symbol, or uppercase 
letter 
• The variable name should describe what the variable contains or will 
be used for
Naming Examples
What we covered 
• A little bit about Ruby 
• How to use Koding 
• Data types: numbers, string, and booleans 
• Variables 
• Math 
• How to print to the screen 
• String methods 
• How to write good code
Any questions? 
Office Hours will be right after the 
class every time.

More Related Content

Similar to ACM Init() lesson 1

Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDavide Mauri
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramMichael Heron
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013Iván Montes
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptxHaythamBarakeh1
 
Nllug 2010 - Web-services bootcamp
Nllug 2010 - Web-services bootcampNllug 2010 - Web-services bootcamp
Nllug 2010 - Web-services bootcampBill Buchan
 
Nllug 2010-web-services
Nllug 2010-web-servicesNllug 2010-web-services
Nllug 2010-web-servicesBill Buchan
 
What Is Coding And Why Should You Learn It?
What Is Coding And Why Should You Learn It?What Is Coding And Why Should You Learn It?
What Is Coding And Why Should You Learn It?Syed Hassan Raza
 
Ruby tutorial
Ruby tutorialRuby tutorial
Ruby tutorialknoppix
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageGOKULKANNANMMECLECTC
 

Similar to ACM Init() lesson 1 (20)

ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
Ruby
RubyRuby
Ruby
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a Program
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
ruby pentest
ruby pentestruby pentest
ruby pentest
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptx
 
Rubykin
Rubykin Rubykin
Rubykin
 
Nllug 2010 - Web-services bootcamp
Nllug 2010 - Web-services bootcampNllug 2010 - Web-services bootcamp
Nllug 2010 - Web-services bootcamp
 
Nllug 2010-web-services
Nllug 2010-web-servicesNllug 2010-web-services
Nllug 2010-web-services
 
What Is Coding And Why Should You Learn It?
What Is Coding And Why Should You Learn It?What Is Coding And Why Should You Learn It?
What Is Coding And Why Should You Learn It?
 
Ruby tutorial
Ruby tutorialRuby tutorial
Ruby tutorial
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c language
 
Java basics
Java basicsJava basics
Java basics
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
 
Cucumber in Practice(en)
Cucumber in Practice(en)Cucumber in Practice(en)
Cucumber in Practice(en)
 
DAA Unit 1.pdf
DAA Unit 1.pdfDAA Unit 1.pdf
DAA Unit 1.pdf
 

More from UCLA Association of Computing Machinery

More from UCLA Association of Computing Machinery (16)

ACM init()- Day 4
ACM init()- Day 4ACM init()- Day 4
ACM init()- Day 4
 
ACM init() Day 3
ACM init() Day 3ACM init() Day 3
ACM init() Day 3
 
ACM init() Day 2
ACM init() Day 2ACM init() Day 2
ACM init() Day 2
 
UCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meetingUCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meeting
 
UCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your DomainUCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your Domain
 
Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)
 
An Introduction to Sensible Typography
An Introduction to Sensible TypographyAn Introduction to Sensible Typography
An Introduction to Sensible Typography
 
Building a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground UpBuilding a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground Up
 
ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
ACM init() Day 5
ACM init() Day 5ACM init() Day 5
ACM init() Day 5
 
Init() Day 4
Init() Day 4Init() Day 4
Init() Day 4
 
Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2
Init() Lesson 2
 
ACM Fall General Meeting
ACM Fall General Meeting ACM Fall General Meeting
ACM Fall General Meeting
 
ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014
 
ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014
 

Recently uploaded

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

ACM Init() lesson 1

  • 1. ACM init() Day 1: October 23, 2014
  • 5. Welcome! Welcome to ACM init(), a Computer Science class for beginners. Over the course of the next few weeks, you will be learning Ruby, a flexible, widely used programming language.
  • 6. About the Course • This course assumes no prior programming experience • Hosted by the Association for Computing Machinery (ACM) • This course will be teaching you how to code in a language called Ruby • NO EXAMS!!!!
  • 7.
  • 8. Resources for You Our website: acm.cs.ucla.edu Slides: slideshare.net/uclaacm Facebook: https://www.facebook.com/groups/717821374976381/ Want to contact us? Kelly: omalleyk@ucla.edu Daniel: dduan@yahoo.com Akshay: akshaybakshi@ucla.edu
  • 9. Remember: If you have any questions, ask! Chances are someone else has the same question and is hesitant to ask.
  • 10. If you don’t understand something, it is more important that you learn it than we finish everything today.
  • 11. Why Computer Science? • Computer Science is a fast growing field with a surplus of high-paying jobs • Learning a programming language will enhance your resume and make you more desirable to companies • Furthermore, learning how to code will better your problem-solving skills and help you in all of your classes Source: http://www.ovrdrv.com/blog/making-computer-science-cool/
  • 12. The numbers speak for themselves... Source: http://code.org/promote
  • 13. Who knows what a programming language is?
  • 14. Why Learn Ruby? • Ruby is easy to read and use, and is considered to be one of the best first languages to learn • Ruby developers are in high demand • Ruby is useful -- Ruby on Rails is one of the most popular web programming platforms in the startup community
  • 16. Computing the Factorial of a Number • Factorial: written as #! • For example, 5! is 5*4*3*2*1 = 120 • I wrote a simple program called fact.rb that will compute the factorial for us • Let's try it out! Compute the factorial of 6. • ruby fact.rb 6
  • 17.
  • 18. A Bit About Computers Computers think in 0s and 1s. Everything that you input into a computer gets stored in this manner. For example: You don't need to know how this conversion works. However, the concept is important because it is the reason why we need programming languages. Human-readable programming languages go through a process that converts them to binary, which can be understood by the compter.
  • 19. How do we make our code work? Ruby is what is called an interpreted language. That means that the computer runs one line of code at a time. We can't just type code into a word document and hope it will magically do something. We need an interpreter that will help the computer understand our code.
  • 20. koding.com koding.com is an easy way to run code that requires no downloading or storage space.
  • 21. Enter your email and choose a username
  • 22. This is what you should see once you log in.
  • 23. Let's learn how to run a program. Type puts "I love computers!" into the top window in Koding.
  • 24. Now, save the file. Select the option Save As.
  • 25. Select a directory to save the file in. I choose a directory I created called ACMclass. Now, name the file myfirstfile.rb
  • 27. The file has been saved! Now, let's run it.
  • 28. First, we need to make sure we are in the right directory. Type in cd 'theDirectoryYouSavedIn' then press Enter. It changed!
  • 29. Now we can access our file! Time to run it. Type in ruby myfirstfile.rb This tells Koding that we are about to run a Ruby file.
  • 30. Press Enter, and it runs! The screen printed out "I love computers!"
  • 31. Time to start learning Ruby.
  • 32. Data Types • Why? Say "five". Did you say 5 or five? • There are three main data types in Ruby: numbers, booleans, and strings. • Numbers: Exactly what it sounds like. (example: 1, 400, 45.7) • Booleans: Something that is either true or false. • String: A word or phrase. (example: "Hello World!")
  • 33. Variables We can store these data types in variables so we can come back and use them again later. Take a look: Now if we want to use any of these stored values again all we have to do is type the variable name!
  • 34. Math There are six main math operations we are going to go over today.
  • 35. + - * / • + is addition • - is subtraction • * is multiplication • / is division • These work the same way your calculator does -- no surprises here
  • 36. ** and % • ** (exponent) raises one number to the power of another. For example, 2**3 would be 8 because 2*2*2=8 • % (modulus) returns the remainder of division. For example, 25%7 would be 4 because 25/7 is 3r4.
  • 37. Printing to the Screen There are two main commands that will allow you to print text out to the screen. These commands are 'puts' and 'print'.
  • 38. 'puts' vs 'print' The 'print' command just takes whatever you give it and prints it to the screen. 'puts' (for "put string") is slightly different: it adds a new (blank) line after the thing you want it to print.
  • 40. String Methods • Remember, a string is is word or a phrase. Ruby provides several methods for us to make it easier to use strings • '.length' : finds the length of the string • '.reverse' : reverses the string • '.upcase' : changes all letters to uppercase • '.downcase' : changes all letters to lowercase
  • 42. We've covered all the Ruby syntax we're going over for today. Now, let's learn a bit about writing good code.
  • 43. Comments What if you write a really long program then not look at it for a year? What if you're working on a project with other people and you have to share your code? We need a way to explain what is going on in the programs we write. The solution? Comments.
  • 44. How to add comments Comments are a tool we have to communicate what our code is intended to do. To put a comment in your code all you have to do is type # your comment here Anything that comes after '#' and is on the same line will be ignored when the code is being run.
  • 46. Naming Conventions • Remember that we can give values to variables so we can use the same value again later • We can name these variables whatever we want • Doesn't it make sense to give variables descriptive names? • Some naming rules: • Separate words with underscores '_' • Do not start a variable name with a number, symbol, or uppercase letter • The variable name should describe what the variable contains or will be used for
  • 48. What we covered • A little bit about Ruby • How to use Koding • Data types: numbers, string, and booleans • Variables • Math • How to print to the screen • String methods • How to write good code
  • 49. Any questions? Office Hours will be right after the class every time.