Upload
fraboni-ec
View
26
Download
0
Embed Size (px)
Citation preview
Learning Ruby
Let's Learn Ruby AQAP
As Quickly As Possible!
Run the interactive ruby shell – irb
Suggest using Netbeans – download ALL
Running Ruby Code
2
2 + 6
22 / 4 5Float(22)/4
22.0 / 4
3 * 3
3 ** 3
big_number = 1_000_000_000 (underscores not required and can go anywhere allowed to make it more readable – commas hard to see)
Simple Ruby Number Objects
3
Math.sqrt( 9 )
Math.methods
9.methods will tell you what methods can be used with 9
27.class will tell you what class 27 belongs to (Fixnum)
3.times { print "Ho! " }
1.upto( 5 ) { |i| puts i } defines i as the iterator variable
termed a “code block” Anonymous delegate given the name i
Ruby Number Methods
4
one = "1"two = "2"
one + two string concat, right?Integer( one ) + Integer( two ) casts and then adds
12.to_s calls to string method, but wants you to request conversion
print "Happy ", 12.to_s, "th Birthday!"puts "Happy ", 12.to_s, "th Birthday!"puts "Happy " + 12 + "th Birthday!"
Number Conversions
5
a1 = "this is a string\n” evaluate backslash and contents of #{} inside of the string. Termed string interpolation
a2 = 'and so is this\n‘ will see the \n as no evaluation is done
print a1print a2
answer = “joy”
puts "The meaning of life is #{answer}"
puts "There's #{24*60*60} seconds in a day"
Ruby String Objects
6
formatted_text = <<END_OF_TEXTLet us turn our thoughts today
To Martin Luther KingAnd recognize that there are ties between us
All men and womenLiving on the Earth
Ties of hope and loveSister and brotherhood
That we are bound togetherIn our desire to see the world become
A place in which our childrenCan grow free and strong
END_OF_TEXT any delimiter – as long as it matches
print formatted_text interpretation does work
Ruby Here Documents
7
String.methods.sort sorted list of String methodsformatted_text.size number of characters
formatted_text.downcase lowercaseformatted_text.upcase uppercase
formatted_text.capitalize! ! means to actually change the object in place (rather than return a changed copy)
formatted_text.reverse reverses all characters
formatted_text.empty? boolean indicated by ? only used in method names (not variables)
Ruby String Methods
8
song_lines = formatted_text.to_a to array(1..10).to_a [1,2,3,4,5,6,7,8,9,10] from a range (denoted ..)(‘a’..’e’).to_a [“a”, “b”, “c”, “d”, “e”]
print song_lines[1]print song_lines[5]print song_lines[99] Gives nil if used when out of
range, will add to array if assign outside of range.
print song_linesprint formatted_text
song_lines.class Arrayformatted_text.class String
Working with Ruby Strings
9
Same variable can hold any type at different times…a = 42puts aa = "The Ruby Programming Language"puts aa = 1.8puts a
What About Variable Type?
10
Everything - numbers, strings, data structures, Class itself etc. - is an object
Methods are invoked using the dot (".") notation
Variables are dynamically created as needed (problems with typos), but will tell you during execution if you access something without a value.
The traditional notion of a variable's "type" is not something the Ruby programmer concerns themselves with (too much)
Ruby So Far
11