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
|
|
![]() /人◕‿‿◕人\ ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 8,283 Joined: Dec 2007 Member No: 602,927 ![]() |
python > ruby
|
|
|
![]()
Post
#3
|
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Staff Alumni Posts: 7,019 Joined: May 2008 Member No: 653,768 ![]() |
white > other
|
|
|
![]()
Post
#4
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 1,574 Joined: Aug 2007 Member No: 555,438 ![]() |
python > ruby There are many people who believe python is better, and there are people who believe ruby is better. Ultimately, it is a matter of preference. I think it'd be ignorant to pass up either language. Here's a good discussion on Ruby vs. Python: http://c2.com/cgi/wiki?PythonVsRuby |
|
|
![]()
Post
#5
|
|
![]() /人◕‿‿◕人\ ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 8,283 Joined: Dec 2007 Member No: 602,927 ![]() |
I don't necessarily think python is better, I just like the strict syntax. Python has been my favorite language for a long time now so I might be a bit biased
|
|
|
![]()
Post
#6
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 1,574 Joined: Aug 2007 Member No: 555,438 ![]() |
I don't necessarily think python is better, I just like the strict syntax. Python has been my favorite language for a long time now so I might be a bit biased Ha-ha, I understand. Many do not like python for that very reason. Like you said, it's a personal preference. ;) |
|
|
![]()
Post
#7
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,647 Joined: Apr 2008 Member No: 639,265 ![]() |
Despite the fact that I learned Python first, and work as a Python programmer, I still favor Ruby over Python. I don't get caught up in the minor syntax issues (such as Python's more rigid syntax), but there are some glaring reasons why I prefer Ruby:
|
|
|
![]()
Post
#8
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Official Member Posts: 1,574 Joined: Aug 2007 Member No: 555,438 ![]() |
Consistent syntax: Despite the fact that everything in Python is an object, sometimes you call things using a function instead of a method -- for example, to get the length of an array, you call len(array) instead of array.length(). Ruby's method calling is consistent. Regular expressions: Ruby has syntactical support for regular expressions. Python doesn't. Flexible syntax: Ruby's flexible syntax allows for nicer DSLs. Optional parentheses are nice, too. Shell commands: Ruby has syntactical support for calling shell commands, which makes it a lot nicer for scripting than Python. Those are probably my main reasons for choosing Ruby over Python. Even though shell commands are different on each OS, it's still so useful to be able to utilize them right out of the box. Anyway, here is my answer for about_scoring_project.rb: CODE def score(dice) total = 0 count = [0,0,0,0,0,0,0] #count the number of times each number appears in the array dice.each {|roll| count[roll] += 1} #handle 1's if count[1] > 2 total += 1000 count[1] -= 3 end #handle other numbers count.each_with_index do |freq, number| if freq > 2 total += number * 100 count[number] -= 3 end end #handle extra 1's & 5's total += count[1] * 100 total += count[5] * 50 total end Have any of you tried Koans? What was your solution? I just made another: CODE def score(dice)
total = 0 (1..6).each { |roll| total += (roll == 1 ? 1000 : 100 * roll) if dice.count(roll) > 2 } total += (dice.count(1) % 3) * 100 total += (dice.count(5) % 3) * 50 end |
|
|
![]()
Post
#9
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,647 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
#10
|
|
![]() 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 |
|
|
![]() ![]() |