Log In · Register

 
 
Reply to this topicStart new topic
Learn Ruby, Koans
Uronacid
post 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.
 
butre
post Jun 16 2011, 04:12 PM
Post #2


/人◕‿‿◕人\
*******

Group: Official Member
Posts: 8,283
Joined: Dec 2007
Member No: 602,927



python > ruby
 
sixfive
post Jun 16 2011, 10:56 PM
Post #3



*******

Group: Staff Alumni
Posts: 7,019
Joined: May 2008
Member No: 653,768



white > other
 
Uronacid
post Jun 17 2011, 06:52 AM
Post #4


Senior Member
******

Group: Official Member
Posts: 1,574
Joined: Aug 2007
Member No: 555,438



QUOTE(boot @ Jun 16 2011, 05:12 PM) *
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
 
butre
post Jun 18 2011, 06:10 PM
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
 
Uronacid
post Jun 20 2011, 08:50 AM
Post #6


Senior Member
******

Group: Official Member
Posts: 1,574
Joined: Aug 2007
Member No: 555,438



QUOTE(boot @ Jun 18 2011, 07:10 PM) *
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. ;)
 
mipadi
post Jun 20 2011, 12:55 PM
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:
  1. Object-oriented programming support: Python's OO constructs seem to be bolted on as an afterthought. You have to explicitly specify the target object as the first parameter to a method (a vestige of Python's poor scoping rules). There are no access protection mechanisms like "private" and "protected". Ruby has nice message-passing semantics reminiscent of Smalltalk.
  2. 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.
  3. Regular expressions: Ruby has syntactical support for regular expressions. Python doesn't.
  4. Packaging: Ruby has better packaging tools. RubyGems is better than the various Python packaging tools. RVM is better than Virtualenv.
  5. Web Tools: Ruby's web development tools are better; for example, Capistrano is a lot nicer than Fabric.
  6. Flexible syntax: Ruby's flexible syntax allows for nicer DSLs. Optional parentheses are nice, too.
  7. Shell commands: Ruby has syntactical support for calling shell commands, which makes it a lot nicer for scripting than Python.
  8. Alternative VMs: Ruby's "alternative" VMs, like Rubinius, are more mature and working to improve performance.
  9. MacRuby: Ruby has a decent compiler in MacRuby, which Python lacks.
 
Uronacid
post Jun 21 2011, 07:40 AM
Post #8


Senior Member
******

Group: Official Member
Posts: 1,574
Joined: Aug 2007
Member No: 555,438



QUOTE(mipadi @ Jun 20 2011, 01:55 PM) *
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
 
mipadi
post Jun 21 2011, 02:00 PM
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
 
Uronacid
post Jun 21 2011, 02:36 PM
Post #10


Senior Member
******

Group: Official Member
Posts: 1,574
Joined: Aug 2007
Member No: 555,438



QUOTE(mipadi @ Jun 21 2011, 03:00 PM) *
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
 

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: