
-----------------------------------
MrHippo
Wed Dec 05, 2007 5:19 pm

Ruby or Python?
-----------------------------------
Hey!

I'm a beginner in programming, and I'm wondering what I should explore first. I was thinking of python, but Ruby seems to have a pretty good rep as a beginner language so I'm in a state of indecision right now and any help would be greatly appreciated :)

Also, is it better to try to learn through simply reading online tutorials or to try to set a goal in terms of writing a program and then do what you can to reach the goal (using said tutorials)? I heard something about MIT having online material regarding computer science at a beginner level too, is that worth exploring?? [edit] In fact, I actually checked out the OpenCourseWare, and the very beginner's course seems like something I might be able to do outside of school (I'm in gr. 12). It uses Scheme though, would that be worth doing or is it better to stick to internet resources?

Thanks a lot in advance!

-MrHippo

-----------------------------------
wtd
Wed Dec 05, 2007 6:33 pm

RE:Ruby or Python?
-----------------------------------
Scheme, Ruby or Python, eh?  Tough call.

Learn them all.

-----------------------------------
Clayton
Wed Dec 05, 2007 7:24 pm

RE:Ruby or Python?
-----------------------------------
Like wtd says, learning each of those languages does not go amiss. However, I know that not everyone can just pick up three languages immediately.

Personally, I would advise you to learn Ruby. It's elegancy and easy to read nature makes it a perfect fit for a beginner programmer. Plus, it's got enough interesting stuff going on later that can keep even the most experienced programmer happy for quite some time.

However, unfortunately, while I'd like to brainwash you into using Ruby, that's just not what's going to happen, so instead, I suggest that you take a look at each of the languages, learn a the basic key concepts in each of those languages and make your decision based on that.

Good Luck!

-----------------------------------
HeavenAgain
Wed Dec 05, 2007 7:49 pm

RE:Ruby or Python?
-----------------------------------
A lot of university are teaching Python first year? at least i know UoT is, might be a good choice, but still depend on yourself. I would learn them all :) NOT!!!! :p

-----------------------------------
MrHippo
Wed Dec 05, 2007 9:54 pm

