Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Hands-On Introduction to Ruby

A Hands-On Introduction to Ruby

A brief introduction to the wonderful world of Ruby.

Zach Latta

July 27, 2013
Tweet

More Decks by Zach Latta

Other Decks in Programming

Transcript

  1. 1 "DO NOT USE ALL CAPS".downcase => "do not use

    all caps" valid ruby 2 5.rationalize => (5/1)
  2. 1 5.times { print "hi" } => "hihihihihi" print “hi”

    five times 2 print "hi" * 5 => "hihihihihi"
  3. 1 import java.io.* 2 3 public class Hi 4 {

    5 public static void main(String[] args) 6 { 7 for (int i = 0; i < 5; i++) 8 { 9 System.out.print("hi"); 10 } 11 } 12 } …and in java
  4. 1 puts "What is your name?" 2 name = gets.strip!

    3 4 puts "Hello #{ name }. How are you?" greetings
  5. ! 1 import java.util.Scanner; 2 3 public class ALessNiceGreeting 4

    { 5 public static void main(String[] args) 6 { 7 Scanner scanner = new Scanner(System.in); 8 9 System.out.println("What is your name?"); 10 String name = scanner.nextLine().trim(); 11 12 System.out.println("Hi " + name + ". How are you?"); 13 14 scanner.close(); 15 } 16 } greetings (cont.)
  6. 1 import java.io.BufferedReader; 2 import java.io.FileReader; 3 import java.io.IOException; 4

    5 public class BufferedReaderExample 6 { 7 public static void main(String[] args) 8 { 9 BufferedReader br = null; 10 11 try 12 { 13 14 String sCurrentLine; 15 16 br = new BufferedReader(new FileReader("file.txt")); 17 18 while ((sCurrentLine = br.readLine()) != null) 19 { 20 System.out.println(sCurrentLine); 21 } 22 23 } 24 catch (IOException e) 25 { 26 e.printStackTrace(); 27 } 28 finally 29 { 30 try 31 { 32 if (br != null)br.close(); 33 } 34 catch (IOException ex) 35 { 36 ex.printStackTrace(); 37 } 38 } 39 40 } 41 } file io (cont.)