9 Replies - 9400 Views - Last Post: 05 August 2012 - 01:12 PM Rate Topic: -----

#1 Lemur  Icon User is online

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Why Ruby?

Post icon  Posted 04 August 2012 - 09:32 PM

*
POPULAR

As with many, when I first started with Ruby, I had the question. Why Ruby? Why should I bother to learn a new language when Perl and Python are already so well established? In my boredom I decided to give Ruby a try for laughs, and since then I haven't looked back.

So what is Ruby?

Ruby is a strong Object Oriented language with several functional features available for use. It's heavily inspired by Perl, Smalltalk, and LISP, making it a hybrid language.

To quote the creator of Ruby, Yukihiro Matsumoto (Matz): "I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language."

The entire concept of Ruby is based around the philosophy that programming languages are for humans to be easily able to read. Considering most time spent in programming environments is reading code, I can sympathize quite strongly with this sentiment.

So how does Ruby achieve this bold claim? Take a look at a few snippets:

array = %w(timelord cybermen dalek who tardis screwdriver)

array.include?('timelord') # true
array.empty? # false
array.count # 6



The use of the question mark is one distinctive feature of Ruby that has made boolean tests far more readable and easily understood at a glance.

So where is Ruby used?

Ruby is used in multiple places from SysAdmin tasks, to Web Development (Rails, Sinatra, etc), to Development. It's a multi use language.

At $WORK (at the time of posting this) we use Ruby to test and maintain over 900 wireless antennas, audit systems, and check for validity against the databases to alert us of discrepancies in conjunction with monitoring software.

What type of power does it have?

Given the functional parts of Ruby, and its hybrid nature, its power is only limited to what types of insanity you can possibly conjure up.

Metaprogramming is one of the defining features of Ruby. It gives you the ability to:
  • Dynamically Define Methods
  • Dynamically Call Methods
  • Overwrite Base Classes
  • Instance Evaluation
  • Overwrite Calls for a Missing Method
  • ...and more


It includes closures, first class functions, tail call recursion (when enabled), blocks, procs, lambdas, and multiple other functional techniques.

In Ruby (almost) everything is an object, and it can be redefined. Everything can be monkeypatched (though not encouraged) and you can add methods to the kernel or object itself.

Some of the greatest power comes from its dynamic metaprogramming in conjunction with the above, such as threading:

require 'timeout'
require 'socket'
require 'win32/sound'
include Win32

ip = ARGV[0] ? ARGV[0] : '127.0.0.1'
puts "Invalid IP, defaulting to localhost: #{ip}" if ip == '127.0.0.1'
sleep 2 if ip == '127.0.0.1'

number = ARGV[1] ? ARGV[1].to_i : 50
i = 0

def ping(ip)
  return system("ping -n 1 -w 1000 #{ip} > NUL")
end

def thread(method_name,ip,number)
  threads = []
  cmethod = method(method_name)
  results = 0
  
  number.times do
    threads <<  Thread.new{results += 1 if cmethod.call(ip)}
  end
  
  threads.each {|t| t.join}
  
  return results
end

while 1 == 1 do
  beginning = Time.now
  results = thread('ping',ip,number)
  puts "#{results} out of #{number} clear"
  puts Time.now - beginning
  Sound.beep(3000, 500) unless results/number.to_f >= 0.9
end



The above demonstrates a few interesting things about Ruby. The method.call can call a method by name, allowing for dynamic calls, and in this case mass generation of threads to achieve a task. The next thing to notice is the post-testing with ifs that allows you to do an inline test to determine execution.

Who should use it?

Everyone should consider Ruby at some point. I would say it was the greatest bridge for me to enter into functional programming, and has really helped me get a grasp on more advanced languages in that realm.

I've come from a .NET and C# background, and Ruby was one of the most refreshing experiences I've had with programming.

Give it a shot, and see what you think!

http://www.ruby-lang.org/en/downloads/

This post has been edited by Skaggles: 05 August 2012 - 05:06 AM


Is This A Good Question/Topic? 5
  • +

Replies To: Why Ruby?

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Why Ruby?

Posted 05 August 2012 - 06:56 AM

