PDA

View Full Version : What the hell is Ruby used for exactly?



YourSurrogateGod
September 11th, 2006, 02:33 AM
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29

I've read the above and I don't get what it's used for or why would anyone use it. I've heard the hype behind Ruby, but have no idea what it is. Anyone care to enlighten me?

goatmale
September 11th, 2006, 02:36 AM
its an extremly object orrented pearl?

Wolki
September 11th, 2006, 03:51 AM
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29

I've read the above and I don't get what it's used for or why would anyone use it. I've heard the hype behind Ruby, but have no idea what it is. Anyone care to enlighten me?

Well, it's very object oriented and quite a lot of fun. Plus, it has Rails, which many web people like (though i haven't really played with it yet).

Read Why's Poignant Guide to Ruby ( www.poignantguide.net ) for an entertaining introduction to ruby :)

maniacmusician
September 11th, 2006, 04:16 AM
Well, it's very object oriented and quite a lot of fun. Plus, it has Rails, which many web people like (though i haven't really played with it yet).

Read Why's Poignant Guide to Ruby ( www.poignantguide.net ) for an entertaining introduction to ruby :)
do you think it's graspable (sry for the word choice..tired) for a beginner? i was going to start with python (practically no experience) but ruby sounds more interesting.

shreko
September 11th, 2006, 04:30 AM
It was not so graspable for me. Seemed to me that you need a deep understanding of ruby object oriented mechanisms and syntax was not to my taste, although you can code in elegant way.

Wolki
September 11th, 2006, 04:42 AM
do you think it's graspable (sry for the word choice..tired) for a beginner? i was going to start with python (practically no experience) but ruby sounds more interesting.

Well, I'm not really an experienced coder myself (I like to read stuff about it, but don't do it that often -_- )...

Yes, I do think that beginners can understand ruby. At the beginning it's all about understanding the concepts, and ruby is quite nice there.

Python is a very good language too. I like ruby a bit better, but enjoy python nonetheless. Python's advantage is that it's more or less GNOME's official scripting language, so you can write plugins/extensions for many gnome programs with it, and it has lots of bindings and libraries. There are some introductions to Python, but I like some of the Ruby ones better, especially the Poignant Guide is a fun read.

The best is,, once you're ok with one language it's not difficult to learn the other. They have different syntax and things, but are on some level quite similar to one another.

cogsprocket
September 28th, 2006, 08:44 PM
do you think it's graspable (sry for the word choice..tired) for a beginner? i was going to start with python (practically no experience) but ruby sounds more interesting.

Ruby is extremely easy to pick up if you're not a programmer, however you will need some other training material. If you want to understand Ruby in depth you're going to need some help understanding object oriented programming fundamentals. It's easy to pick up a lot of the basics on your own but don't take my word for it try it yourself. (http://tryruby.hobix.com/) Whytheluckystiff developed this neat little tool to give you a little better understanding of how Ruby operates and interacts with what you throw at it. It's very basic, but you will learn something from it.

Where Ruby falls down, however, is that even though you may learn Ruby it doesn't really help your understanding of syntax. Ruby is very ambitious and has enjoyed quite a bit of success thanks to how easy it is to get started and even become proficient with it, however that's where the story really ends. You can't really go from Ruby to another language and pick it up quickly without getting deep into what Ruby does and why you're doin what you're doing. It may be best to learn another language like Python, PHP or even C due to the fact that these languages are more conventional in their syntax and structure.

Also, in regards to Rails (since I understand you want to learn some web development) you may find Rails a difficult animal in and of itself. While there are plenty of good tutorials on the internet for the initiated to follow, it's very difficult for the beginner to understand exactly what he has when he's trying to start out with Rails. You'll find very helpful people but you will need to have a good understanding of server administration in order to really get what you want out of Rails.

But I don't want to put you off of Ruby entirely. I'm a Ruby developer myself among many other things. The lesson I really want you to take away from this is do not try to pick one language. You need diversity if you really want to program. If you're trying to decide between Python and Ruby; don't. Do both of them. The knowledge you'll get from learning both and the experience you get from learning both is indespensible.

mostwanted
September 28th, 2006, 09:13 PM
Ruby is a concept:


1) Steal all of the cool features that make certain languages special

