Learn Ruby, Koans |
Learn Ruby, Koans |
![]()
Post
#1
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 1,574 Joined: Aug 2007 Member No: 555,438 ![]() |
Hello All,
So I've been trying to learn a new programming language (Ruby). It's like the anti-python. They're both scripting languages. However, where python tends to be very ridged and strict with regards to syntax, ruby is very lenient. In fact, I can see many programmers hating it for that very reason. Sometimes the code you write in this language can be so loose, but black magic will allow it to run properly. Anyway, there is a great tutorial which cover a good amount of the ruby language, syntax, structure, and a few functions/libraries. I encourage you all to install the language on your desktop and install Ruby Koans. Ruby works on any operating system, and the base syntax is the same for each. There are a few functions that are custom to an OS. In fact, Ruby on windows is really sweet because it is very easy to access WMI. Install: http://www.ruby-lang.org/en/downloads/ Koans: http://rubykoans.com/ I'm on the koan "about_iteration.rb". I love this language. Figured I'd share it with you guys. |
|
|
![]() |
![]()
Post
#2
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
I did this for about_scoring_project:
CODE def score(dice)
sum = 0 sum += 1000 if dice.count(1) >= 3 2.upto(6).each { |n| sum += (n * 100) if dice.count(n) >= 3 } sum += (dice.count(1) % 3) * 100 sum += (dice.count(5) % 3) * 50 end |
|
|
![]()
Post
#3
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 1,574 Joined: Aug 2007 Member No: 555,438 ![]() |
I did this for about_scoring_project: CODE def score(dice) sum = 0 sum += 1000 if dice.count(1) >= 3 2.upto(6).each { |n| sum += (n * 100) if dice.count(n) >= 3 } sum += (dice.count(1) % 3) * 100 sum += (dice.count(5) % 3) * 50 end Very cool, I'm not familiar with the "upto" method. I would have written (2..6).each My buddy from Agora Games wrote the following: CODE def score(dice) count = [0, 0, 0, 0, 0, 0] dice.each { |roll| count[roll - 1] += 1 } (count[0] / 3) * 1000 + (count[1] / 3) * 200 + (count[2] / 3) * 300 + (count[3] / 3) * 400 + (count[4] / 3) * 500 + (count[5] / 3) * 600 + (count[0] % 3) * 100 + (count[4] % 3) * 50 end I thought it was pretty interesting how he didn't declare a variable for the score, and instead used a plus sign at the end of each line. Here is my answer for the DiceSet class in about_dice_project.rb: CODE class DiceSet
attr_reader :values def roll(num_of_dice) @values = num_of_dice.times.map{ 1 + rand(6) } end end |
|
|
![]() ![]() |