Learn Ruby, Koans |
Learn Ruby, Koans |
Jun 16 2011, 06:54 AM
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. |
|
|
Jun 20 2011, 12:55 PM
Post
#2
|
|
Senior Member Group: Administrator Posts: 2,648 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:
|
|
|
Jun 21 2011, 07:40 AM
Post
#3
|
|
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 |
|
|