Week #5 Challenge: Ruby

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 9874 Views - Last Post: 19 August 2010 - 10:11 PM

#1 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Week #5 Challenge: Ruby

Post icon  Posted 30 January 2010 - 11:53 AM

Posted Image

GET FAMILIAR WITH RUBY

CHALLENGE: Get familiar with Ruby by creating a simple Ruby script.

What is Ruby? Wikipedia says it best:

Quote

Ruby is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby supports multiple programming paradigms, including functional, object oriented, imperative and reflective. It also has a dynamic type system and automatic memory management; it is therefore similar in varying respects to Python, Perl, Lisp, Dylan, and CLU.


IDEAS FOR SCRIPTS:
  • Simple number guessing game.
  • Script that checks if a string is a palindrome.
  • A madlibs game.
  • File splitter/joiner.
  • XML or JSON parser.
RESOURCES:
There are a lot of good resources out there for learning Ruby. Of course, I'd suggest reading the Ruby Basics tutorial by Kingbradley6 right here on DIC.

If you would like to read more, you can visit the Ruby website, where you can even try out Ruby right from your web browser without installing anything!

Also, here's a link to the wikipedia page that I quoted.

We also have a bunch of Ruby snippets here on Dream.In.Code that you can easily use in your application:
http://www.dreaminco...owse.php?cid=22

And of course, there are some tutorials to help you get started:
Ruby: Using Loops
Block and Iterators in Ruby
A Basic Understanding of Classes inside of Ruby
How to distribute Ruby Applications
Create a Game in Ruby ~pt 1~

HOW TO GET STARTED:
I would suggest following the Ruby Quickstart guide to get started. It will help you get Ruby installed on your operating system and take you through a small tutorial that will help get you familiar with the language and the Ruby console.

Ruby files are usually saved with the .rb extension and can be ran by passing the file as an argument to Ruby.

Here's an example of a simple Ruby script file:

print "What is your name? "
name = gets.chomp
puts "Hello #{name}!"


To run this script, save it as a Ruby file (.rb), and use the command:

ruby sample_ruby_script.rb


As you can see, Ruby is easy to get setup and an amazing language to work with. I've been working with Ruby for almost 5 months now and I can say that it's definitely a favorite of mine.

If you find yourself becomming increasingly interested in Ruby, then you can look into the book Wicked Cool Ruby Scripts by Steve Pugh for more scripting ideas and to further learn the Ruby language.

I wish you luck in this challenge and more importantly, HAVE FUN WITH IT!

This challenge will self destruct in 5 seconds.

Is This A Good Question/Topic? 2
  • +

Replies To: Week #5 Challenge: Ruby

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Week #5 Challenge: Ruby

Posted 30 January 2010 - 12:37 PM

Also, if you are up for some extensive reading, there are a few Ruby books freely available here

If you are using a Mac, then it will be worth checking MacRuby, that is a version of Ruby 1.9, ported to run directly on top of Mac OS X core technologies such as the Objective-C common runtime and garbage collector, and the CoreFoundation framework.

Have Java experience? Then take a look at JRuby (recommended by erik.price), which is a pure Java implementation of Ruby.
Was This Post Helpful? 0
  • +
  • -

#3 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Week #5 Challenge: Ruby

Posted 31 January 2010 - 03:10 PM

I've done some stuff with Ruby in the past. I'll probably post something anyways. Now to figure out what to submit.
Was This Post Helpful? 0
  • +
  • -

#4 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Week #5 Challenge: Ruby

Posted 31 January 2010 - 04:35 PM

What an amazing idea for a challenge. The person who wrote this must be the most awesome person ever! :P
Was This Post Helpful? 0
  • +
  • -

#5 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Week #5 Challenge: Ruby

Posted 31 January 2010 - 04:44 PM

View PostSkaggles, on 31 Jan, 2010 - 03:35 PM, said:

What an amazing idea for a challenge. The person who wrote this must be the most awesome person ever! :P


Shoot. No vested interests here.
Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Week #5 Challenge: Ruby

Posted 31 January 2010 - 07:04 PM

Yay for Ruby!

Here's a tiny Ruby method to calculate the factorial of a number
def fact n
 return 0 if n == 0
 1.upto(n-1).each.inject(1) { |fact, x| fact *= x+1 }
end



I'll try and submit some more stuff later on.

Also, I'd like to add JRuby to the list of resources. It's a Ruby implementation written in Java which allows you to interact with any Java classes and methods.

This post has been edited by erik.price: 31 January 2010 - 07:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Week #5 Challenge: Ruby

Posted 31 January 2010 - 10:12 PM

here's a simple palindrome checker


def is_palindrome(input)
  return input == input.reverse
end




and a palindrome checker using a simple class with an example of use


class Palindrome
  attr_accessor :word
  
  def palindrome?
	return @word == @word.reverse
  end
end

p = Palindrome.new

puts "Enter a string"
p.word = gets.chomp

puts "#{p.word} is a palindrome" if p.palindrome?
puts "#{p.word} is not a palindrome" if not p.palindrome?


This post has been edited by Nakor: 31 January 2010 - 10:23 PM