I've had the opportunity to work a bit with Ruby this year and have found it enjoyable and similar to Python in many ways. I've also been using CoffeeScript for almost a year and it was apparent to me very quickly that it took a LOT from Ruby and Python. I find that when I have to go back to Java I end up hating how much more I have to type. Even Groovy syntax seems cumbersome after writing Ruby or CoffeeScript:

# Valid Ruby or CoffeeScript
if message == 'hello'
    print message
else
    print 'goodbye'

// Groovy
if (message == 'hello') {
    print message
} else {
    print 'goodbye'
}

This post has been edited by Programmist: 05 August 2012 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 Lemur  Icon User is online

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: Why Ruby?

Posted 05 August 2012 - 07:19 AM

View PostProgrammist, on 05 August 2012 - 08:56 AM, said:

I've had the opportunity to work a bit with Ruby this year and have found it enjoyable and similar to Python in many ways. I've also been using CoffeeScript for almost a year and it was apparent to me very quickly that it took a LOT from Ruby and Python. I find that when I have to go back to Java I end up hating how much more I have to type. Even Groovy syntax seems cumbersome after writing Ruby or CoffeeScript:

# Valid Ruby or CoffeeScript
if message == 'hello'
    print message
else
    print 'goodbye'

// Groovy
if (message == 'hello') {
    print message
} else {
    print 'goodbye'
}


...or you can take advantage of some of Ruby's elegance with conditionals:

puts message if message == 'hello'
#or
puts 'goodbye' unless message == 'hello'


This post has been edited by Lemur: 05 August 2012 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,183
  • Joined: 21-June 11

Re: Why Ruby?

Posted 05 August 2012 - 07:29 AM

In my opinion, it's not elegant if you have to repeat the condition. I'd only recommend using postfix-if if the if you're replacing doesn't have an else block.
Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Why Ruby?

Posted 05 August 2012 - 07:36 AM

You could really be confusing:
unless message == "hello"
    puts "goodbye"
else
    puts message
end


Was This Post Helpful? 0
  • +
  • -

#6 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Why Ruby?

Posted 05 August 2012 - 07:45 AM

View PostLemur, on 05 August 2012 - 09:19 AM, said:

...or you can take advantage of some of Ruby's elegance with conditionals:

puts message if message == 'hello'
#or
puts 'goodbye' unless message == 'hello'


Yeah, CoffeeScript has that conditional syntax, so they must have taken that from Ruby as well. Pretty cool.

View Postxclite, on 05 August 2012 - 09:36 AM, said:

You could really be confusing:
unless message == "hello"
    puts "goodbye"
else
    puts message
end


Right - yeah I forgot 'puts' vs 'print.' I'm obviously no Ruby expert.

This post has been edited by Programmist: 05 August 2012 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#7 Lemur  Icon User is online

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: Why Ruby?

Posted 05 August 2012 - 07:51 AM

https://github.com/b...by-style-guide/

Great link to get up to speed on its little nuances.
Was This Post Helpful? 1
  • +
  • -

#8 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Why Ruby?

Posted 05 August 2012 - 08:00 AM

View PostProgrammist, on 05 August 2012 - 10:45 AM, said:

Right - yeah I forgot 'puts' vs 'print.' I'm obviously no Ruby expert.


Oh I wasn't trying to comment on that, just on the unnatural thought process of "unless, otherwise":

unless condition
   do this
else
   do this
end


Was This Post Helpful? 0
  • +
  • -

#9 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Why Ruby?

Posted 05 August 2012 - 08:42 AM

Yes, that is a bit strange.
Was This Post Helpful? 0
  • +
  • -

#10 Lemur  Icon User is online

  • Pragmatism over Dogma
  • member icon


Reputation: 1359
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: Why Ruby?

Posted 05 August 2012 - 01:12 PM

View Postxclite, on 05 August 2012 - 10:00 AM, said:

View PostProgrammist, on 05 August 2012 - 10:45 AM, said:

Right - yeah I forgot 'puts' vs 'print.' I'm obviously no Ruby expert.


Oh I wasn't trying to comment on that, just on the unnatural thought process of "unless, otherwise":

unless condition
   do this
else
   do this
end



Most style guides say that you should never do such a thing. Unless should only be used by itself or as a postfix.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1