CS 3060: Programming Languages

Ruby

Ruby

Robert C. Green II, Ph.D.

Day 1

Meet Ruby...She's sometimes quirky, always beautiful, a little mysterious, and absolutely magical. Think Mary Poppins..

A Few Notes...

  • Created in 1993
  • Interpreted
  • Object-oriented
  • Dynamically typed
  • Scripting Language
  • Everything returns something...

...programming was fun again...

What is the Typing Model?

Strong or Weak?

						
			4.class
						
					
						
			'four'.class
						
					
						
			4 + 'four'
						
					

What is the Typing Model?

Dynamic or Static?

						
			def add_them_up
			    4 + 'four'
			end

			add_them_up
						
					

What is the Typing Model?

Dynamic or Static?

						
			i = 0
			a = ['100', 100.0]
			
			while i < 2
			  puts a[i].to_i
			  i = i + 1
			end
						
					

What is the Programming Model?

Purely Object-Oriented

Interpreted or Compiled?

Interpreted

How do I write output?

						
			puts "Hello, World!"
						
					

How do I write output?

						
			language = "Ruby"
			puts "hello, #{language}"
						
					

Some clever things...

						
			4.methods
						
					

Control Structures

						
		x = 4 
		puts 'This appears to be false.' unless x == 4
						
					
						
		 puts 'This appears to be true.' if x == 4
						
					
						
		 if x == 4
		 	puts 'This appears to be true.'
		 end
						
					

Control Structures

						
		 unless x == 4
		 	puts 'This appears to be false.'
		 else
		 	puts 'This appears to be true.'
		 end
						
					
						
		  puts 'This appears to be true.' if not true
						
					
						
		  puts 'This appears to be true.' if !true
						
					

Control Structures

						
		  x = x + 1 while x < 10
						
					
						
		  x = x - 1 until x == 1
						
					
						
		  while x < 10
		      x = x + 1
		      puts x
		  end
						
					

Note

Everything but nil and false evaluate to true.

Assignment

Read Day 2