🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

ruby language - what do you think about?

Started by
11 comments, last by GnuVince 19 years, 11 months ago
check it here or on SF so what do you think about? I have seen this language first time in my life now so I wanted to know if there is somebody with some experiences with it. please simply post your opinion thanks exa_einstein
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
Advertisement
It looks intriguing but I have never tried it. The thing that struck me about the language was that it's a fairly large download and that it's reportedly quite slow.
Quote: Original post by evolutional
(...)and that it's reportedly quite slow.

huh, very slow. hope that next version will produce more optimized code.
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
Ruby is a really fun language, and it's pretty powerful. Ruby takes a lot from Smalltalk (blocks for one) and makes it possible to do things you can't do as easily in other languages. Here's an example of a method that calculates the time required to do a task:

def time_it(█)  t = Time.now  block.call()  Time.now - tend


And now you can do things like:
puts time_it { 2**1024 }puts time_it do  s = "hello world"  s.tr("l", "x")end


And you'll get the time it took to execute the code contained in the block. Pretty cool huh? Another cool thing about Ruby that comes from Smalltalk is that you can add and modify methods to already existing classes at run-time. For example, let's say you want to add a factorial() method to the Fixnum class:

class Fixnum  def factorial    ans = 1    1.upto(self) { |i| ans *= i }    ans  endend


And now you can say:
100.factorial


So you can put methods that you need EXACTLY where they should be. In languages like C# or Java, the way to go about it is to subclass Integer and write the new method, and each time you want to find the factorial of a number, you need to cast it your new Integer class.

Ruby has a book available on the internet (www.rubycentral.com) which is extremely good, a very friendly community and some interesting projects (check out www.basecamphq.com which is written in Ruby)
I think you can add new methods to a class in Python, but there the syntax would simply be

def factorial(self):
blah

Fixnum.factorial = factorial

Which is much clumsier. This seems to be a nice extension of the "custom block approach". Python would be so very much nicer with custom blocks.

imho, Ruby is (syntactically) what python should be. The only problem I run into is that the language is made in Japan so most of the userbase writes in Japanese - but I guess that's probably how Japanese people feel about Python.
-- Single player is masturbation.
Now here's the question: can I override the "class" block to work like the Python one works, where if I call a "class" block and the class already exists, instead of adding new features to the old class it simply destroys it?

So
<code>
def class(█, name)
if exists(name)
del name
end
class(█, name);
end
</code>

In very bad Ruby (only used the language once or twice to try it out, don't really know it) - to override the global class constructor such that if a class already exists it replaces it with the new class instead of modifying it?
-- Single player is masturbation.
Quote:
I think you can add new methods to a class in Python, but there the syntax would simply be

def factorial(self):
blah

Fixnum.factorial = factorial

Don't forget setattr and getattr, which allow you to set and get attributes respectively via the attribute's name.
Quote:
Which is much clumsier.

This is one of those things where there really isn't any use for it that couldn't be accomplished cleaner by, say subclassing it. That factorial example GnuVince posted could easily be accomplished by just having a function that accepts an integer and returns the factorial.
Quote:
his seems to be a nice extension of the "custom block approach". Python would be so very much nicer with custom blocks.

IMO python makes up for that with the combination of lambda and nested functions.
Yeah, I agree that Python's nested functions are very, very nice - I'm just not fond of Lamda (the fact that it doesn't accept statements bothers me to no end) - it feels like a tacked-on kludge. The lack of custom blocks is coming back to bite them in the ass, as now they're having to design messy decorator syntax to compensate.

And yes, modifying core classes at run-time is bad coding style, but still its nice that its possible for your own use.
-- Single player is masturbation.
Python's lambda totally sucks.

Pxtl: Ruby has an abs() method for Integers. You can override it this way:
class Integer  def abs    puts "ABSOLUT VODKA!"  endend


Not hard, eh?

Quote:
This is one of those things where there really isn't any use for it that couldn't be accomplished cleaner by, say subclassing it. That factorial example GnuVince posted could easily be accomplished by just having a function that accepts an integer and returns the factorial.


I don't think it's really cleaner to do:
class FixnumFactorial < Fixnum  def fact    ans = 1    1.upto(self) { |i| ans *= i }    ans  endendx = FixnumFactorial.new(10)x.fact


factorial is an action that all Integer objects can do, so I don't see why you don't have it where it belongs and not in a stupid seperated class. As for having a function, you could do it in Ruby, because when you write a method definition that's not within a class definition, that method becomes a method of the Object class (so it is in fact added to the Object class at run-time), but some languages can't do it. For example, in Smalltalk, C# or Java, you absolutely need to put method definitions inside class definitions. You also get the added benifit that everything is always the same. Here's something about Python that has always bugged me:

len("hello".upper())


Why isn't len() a method? It seems that it woudl be more natural if it was like upper(). Don't underestimate the usefulness of having consistancy.

len isn't a method because it operates on arbitrary sequence objects. It defers implementation to that object, which often implements it as a __len__ method, but also applies to iterators too I believe.

This topic is closed to new replies.

Advertisement