RE:Ruby or Python?
-----------------------------------
Learning everything sounds good, though difficult =(

I tried out the free on-line lectures from MIT and it's pretty interesting, thought I think they use Scheme as well. My main goal was to learn about the concepts of programming through an easy-to-learn language and then apply the knowledge in order to "master" something like C++.

That seems pretty far down the road though...

-----------------------------------
HeavenAgain
Wed Dec 05, 2007 10:06 pm

RE:Ruby or Python?
-----------------------------------
getting the concepts is hard, switching to a new lanuage is not TOO hard, most syntax are pretty alike, just how you use it is different, remember programming language is only a tool, the tool master is still your head, if you get the idea, of doing things, changing to an efficient tool isnt that hard.
my suggestion is, dont aim to master one language, but instead master the logic and algorithm behind it, because language can always be out of date, but concepts cant :D just my cookies

-----------------------------------
wtd
Thu Dec 06, 2007 2:20 pm

RE:Ruby or Python?
-----------------------------------
You could learn in parallel, comparing and contrasting as you go.

For instance, simple values like integers:

Ruby, Python and Scheme:

1
2
-3
42

Pretty simple stuff.  Now, for real numbers (those with some decimal value:

Ruby, Python and Scheme:

1.0
2.13528
3.01
-0.45

Binding a value to some name.

Ruby and Python:

answer_to_everything = 42
bad_pi_approximation = 3.14

Scheme:

(define answer-to-everything 42)
(define bad-pi-approximation 3.14)

Printing "Hello, world!"

Ruby:

puts "Hello, world!"

Python:

print "Hello, world!"

Scheme:

(begin
  (display "Hello, world!")
  (newline))

Defining a function to do that.

Ruby:

def hello_world
  puts "Hello, world!"
end

Python:

def hello_world():
  print "Hello, world!"

Scheme:

(define (hello-world)
  (begin
    (display "Hello, world!")
    (newline)))

Calling those functions.

Ruby:

hello_world

Python:

hello_world()

Scheme:

(hello-world)

Creating a function that takes a name and displays an appropriate greeting, and then calling that function for "Bob".

Ruby:

def hello(name)
  puts "Hello, #{name}!"
end

hello("Bob")

Python:

def hello(name):
  print "Hello, %s!" % name

hello("Bob")

Scheme:

(define (hello name)
  (begin
    (display (string-append "Hello, " name "!"))
    (newline)))

(hello "Bob")

Using a local variable to hold the greeting text.

Ruby:

def hello(name)
  greeting = "Hello, #{name}!"
  puts greeting
end

Python:

def hello(name):
  greeting = "Hello, %s!" % name
  print greeting

Scheme:

(define (hello name)
  (begin
    (let ((greeting (string-append "Hello, " name "!")))
      (display greeting))
    (newline)))
 
Or perhaps:

(define (hello name)
  (let ((greeting (string-append "Hello, " name "!")))
    (begin
      (display greeting)
      (newline))))

There, a few different concepts, in all three languages.  That wasn't so bad, was it?  :-)

-----------------------------------
MrHippo
Thu Dec 06, 2007 3:53 pm

RE:Ruby or Python?
-----------------------------------
Hehe thanks a lot! =) Seems like a bit more work, but also sounds like it's definitely worth it in the long run!

We'll see how it goes... however, Scheme does seem less user-friendly (for beginners) than the other two, I wonder why places like Waterloo use it in some of the beginner courses instead of the others...

-----------------------------------
wtd
Thu Dec 06, 2007 3:57 pm

RE:Ruby or Python?
-----------------------------------
And just because I'm bored...

Changing the message depending on what name is given.  I'll also use a second function to generate the greeting.

Ruby:

def greeting(name)
  if name == "Clarence"
    "Hi!  Did your parents hate you?"
  elsif name == "Sid"
    "Hola!  That's a nice short name."
  else
    "Hello, #{name}!"
  end
end

def hello(name)
  puts greeting(name)
end

Or:

def greeting(name)
  case name
    when "Clarence"
      "Hi!  Did your parents hate you?"
    when "Sid"
      "Hola!  That's a nice short name."
    else
      "Hello, #{name}!"
  end
end

def hello(name)
  puts greeting(name)
end

Python:

def greeting(name):
  if name == "Clarence":
    return  "Hi!  Did your parents hate you?"
  elif name == "Sid":
    return "Hola!  That's a nice short name."
  else:
    return "Hello, %s!" % name

def hello(name):
  print greeting(name)

Scheme:

(define (greeting name)
  (cond ((string=? name "Clarence") 
         "Hi!  Did your parents hate you?")
        ((string=? name "Sid") 
         "Hola!  That's a nice short name.")
        (else 
         (string-append "Hello, " name "!"))))

(define (hello name)
  (begin
    (display (greeting name))
    (newline)))

But that doesn't account for a name like "clarence" where all of the characters are lowercase.

def greeting(name)
  case name.downcase
    when "clarence"
      "Hi!  Did your parents hate you?"
    when "sid"
      "Hola!  That's a nice short name."
    else
      "Hello, #{name}!"
  end
end

def hello(name)
  puts greeting(name)
end

Python:

def greeting(name):
  if name.lower() == "clarence":
    return  "Hi!  Did your parents hate you?"
  elif name.lower() == "sid":
    return "Hola!  That's a nice short name."
  else:
    return "Hello, %s!" % name

def hello(name):
  print greeting(name)

Scheme:

(define (greeting name)
  (cond ((string-ci=? name "Clarence") 
         "Hi!  Did your parents hate you?")
        ((string-ci=? name "Sid") 
         "Hola!  That's a nice short name.")
        (else 
         (string-append "Hello, " name "!"))))

(define (hello name)
  (begin
    (display (greeting name))
    (newline)))

-----------------------------------
Flikerator
Thu Dec 06, 2007 4:09 pm

Re: RE:Ruby or Python?
-----------------------------------
Hehe thanks a lot! =) Seems like a bit more work, but also sounds like it's definitely worth it in the long run!

We'll see how it goes... however, Scheme does seem less user-friendly (for beginners) than the other two, I wonder why places like Waterloo use it in some of the beginner courses instead of the others...

Waterloo uses its own Scheme languages to teach students (As the course goes on, the languages progress to more and more complex language variations). Also, a considerable amount of people already have programming experience so its an easy switch from OOP to functional (at least it was for me). For instance, hello world can be as simple as;

"Hello World"

A function starts as;

(define (sum n1 n2)
 (+ n1 n2))

and ends up as

(define sum
 (lambda (n1 n2)
  (+ n1 n2)))

-----------------------------------
PaulButler
Thu Dec 06, 2007 4:18 pm

RE:Ruby or Python?
-----------------------------------
Python and Ruby are both quite similar in terms of what you can do with them, at least as a beginner. Both are simple to learn and have a friendly beginner community. Choosing between the two is really just a matter of preference. Personally, I prefer Python, but either are an excellent choice for a beginner.

I don't disagree with the idea of learning them in parallel, but keep in mind that you may initially find it easier to lean them one at a time. Otherwise, you may find yourself confusing the syntax a lot and getting frustrated when your programs won't run.

Scheme is a very cool language as well, but you may find that it takes you more time and effort to create programs you can actually use (for example, a simple game) and this might reduce your motivation to learn. If you want to learn Scheme, "Teach Yourself Scheme in Fixnum Days" is a good reference, and so is "How to Design Programs" (both are available for free online).

-----------------------------------
MrHippo
Thu Dec 06, 2007 4:20 pm

RE:Ruby or Python?
-----------------------------------
X_X Seems like it could get pretty confusing, especially if I approach more advanced concepts. However, no point speculating!

*off to learn some programming*

Thanks!

-MrHippo

-----------------------------------
PaulButler
Thu Dec 06, 2007 4:26 pm

Re: RE:Ruby or Python?
-----------------------------------

Waterloo uses its own Scheme languages to teach students (As the course goes on, the languages progress to more and more complex language variations). Also, a considerable amount of people already have programming experience so its an easy switch from OOP to functional (at least it was for me).


Waterloo uses DrScheme, which is available for free and it is something you should look into if you decide to go with Scheme. Calling it Waterloo's own is a bit misleading, it is not made for Waterloo and a good number of other universities use it as well.

-----------------------------------
wtd
Thu Dec 06, 2007 5:13 pm

RE:Ruby or Python?
-----------------------------------
Scheme is less user-friendly?

Perhaps you are referring to the parentheses, and feel intimidated by them.  The beauty of Scheme is that there is very little in the way of syntax, and very very little ambiguity.

In Ruby, for instance:

puts 5 + 4 + 3 - 2

What is the order of precedence?  Does "puts 5" get evaluated first, or "5 + 4 + 3 - 2".

Of course, with an interactive interpreter it's easy enough to find out, but Scheme does not have this ambiguity at all.

(display (- (+ 5 4 3) 2))

-----------------------------------
wtd
Thu Dec 06, 2007 5:28 pm

Re: RE:Ruby or Python?
-----------------------------------
and ends up as

(define sum
 (lambda (n1 n2)
  (+ n1 n2)))

Nawww... it hardly ends there.  Imagine you want to sum a variable number of integers.

(define (sum . args)
  (letrec ((reduce (lambda (init f lst) 
                     (if (empty? lst) 
                         init
                         (reduce (f init (car lst)) f (cdr lst))))))
    (reduce 0 + args)))

:-)

-----------------------------------
wtd
Thu Dec 06, 2007 10:00 pm

RE:Ruby or Python?
-----------------------------------
And for fun, a little more.  Tell me to stop if this becomes irritating.  I may or may not listen to such a suggestion.  ;-)

Greeting a name thrice.

Ruby:

def greet_thrice(name)
  3.times do
    puts "Hello, #{name}!"
  end
end

Python:

def greet_thrice(name):
  for i in 1..3:
    print "Hello, %s!" % name

Scheme:

(define (greet-thrice name)
  (letrec ((loop (lambda (n f)
                   (when (> n 0)
                     (f n)
                     (loop (- n 1) f))))
           (greet (lambda (name)
                    (display (string-append "Hello, " name "!"))
                    (newline))))
    (loop 3 (lambda (n) (greet name)))))

Now, to greet any number of times...

Ruby:

def greet_n(name, n)
  n.times do
    puts "Hello, #{name}!"
  end
end

Python:

def greet_n(name, n):
  for i in 1..n:
    print "Hello, %s!" % name

Scheme:

(define (greet-n name n)
  (letrec ((loop (lambda (n f)
                   (when (> n 0)
                     (f n)
                     (loop (- n 1) f))))
           (greet (lambda (name)
                    (display (string-append "Hello, " name "!"))
                    (newline))))
    (loop n (lambda (n) (greet name)))))

-----------------------------------
HeavenAgain
Thu Dec 06, 2007 10:09 pm

RE:Ruby or Python?
-----------------------------------
learning all 3 at once, dont stop!!

in ruby: puts "dont stop"
in python: print "dont stop"
in scheme : 
(begin
  (display "dont stop")
  (newline))
:D

-----------------------------------
Geminias
Fri Dec 07, 2007 1:31 am

RE:Ruby or Python?
-----------------------------------
Hmm...  The beauty of scheme may be that it eliminates ambiguity of the order of operations, but at a high, in-fact, intolerable price.  

The price is this: unreadability.  

There's a difference between reading code, and figuring code out.  The difference can't be described, except to say, when you are reading code it's like a story unfolding before your eyes, figuring out code is more like watching the letters trail by your eyes.  Ruby is the former, Scheme is the ladder.  Not to say that if you program in Ruby its needless to learn how to architect code and write maintainable code because the language is so descriptive...  These things come part and parcel with writing any code in any language, but Ruby, and even Python as a tool provide the advantage over Scheme in terms of the outline it provides to enable a programmer to write self-documenting code.  

Why is this so important?  The most important thing in writing any software is managing complexity.  Languages like Scheme add unnecessary mental hurdles to the already difficult process of managing complexity.  I know that everyone knows what I'm talking about and I don't  need to express how it does this, as it is so blatantly obvious.

If you are really concerned with the order in which code is processed, you can use brackets in most languages and not worry about it.  And in cases where you can't use brackets there is but a quick google involved.  

This over the time you spend adding and removing brackets at random to get your program to merely compile, as well as, the extra time it takes to understand what the code is doing...  Well, I leave it to you to decide which is more efficient.  

DON'T PROGRAM IN SCHEME LISP etc... 
These languages are dumb.

EDIT:  Unless you find Scheme reads nicer than any other language you've ever seen.  (But I've never known anyone who has found this, probably because the only people I know are humans.)  But, by all means, if you can read Scheme easier than the other languages out there, don't listen to a word I've said.

-----------------------------------
Tony
Fri Dec 07, 2007 3:02 am

Re: RE:Ruby or Python?
-----------------------------------

This over the time you spend adding and removing brackets at random to get your program to merely compile
...
DON'T PROGRAM IN SCHEME LISP etc... 

What you have described (random brackets), is clearly not a process of programming. If your only intention is throwing together enough code to merely compile, then perhaps HTML is a better suited type of work ;)

-----------------------------------
PaulButler
Fri Dec 07, 2007 11:38 am

Re: RE:Ruby or Python?
-----------------------------------
Hmm...  The beauty of scheme may be that it eliminates ambiguity of the order of operations, but at a high, in-fact, intolerable price.  

The price is this: unreadability.  


If you only have experience reading imperative languages, yeah, LISP and Scheme will look very foreign, and code in them might be hard to follow. Scheme's syntax does more than just eliminating ambiguity. Macros, for example, are a powerful concept, that would be difficult without a LISP-like syntax (Dylan is the only language I know of that has real macros without a LISP syntax). Eliminating ambiguity is not the beauty of Scheme, it is merely a side-effect of Scheme's beauty.


There's a difference between reading code, and figuring code out.  The difference can't be described, except to say, when you are reading code it's like a story unfolding before your eyes, figuring out code is more like watching the letters trail by your eyes.  Ruby is the former, Scheme is the ladder.  Not to say that if you program in Ruby its needless to learn how to architect code and write maintainable code because the language is so descriptive...  These things come part and parcel with writing any code in any language, but Ruby, and even Python as a tool provide the advantage over Scheme in terms of the outline it provides to enable a programmer to write self-documenting code.  

Why is this so important?  The most important thing in writing any software is managing complexity.  Languages like Scheme add unnecessary mental hurdles to the already difficult process of managing complexity.  I know that everyone knows what I'm talking about and I don't  need to express how it does this, as it is so blatantly obvious.


I will give it to you that Scheme adds mental hurdles. They may be unnecessary, but they pay off in the end. Scheme doesn't increase complexity, if anything it reduces it. Being functional, Scheme allows levels of abstraction that purely imperative languages simply can't.