2) Use a minimalistic coding style utilising lots of syntactic sugar rather than the usual "function upon function" design like Python, Java, C++, Visual Basic and so on

3) Make these things fit together seamlessly (but, thus, without regard for how languages are usually constructed).

To me, Ruby is a next-gen language. Python, Perl, Tcl, SmallTalk etc. are all deprecated by Ruby. That is not to say those languages aren't still great for production use, it's just saying that Ruby and the languages to come inspired by Ruby, are going to be the next step (yes, I realise I'm a pompous Ruby fan).

Bloodfen Razormaw
September 28th, 2006, 11:20 PM
I'd have to say Ruby is a before-the-last-gen language. Ruby seems next-gen because current languages like C, C++, and Python just suck beyond words. Before that languages were expected to have Ruby's power and elegance. It wasn't until C became popular that people started expecting their languages to be primitive, and C++ taught them to expect their language to be completely broken. Ruby revived the 60 years of programming language study and the best principals it found.


Where Ruby falls down, however, is that even though you may learn Ruby it doesn't really help your understanding of syntax. Ruby is very ambitious and has enjoyed quite a bit of success thanks to how easy it is to get started and even become proficient with it, however that's where the story really ends. You can't really go from Ruby to another language and pick it up quickly without getting deep into what Ruby does and why you're doin what you're doing. It may be best to learn another language like Python, PHP or even C due to the fact that these languages are more conventional in their syntax and structure.
This is a failure of other languages, not Ruby. Ruby teaches you best practices. Many languages force you to use round-a-bout ways of doing things because they lack those features. Python and PHP especially are very poorly designed and do not result from any consideration of good language design. C is okay if you use it for what it is good for, which is nothing a beginner programmer does.

raublekick
September 29th, 2006, 01:44 AM
It's easy to pick up a lot of the basics on your own but don't take my word for it try it yourself. (http://tryruby.hobix.com/) Whytheluckystiff developed this neat little tool to give you a little better understanding of how Ruby operates and interacts with what you throw at it. It's very basic, but you will learn something from it.

Heh, using that nifty little web app, I already found a quirk with Ruby. Doing the tutorial, it shows you how to reverse a string. Cool, what a neat little trick. Later it tells you to do "40.reverse" and it fails. Why? Because reversing a number doesn't make sense? Uh... why doesn't it? It's cool that they provide a nice 4 character method to convert a number to a string, but why couldn't you just reverse a number from the beginning? I mean, hey, what's the reverse of 1234? I don't think most people would get confused and not know how to answer that. I think most people would say "easy, 4321" and not be concerned if it's a number or a string.

Bloodfen Razormaw
September 29th, 2006, 02:21 AM
You are confusing the value of 1234 with the string that represents it. The string 1234, which would be represented by a String object, is reversible. The value that string represents has no reverse. Of course, if you find you convert to strings for that purpose a lot, you could always add it yourself. Ruby leaves its classes open to editing at all times. Just do:


class Integer
def reverse
to_s.reverse.to_i
end
end

The Integer class and its subclasses Fixnum and Bignum now have a reverse method. This is an important part of Ruby, as less object oriented languages will often make you use more standard procedural style within a class, e.g. a custom method I want to operate on strings would typically be a static method that takes a string as an explicit method in Java or C#, while in Ruby you would add it to the string class and continue calling it OOP style. Ruby even lets you add a method to a single specific instance of a class.

raublekick
September 29th, 2006, 02:32 AM
yes, yes, i realize all of that. i'm just saying, that the claim "reversing a number doesn't make sense" is garbage. just like the example i gave, reversing a number isn't something that requires a strange approach to the problem. yes, technically there is a difference in the way integers and strings are represented BEHIND the scenes. but why is it odd to think that 40.reverse should output 04?

i could easily write a def that does that, and you just did, so why couldn't the ruby devs have done this?

let's say i have a list of prices, and i calculate tax on them. well, i find it annoying when a price comes to $x.01. so i want to write a little script that finds all prices that end in 01 after tax and then reduce them so that they don't end like that. wouldn't one way to approach it be to reverse all the prices and find all the ones that begin with 10? there's definitely better ways to do this problem, i'm just trying to show that reversing a number isn't some odd thing that "doesn't make sense"

i guess what i'm really asking is, what other meaning could .reverse have on a number?

Bloodfen Razormaw
September 29th, 2006, 02:50 AM
just like the example i gave, reversing a number isn't something that requires a strange approach to the problem.
You gave an example of reversing a string. Not a number. Since a number has no reverse there is no reason to add a method for it. Especially when it is as easy as to_s.reverse.to_i, in the very rare instance a user needs to do it he can easily so so. One could add hundreds of other methods that stretch the limits of reason and necessity, but it would just hurt performance for no reason.

Another meaning for reverse could be (and this is the meaning I would assume, and one which actually makes sense for a number) the number multiplied by negative one.

Edit: I'll also point out a couple of flaws in your example.
1) Ruby already lets you read from the end of a string trivially. str[-1] gets the last character, so there is no need to reverse it. No need to even know the length.
2) That kind of operation requires that you only once, in your entire program, convert the number to a string. The purpose of a standard library is to cover commonly needed functionality. There is no reason to not use to_s.reverse, or better yet to_s[-1] as stated above, when you only need to do it once. Assuming you have an array of prices, here is how you would do it using string versions of the numbers:


prices_ary.map! do |p|
if p.to_s[-2, 2] == "01" # We could more easily say if p % 1 == 0.01
p - 0.01
else
p
end
end

raublekick
September 29th, 2006, 03:06 AM
i understand what you're telling me, and and i agree it's still easy to add .to_s and .to_i, but why not just make it even easier by having that built into the int class?

i'm being a bit crazy about this because i don't think any other language has this feature either. it's just the way that the other worded it and the fact that ruby is supposed to be the so high-level, as people in this thread have said, next generation. should users of ruby need to know that a string is really a bunch of single characters and an integer is just a value (and not a bunch of single digits)?

i think 40.reverse is much more intuitive than "string" * 5

Bloodfen Razormaw
September 29th, 2006, 03:27 AM
i understand what you're telling me, and and i agree it's still easy to add .to_s and .to_i, but why not just make it even easier by having that built into the int class?
Simply because it would be very very very rarely needed, and when if you ever did need it, it would take you 10 seconds to add it. String#* makes logical sense. In math, n * 5 means n five times. If you take four of something five times you get 20. Likewise if you asked me what the string "ab" written five times looked like I would say "ababababab". On the other hand, a pure abstract value has no reverse. When you see 1234, you aren't seeing the value, you are seeing a string that refers to it. The string "1234" has a reverse, but if you wanted the string "1234" you would use a string. It is the same as how the word "dog" can't wag it's tail just because it refers to a creature that does. If they put in something as useless as reverse for Integer, why not a method which takes a float, like 12.987, and returns [[1, 2], [9, 8, 7]]? That is no more useless. They could add all sorts of crazy stuff that just mucks up the API, and before you know it you have something almost as unbearable as the Java standard libraries.

Edit: Let me just put it this way. If you think 1234 is sensibly reversed to 4321, then it also seems to make sense that 5678 concatenated to 1234 is 12345678. Should we also have concat for integers? And what is the reverse of 0b101100100? Is it 0b001001101, 001001101b0, or 653?

raublekick
September 29th, 2006, 03:38 AM
when i see "string" * 5 i don't automatically think "that means "string" written 5 times". i would naturally associate it with mathematics and think "string" multiplied 5 times... but how do you multiply a word? "string" is simply the letters s, t, r, i, n, and g, and a base10 number like 1234 is a 1000's place 1, a 100's place 2, a 10's place 3, and a 1's place 4. if you proposed 1234.reverse, i would think that it means reverse the number so that the lowest place is the highest, etc.

in a high level language that claims ease of use, a user shouldn't care that 1234 isn't really 4 digits, because most people don't think of 1234 as being abstract. if you have "1234 string" they would see the number 1234 and the word string, and know that they are different concepts, but treat them as two things that can be read the same way.