Was This Post Helpful? 1
  • +
  • -

#8 cmckni3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 09

Re: Week #5 Challenge: Ruby

Posted 31 January 2010 - 11:50 PM

My die rolling game.

def main
  puts "Enter your guess for the number (1-6): "
  number = gets.chomp
  while (number.to_i < 1 or number.to_i > 6)
	puts "Invalid number. Enter your guess for the number (1-6): "
	  number = gets.chomp
  end
	rnumber = random
	if (number.to_i == rnumber)
	  puts "You win!"
	  puts "You correctly guess #{rnumber}"
  else
	puts "You lose :("
	puts "The number was #{rnumber}"
  end
end

def random
  return 1 + rand(6)
end

def menu
  op = 1
	while (op != 2)
	  puts "1. Roll Die"
	  puts "2. Exit"
	  print "Enter your selection: "
	  op = gets.chomp
	  if (op.to_i == 1)
	  main
	elsif (op.to_i == 2)
	  break
	else
	  puts "1. Roll Die"
	  puts "2. Exit"
	  print "Invalid Selection. Please, Enter your selection: "
	  op = gets.chomp
	end
	end
end

menu

Was This Post Helpful? 0
  • +
  • -

#9 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Week #5 Challenge: Ruby

Posted 01 February 2010 - 02:00 AM

Here's a quick Mad Libs-like game I put together tonight, for an example. It's only got one story that I found by doing a Google search for 'Mad Libs'. Feel free to use it and add in the option for multiple stories.

class MadWords
  attr_accessor :place, :adjective, :verb_ing,
				:famous_person, :friend, :present_verb,
				:room, :instrument
  
  # Display the full story with submitted words
  def display_final_story
	puts "\n------ A WEIRD DREAM --------\n\n"
	puts "I fell asleep yesterday listening to an old album"
	puts "that my mom had. I really enjoyed it, though I had"
	puts "a weird dream. It was kind of like one of the old songs.\n\n"
	
	puts "I've been working' in #{place}."
	puts "All the #{adjective} day."
	puts "I've been working' in #{place}."
	puts "Just to pass the time away."
	puts "Don't you hear the whistle #{verb_ing}."
	puts "Rise up so early in the morn."
	puts "Don't you hear #{famous_person} shouting."
	puts "\"#{friend}, #{present_verb}?\"\n\n"

	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb} your horn?"
	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb} your horn?\n\n"

	puts "Someone's in the #{room} with #{friend}."
	puts "Someone's in the #{room}, I know."
	puts "Someone's in the #{room} with #{friend}."
	puts "Strumming on the old #{instrument}.\n\n"

	puts "It was the weirdest dream I've had in a long time!\n\n"
  end
  
  # Get various words for the player
  def ask_for_words
	print "Enter the name of a place: "
	self.place = gets.chomp
	
	print "Enter an adjective: "
	self.adjective = gets.chomp
	
	print "Enter a verb ending with 'ing': "
	self.verb_ing = gets.chomp
	
	print "Name of a famous person: "
	self.famous_person = gets.chomp
	
	print "Name of a friend: "
	self.friend = gets.chomp
	
	print "Enter a present-tense verb: "
	self.present_verb = gets.chomp
	
	print "Enter the name of a room: "
	self.room = gets.chomp
	
	print "Name of a musical instrument: "
	self.instrument = gets.chomp
  end
end

# Declare a new MadWords object
mad_words = MadWords.new
game_over = 0

puts "MAD WORDS!\n\n"

# Main game loop
while game_over == 0
  # The game stuff
  mad_words.ask_for_words
  mad_words.display_final_story
  
  # Another round?
  print "Would you like to do this again (y/n)? "
  response = gets.chomp
  
  if response.capitalize == "N"
	game_over = 1
  end
end

Was This Post Helpful? 0
  • +
  • -

#10 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Week #5 Challenge: Ruby

Posted 01 February 2010 - 06:57 AM

View PostSkaggles, on 1 Feb, 2010 - 01:00 AM, said:

Here's a quick Mad Libs-like game I put together tonight, for an example. It's only got one story that I found by doing a Google search for 'Mad Libs'. Feel free to use it and add in the option for multiple stories.