If you are really concerned with the order in which code is processed, you can use brackets in most languages and not worry about it.  And in cases where you can't use brackets there is but a quick google involved.  


Err, you are missing the point of Scheme again.


EDIT:  Unless you find Scheme reads nicer than any other language you've ever seen.  (But I've never known anyone who has found this, probably because the only people I know are humans.)  But, by all means, if you can read Scheme easier than the other languages out there, don't listen to a word I've said.

Scheme won't read nicer until you have used it. By all means, choose a language like Ruby or Python to start with if you want more immediate results. But don't discard the idea of ever learning Scheme because it is hard. (And trust me, once you get started, it isn't as hard as it looks.)

EDIT: fixed up quote syntax

-----------------------------------
wtd
Fri Dec 07, 2007 11:58 am

RE:Ruby or Python?
-----------------------------------
It should be noted that the last example points out something important about Scheme.  There are no loops.

Yes, you heard me right.  There are no loops in Scheme.

But, I still managed to do something repeatedly.  Instead of an imperative loop, I used recursion.

Let's consider the anatomy of an imperative loop.

for (int i = 0; i > 3; i++)
{
  std::cout  2), although I'm not sure if that would come in handy on the CCC.

- I could be wrong about this, but I thought list comprehensions were just syntax. Ruby does have list functions like map, which is one thing that list comprehensions can be used for. I don't know the full power of list comprehensions (yet), so maybe there is more to them that I am missing.

- Ruby has some basic functional capabilities, like closures and higher-order functions. I find Python's lambda more limiting than Ruby's, because Ruby treats all code blocks as the same (IIRC) but Python treats lambdas as a single statement, not a code block (that's my understanding, anyway).

- Automatic handling of big integers is a big one. It's nice to have the math taken care of without any extra effort on your part, and you don't get that in Ruby. If I remember, you don't have to deal with huge numbers in the CCC, but it's something to consider.

Personally I prefer Python myself, but it really comes down to a matter of preference, the languages are so similar in the power they give to a beginner. Maybe at higher levels they differ more, but they seem similar at the bottom.

By the way, good luck with the CCC MrHippo.

-----------------------------------
richcash
Sun Dec 09, 2007 6:24 pm

Re: Ruby or Python?
-----------------------------------
- Automatic handling of big integers is a big one. It's nice to have the math taken care of without any extra effort on your part, and you don't get that in Ruby.
Ruby definetely does do that. It's one of its advantages.

As wtd and PaulButler said, I think ruby does everything on mckenzie's list fully well.

I am not qualified to judge, but it seems a bit useless for a beginner to learn both ruby and python simultaneously. They are too similar. Wouldn't you rather learn different types of languages with different concepts?

-----------------------------------
PaulButler
Sun Dec 09, 2007 7:21 pm

Re: Ruby or Python?
-----------------------------------
- Automatic handling of big integers is a big one. It's nice to have the math taken care of without any extra effort on your part, and you don't get that in Ruby.
Ruby definetely does do that. It's one of its advantages.


Hmm, you're right, it does. It's weird, because I'm sure I remember doing something in irb just the other day that overflowed the number type, but now as hard as I try I can't get an error. I guess it was just my imagination.

-----------------------------------
MrHippo
Sun Dec 09, 2007 8:07 pm

RE:Ruby or Python?
-----------------------------------
I'm checking out Why's Guide and it's pretty interesting and well-written :) See where it takes me...

Hopefully by the end of December I'll at least have a clue how to program solutions to the easier problems I saw on the CCC website x_x

Thanks for the help everyone!

-MrHippo

-----------------------------------
Bobrobyn
Mon Dec 10, 2007 6:11 pm

Re: Ruby or Python?
-----------------------------------
I am not qualified to judge, but it seems a bit useless for a beginner to learn both ruby and python simultaneously. They are too similar. Wouldn't you rather learn different types of languages with different concepts?

I think a beginner would be better off learning one language fully (or close too), and then moving on, rather than two similiar languages like Ruby and Python but not getting as far.  I learned Java and assembly in the same semester, but they're so different that they hardly interfered with eachotherr in my head.  However, if I had to learn something as similiar as Ruby and Python at the same time, I would totally confuse the syntax.  Heh.

My opinion:  It doesn't matter which you learn first.  Just learn it well, and then learn the one you didn't learn later.
