Blog Archives

Entries for month: December 2010

Seven Languages in Seven Weeks: Ruby Day 2

Day two was little faster pace and we saw Ruby's magic and sugar according to to the author. We dove into working with structures, arrays and built a simple class. So far Ruby seems pretty easy and a little fun and its very easy to find help via google.

Read more...

Seven Languages in Seven Weeks: Ruby Day 1

 

For Christmas 2010 my wife bought me Seven Languages in Seven Weeks which I ask for after seeing it on Ben Nadel's blog. 

I Finished Day 1 of Ruby, so far its a very easy read. I learned how to output strings, a couple of different loops (while and until), Decisions (if and unless) and a little more. One of the things that I like about Ruby is the way it reads. 

Example 1:

 

view plain print about
1x = 4
2unless x == 4
3    puts("This appears to be true");
4else
5    puts("This appears to be false");
6end

Day 1 HW

Print the string "Hello, World."

view plain print about
1#Print the string "Hello, World."
2print "Hello, World."

For the string "Hello, Ruby." find the index of the word "Ruby."

view plain print about
1#For the string "Hello, Ruby." find the index of the word "Ruby."
2phrase = "Hello, Ruby."
3puts phrase.index("Ruby.")

Print your name ten time

view plain print about
1#Print your name ten time
2i = 0
3while i <= 10
4    puts "Matt Graf"
5    i +=1
6end

Print the string "This is sentence number 1," where the number 1 changes from 1 to 10

view plain print about
1#Print the string "This is sentence number 1," where the number 1
2#changes from 1 to 10
3i=1;
4until i > 10 do
5 puts ("This is Sentence number #{i},")
6 (i+=1);
7end

Run a Ruby program from a file.

view plain print about
1#Run a Ruby program from a file.
2ruby /users/foobar/rubydemo/foo.rb

Bonus problem: If you're feeling the need for a little more, write a program that picks a random number. Let a player guess the number, telling the player whether the guess is too lower or too high.

view plain print about
1#Bonus problem: If you're feeling the need for a little more,
2#write a program that picks a random number. Let a player
3#guess the number, telling the player whether the guess is too
4#lower or too high.
5#lets go head and build a randRange function because we can
6def rand_range(min,max)
7 min + rand( max - min )
8end
9# define a function, for the heck of it
10def guess_number_game
11    
12    #create a random number
13    start_num =9;
14    end_num=10;
15    random_number = rand_range( start_num, end_num );
16    
17    #create a variable to track how attempts it take
18    #the user to guess the number
19    user_attempt = 0;
20    
21    
22    #prompt the user to guess a number between 1 and 10
23    puts("Guess a Number? Between #{start_num} and #{end_num}. ");
24    puts("Guess: ");
25    
26    #use the begin while loop
27    begin    
28        
29        #Get the users input and make it an integer
30        user_guess = gets.chomp.to_i;
31        
32        #Here I am using one of the ways to
33        #make a decision with an if
34        puts "Guess higher: " if user_guess < random_number
35        
36        #Here I wanted to use another decisions method unless
37        puts "Guess lower: " unless user_guess <= random_number
38        
39        #Track the users attempts
40        user_attempt += 1;
41    
42        #Loop while the user's guess does not match
43        #the random number
44    end while( user_guess != random_number )
45    
46    #Output a message along with the user's attempts
47    puts("Guessed it on #{user_attempt} try's");
48    
49end
50#call the number game
51guess_number_game

I have 2 more days to go on Ruby.