class MadWords
  attr_accessor :place, :adjective, :verb_ing,
				:famous_person, :friend, :present_verb,
				:room, :instrument
  
  # Display the full story with submitted words
  def display_final_story
	puts "\n------ A WEIRD DREAM --------\n\n"
	puts "I fell asleep yesterday listening to an old album"
	puts "that my mom had. I really enjoyed it, though I had"
	puts "a weird dream. It was kind of like one of the old songs.\n\n"
	
	puts "I've been working' in #{place}."
	puts "All the #{adjective} day."
	puts "I've been working' in #{place}."
	puts "Just to pass the time away."
	puts "Don't you hear the whistle #{verb_ing}."
	puts "Rise up so early in the morn."
	puts "Don't you hear #{famous_person} shouting."
	puts "\"#{friend}, #{present_verb}?\"\n\n"

	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb} your horn?"
	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb},"
	puts "#{friend}, won't you #{present_verb} your horn?\n\n"

	puts "Someone's in the #{room} with #{friend}."
	puts "Someone's in the #{room}, I know."
	puts "Someone's in the #{room} with #{friend}."
	puts "Strumming on the old #{instrument}.\n\n"

	puts "It was the weirdest dream I've had in a long time!\n\n"
  end
  
  # Get various words for the player
  def ask_for_words
	print "Enter the name of a place: "
	self.place = gets.chomp
	
	print "Enter an adjective: "
	self.adjective = gets.chomp
	
	print "Enter a verb ending with 'ing': "
	self.verb_ing = gets.chomp
	
	print "Name of a famous person: "
	self.famous_person = gets.chomp
	
	print "Name of a friend: "
	self.friend = gets.chomp
	
	print "Enter a present-tense verb: "
	self.present_verb = gets.chomp
	
	print "Enter the name of a room: "
	self.room = gets.chomp
	
	print "Name of a musical instrument: "
	self.instrument = gets.chomp
  end
end

# Declare a new MadWords object
mad_words = MadWords.new
game_over = 0

puts "MAD WORDS!\n\n"

# Main game loop
while game_over == 0
  # The game stuff
  mad_words.ask_for_words
  mad_words.display_final_story
  
  # Another round?
  print "Would you like to do this again (y/n)? "
  response = gets.chomp
  
  if response.capitalize == "N"
	game_over = 1
  end
end


Holy hardcode everything.
Was This Post Helpful? 0
  • +
  • -

#11 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Week #5 Challenge: Ruby

Posted 01 February 2010 - 08:25 AM

View PostRaynes, on 1 Feb, 2010 - 05:57 AM, said:

Holy hardcode everything.


:P Give me a break, I was coding one-handed.
Was This Post Helpful? 0
  • +
  • -

#12 marmoor1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-August 09

Re: Week #5 Challenge: Ruby

Posted 01 February 2010 - 09:12 AM

My first Ruby program. A number guessing game.
play_again = 'y'
while( play_again == 'y')
puts "Enter a number from 1 to 100."
puts "You have only 10 attempts!"
attempts = 10

guesses = []
random_number = rand(100) + 1

while( attempts > 0 )
 
  x = 0
  number = gets.chomp
  guesses.each do |guess|
	if ( number == guess )
	  puts "You've entered this number before!"
	  x = 1
	  break;
	end
  end
  
  if( x == 0 )
  guesses.push(number)
  attempts = attempts - 1


  if ( number.to_i == random_number )
	  puts "Correct"
  break

  else
	if ( number.to_i < random_number)
		puts "Too small!"
		puts 'You have ' + attempts.to_s  + ' attempts left'
	else( number.to_i > random_number )
	  puts "Too Big!"
	  puts 'You have ' + attempts.to_s  + ' attempts left'
	end
  end
  end
end#end while

if ( attempts == 0 )
  puts "You Lose!"
end

puts("Do you want to play again?(y/n)")
play_again = gets.chomp()

end


Was This Post Helpful? 0
  • +
  • -

#13 amitchhajer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 30-January 10

Re: Week #5 Challenge: Ruby

Posted 01 February 2010 - 10:15 AM

hi, i have this basic question....whats basic use of ruby?i heard it can be used in place of PHP and ASP.net
OK, even i made a number guessing game.
Next goes the splitter....
Was This Post Helpful? 0
  • +
  • -

#14 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Week #5 Challenge: Ruby

Posted 01 February 2010 - 11:25 AM

roman_numeral = []
x = 1

puts 'Enter a number to be converted to a Roman numeral'
int = gets().chomp().to_i()
original_int = int
while(x != 0)do 
	if(int >= 1000)
		r_numeral = 'M'
		int = int%1000
	elsif(int >= 500)
		r_numeral = 'D'
		int = int%500
	elsif(int >= 100)
		r_numeral = 'C'
		int = int%100
	elsif(int >= 50)
		r_numeral = 'L'
		int = int%50
	elsif(int >= 10)
		r_numeral = 'X'
		int = int%10
	elsif(int >= 5)
		r_numeral = 'V'
		int = int%5
	else
		r_numeral = 'I' * int
		x = 0
	end
	roman_numeral.push(r_numeral)
end
numeral = ''
roman_numeral.each() do |r_numeral|
	numeral = numeral + r_numeral
end
puts "#{original_int}, when converted to a Roman numeral, is #{numeral}!"


This is a Number to Roman Numeral script, functions well however I am not experienced in Ruby and am not sure how I can get it so that 4 = IV instead of IIII.

If anybody can help, please do!

This post has been edited by Shane Hudson: 02 February 2010 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Week #5 Challenge: Ruby

Posted 01 February 2010 - 03:05 PM

View Postamitchhajer, on 1 Feb, 2010 - 11:15 AM, said:

hi, i have this basic question....whats basic use of ruby?i heard it can be used in place of PHP and ASP.net


Ruby is a scripting Language that is commonly expanding in Industry, just like Python. The advantages of HLL's (High Level Languages) like Python and Ruby is that they can be used for just about everything from desktop applications like C++ and Java, to Server and web programming (PHP and ASP)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3