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...
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
x = 4
unless x == 4
puts("This appears to be true");
else
puts("This appears to be false");
end
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."
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
#Print the string "Hello, World."
print "Hello, World."
1#Print the string "Hello, World."
2print "Hello, World."
For the string "Hello, Ruby." find the index of the word "Ruby."
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
#For the string "Hello, Ruby." find the index of the word "Ruby."
phrase = "Hello, Ruby."
puts phrase.index("Ruby.")
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
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
#Print your name ten time
i = 0
while i <= 10
puts "Matt Graf"
i +=1
end
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
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
#Print the string "This is sentence number 1," where the number 1
#changes from 1 to 10
i=1;
until i > 10 do
puts ("This is Sentence number #{i},")
(i+=1);
end
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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
#Run a Ruby program from a file.
ruby /users/foobar/rubydemo/foo.rb
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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
#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.
#lets go head and build a randRange function because we can
def rand_range(min,max)
min + rand( max - min )
end
# define a function, for the heck of it
def guess_number_game
#create a random number
start_num =9;
end_num=10;
random_number = rand_range( start_num, end_num );
#create a variable to track how attempts it take
#the user to guess the number
user_attempt = 0;
#prompt the user to guess a number between 1 and 10
puts("Guess a Number? Between #{start_num} and #{end_num}. ");
puts("Guess: ");
#use the begin while loop
begin
#Get the users input and make it an integer
user_guess = gets.chomp.to_i;
#Here I am using one of the ways to
#make a decision with an if
puts "Guess higher: " if user_guess < random_number
#Here I wanted to use another decisions method unless
puts "Guess lower: " unless user_guess <= random_number
#Track the users attempts
user_attempt += 1;
#Loop while the user's guess does not match
#the random number
end while( user_guess != random_number )
#Output a message along with the user's attempts
puts("Guessed it on #{user_attempt} try's");
end
#call the number game
guess_number_game
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.