i can definitely swallow the fact that if you add reverse to the int class, then why not the float and any other class and so on... but to say that reversing a number doesn't make sense is looking from the perspective of a programmer who knows the detailed differences behind numbers and strings, not the person who has never programmed before and just sees digits and letters.

<edit>perhaps we should have a concat for integers, i dunno. and ints are way different from non-base10 numbers, they are the most commonly used. ok, now i agree that for a standard library, perhaps something not so useful should not be put in. that's fine. my major gripe is simply with the wording that reversing a number doesn't make sense, because i think it truly does make sense in layman's terms (which is what languages get closer to as they become more and more high-level, right?)

you don't need to respond anymore because i truly agree with your reasons why .reverse shouldn't be added to the int class, it's simply a gripe with the reasoning the tutorial said and i put it down on the whole language.

Wolki
September 29th, 2006, 04:00 AM
my major gripe is simply with the wording that reversing a number doesn't make sense, because i think it truly does make sense in layman's terms (which is what languages get closer to as they become more and more high-level, right?)

No. Not even people understand other people well enough, and software is even worse at reading minds.

And even in layman's terms, characters and numbers are very different - if you put a 2 at the end of a 4, you get a 42, but if you add a 2 to a 4, you get a 6. It is kinda complicated (base1 numbers would be much easier), but most people get it at a very young age. :)

What you suggest here becomes completely non-intuitive very fast: Is 4 the same as 04? Leading zeros don't have any meaning, so they must be the same. If I get a bill that says 041€, I can pay 41, get nothing back, and do not have to pay more. But is 4.reverse really the same as 40.reverse?

Bloodfen Razormaw
September 29th, 2006, 04:04 AM
<edit>perhaps we should have a concat for integers, i dunno. and ints are way different from non-base10 numbers, they are the most commonly used.
Values have no base. Ruby lets you write your integer literals in base 2, 10, and 16. You can also have your numbers printed to strings in any base, to_s merely defaults to 10. to_s(2) gives you the binary form, to_s(16) gives you hex, to_s(11) gives you base 11, etc. (up to base 36).

cogsprocket
September 29th, 2006, 06:25 AM
This is a failure of other languages, not Ruby. Ruby teaches you best practices. Many languages force you to use round-a-bout ways of doing things because they lack those features. Python and PHP especially are very poorly designed and do not result from any consideration of good language design. C is okay if you use it for what it is good for, which is nothing a beginner programmer does.

You misunderstood. What I'm saying is not that Ruby is poorly designed but rather that it shares little similarities with other languages in syntax and structure. Does that make Ruby bad? No. What it does mean, however, is that just because you have learned the Ruby language does not mean that you can quickly take what you have learned with Ruby and move over to another language like Python or one of the C subsets. The downshot of this is that while Ruby does quite a bit to help with a lot of the things that you'll learn through any good computer science curriculum, much like the computer science curriculum it doesn't give you much in the way of practical application. You learn plenty of theory but very little of how to apply it to another situation.

Here's a good example. Business America is still running on legacy CoBOL apps. There's good money to be made in maintaining the code that powers these applications if you know CoBOL. The strange truth is that CoBOL is next to useless in modern applications. Despite the fact that the business world is still running on legacy CoBOL apps, the IT world has moved on. Does that mean that knowing CoBOL is absolutely useless? No, you can make great money if you can service these apps. It would behoove you, however, to learn something that is being used. A working knowledge of C++ is almost an absolute neccessity regardless of what your feelings towards the code might be. In the end, regardless of what you know, what proves your worth is whether or not you can create a solution with what you know. Still an understanding of common syntax is a must and, frankly, the world is still very fond of the C structure.

Bloodfen Razormaw
September 29th, 2006, 12:08 PM
You misunderstood. What I'm saying is not that Ruby is poorly designed but rather that it shares little similarities with other languages in syntax and structure.
Ruby shares a great deal with other languages. It's syntax is EXTREMELY similar to Eiffel for example. It's structure is much like Smalltalk. Python, on the other hand, has very unique syntax, but you suggest it is conventional. If you are learning a language because you think the specific syntax is transferable to any other you will be sorely disappointed, since that never is the case. A Java programmer can't take his knowledge of Java and write good C# apps using Java practices, and yet C# is made to be similar to it.