Ruby on Rails Basics
The "Hello world!" program is a simple program that displays some text on the screen.
ruby -v - what version is running
treehouse:~/workspace$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
treehouse:~/workspace$ irb
irb(main):001:0> puts "Hello world"
Hello world
=> nil
irb(main):002:0>
Exit irb:
irb(main):001:0> exit
Create a file called "hello.rb" and place the following in the file:
puts "Hello World!"
Run the program we just created by typing the following:
ruby hello.rb
Rules
irb(main):001:0> exit
Create a file called "hello.rb" and place the following in the file:
puts "Hello World!"
Run the program we just created by typing the following:
ruby hello.rb
------------
puts
put string on the screen
gets
get string input
-------------
Create a variable name = "priss"
file
name = "priss"
puts "hello"
puts name
console
treehouse:~/workspace$ ruby hello.rb
hello
Jason
-------------
Put name on 1 line
name = "Jason"
puts "hello #{name}!"
treehouse:~/workspace$ ruby hello.rb
hello Jason!
-------------
print "Please enter your name: "
name = gets
puts "hello #{name}!"
treehouse:~/workspace$ ruby hello.rb
Please enter your name: priss
hello priss
!
treehouse:~/workspace$
-------------
Print out your name:
name = "Jason"
puts "Hello #{name}"
Get input from the user:
name = gets
puts "Hello #{name}"
Run a Ruby program called "hello.rb":
ruby hello.rb
Prompt the user for their name:
print "Please enter your name: "
name = gets
puts "Hello #{name}"
Ruby on Rails Iron Hack
-----
Comments
#this is the code
=begin
klgklh
=end
----
#Put strings
puts "hello"
-----
#Put number
puts 42
----
#Put multiplication
puts 6 * 7
operation return results and then it puts =>nil
7 alone return
=>
Return last thing I did
Puts output values
------
Variables
You can use them to keep track of values over time
result = 2 * 7
puts result
-----
Note: Use
minute = 60
hour = 60 * minute
day = 24 * hour
puts "Seconds in a day:"
puts day
(or day also works)
-------
String interpolation
name = "Nizar"
puts "Hi #{name}" (Put value inside inside like a template)
minute = 60
hour = 60 * minute
day = 24 * hour
puts "Seconds in a #{day}"
puts day
-------
\n (is a new line)
More printing
days = "\nMon\nTue\nWed\nThur\nFriday\nSaturday\nSunday"
puts "The days of the week #{days}"
------
Multi-line string """
beginning = """
Mon
Tue
Wed
Thur
Friday
Saturday
Sunday
"""
------
Asking Questions
Grab the text from the terminal input
puts "What is your name"
name = gets.chomp
puts "Hello, #{name}!"
__
to_i stand for integer (whole numbers)
puts "Give me one number"
first_number = gets.chomp.to_i
puts "Give me one number"
second_number = gets.chomp.to_i
result = first_number * second_number
puts "#{first_number} x #{second_number} = #{result}"
--------
Reading files
#doesn't work on repl.it - contents = 10.read("README")
contents = io.read("humans.txt")
puts contents
-----
Writing files
puts "Your name?"
name = gets.chomp
IO.write('humans.txt', name*100)
------
#Repeat hi 5 times
puts "hi" = 5
------
Function
#def defining the functions - greet name of the functions
#functions put - get
def greet(name)
puts "Hi, #{name}!"
end
greet "niazar"
greet "Priss"
greet "clara"
-----
#Function that said good thing about first and bad to second
def props_slops(name_one, name_two)
puts "Your are great #{name_one}!"
puts "Your are bad #{name_two}!"
end
props_slops ("niazar" , "Priss")
-------
Gets some input from person chomp
puts "Who is cool"
good_person = gets.chomp
puts "Who is bad"
bad_person = gets.chomp
props_slops good_person , bad_person
--------
Return Values
#Function returns values - variables have values
#square al cuadrado 8*8 = 64
def square (number)
number * number
end
#call the function
puts square(8)
def multiply(a, b)
return a * b
end
puts multiply(6,7)
puts square (8)
#calling the function and saving the values on variables
six_times_seven = multiply(6,7)
seven_square = square(7)
puts six_times_seven
puts seven_square
#storing new values - overriding
puts_value = puts 'test'
puts_value
------------
returning multiple values
minute = 60
hour = 60 * minute
day = 24 * hour
def time_in_days(days)
hours = 24 * days
minutes = 60 * hours
seconds = 60 minutes
return hours, minutes, seconds
end
hours, minutes, seconds = time_in_days()
puts "Threre are #{hours} in 3 days"
puts "Threre are #{minutes} in 3 days"
puts "Threre are #{seconds} in 3 days"
--------
Comparison Logic
puts 12 == 1
puts 3 < 4
puts 12 >= 12
puts true && true
puts false && true
puts true || false
puts false || false
#with &&
#If one is false is going to be false - both have to be true
#with ||
#is different
---------
Question when user enter input
Is empty?
is more than 10 character?
--------
Program making decisions
puts "What's your name?"
name = gets.chomp
if name == ""
puts "Come on, person! Don't be shy!"
else
puts "Hello, #{name}!"
end
-----------
Conditionals
f name == ""
puts "Come on, person! Don't be shy!"
else
puts "Hello, #{name}!"
--------
else if
puts "What's your favorite animal?"
animal = gets.chomp
if animal == "porcupine"
puts "Gross!"
elseif animal == "hippo"
puts "Great!"
else
puts "I guess #{animal}s are alright."
end
-------
Array - Looping arrays
---------
Loop keep going until the condition is true
reprompt
puts "What's your name?"
name = gets.chomp
while name ==""
puts " Come tell my your name"
name = gets.chomp
end
puts "Hello, #{name}!"
-----------
for number in 1..30
puts number
end
------
for number in 1..30
if number == 5
puts "I like 5"
end
puts number
end
Application Address:
http://localhost:3000/
Commands
Install the Dev Kit
ruby dk.rb init
ruby dk.rb install
Install Supplementary Gems
gem install bundler
gem install sqlite
gem install rails --version=4.0.2
Create Test Rails Application
rails new testapp
cd testapp
bundle exec rails server
--------
Install Ruby on Mac
Commands
Install rbenv:
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Then close and open your terminal.
Install Ruby
rbenv install 2.0.0-p353
Wait for that to finish, then type:
rbenv global 2.0.0-p353
Install Gems
gem install bundler
rbenv rehash
gem install sqlite3
gem install rails --version=4.0.2
rbenv rehash
Generate a Rails App
Create Test Rails Application
rails new testapp
cd testapp
bin/rails server
-------
Installing the Treehouse VM on Mac
In this video we install a Ruby Development Environment using the Treehouse Virtual Machine on our Macintosh computers.
Commands
Launch vagrant and download virtual machine:
vagrant up
Log In To VM
vagrant ssh
Shut Down VM
vagrant halt
Create Test Rails Application
rails new testapp
cd testapp
bin/rails server
----