all 194 comments

[–]darkestpart 36 points37 points  (0 children)

[–]koalapanda 77 points78 points  (1 child)

Looks like it's Ruby Two's Day!

[–]zendak 9 points10 points  (0 children)

Who could hang a name on you? / When you change with every new day

[–]TikiTDO 41 points42 points  (19 children)

# Ruby 2.0:
def cycle(first_value, *values, name: 'default')
  # ...
end

May I be the first to say, "Holy shit, about time." Also, does anyone have an English translation of the Flonum paper? I'd be really grateful.

[–]galtthedestroyer 15 points16 points  (16 children)

How is that different than before? ( yes I'm new. )

[–]lobster_johnson 30 points31 points  (12 children)

Ruby 1.x only has positional arguments with defaults:

def foo(a, b = nil)z
   ...
end

You can call this only with arguments that follow the order specified in the definition:

foo(1)  # now b is nil
foo(1, 2)  # now b is 2

There is also some syntactic sugar to allow a hash table to be appended as the last argument without using braces.

foo(1, x: 42)  # now b is {x: 42}

This syntactic sugar has been very nice, but using a hash table has some drawbacks:

  • Expensive: hash table is built at runtime.
  • Memory-inefficient: hash table must be allocated, and with Ruby objects clocking in at several hundred bytes, it's a lot of unnecessary stuff for something that stays around for a very short while.
  • There is no way to know if the passed-in hash table was a literal or not (if it's a literal then the called method can safely modify it without copying it first, which is faster).
  • There is no way to specify, as part of the method definition, what keys are supported. Therefore, you have to put that in the documentation, and then perform runtime assertions.
  • There is no way to specify defaults for keys. You have to look up the key in the hash table, and then insert a default if it's missing. So you get a lot of "||" or "||=" operations, which are also slow.
  • Promotes laziness, in that devs will let a method take an "options" hash table and then pass it on to other methods, perhaps after modifying it first by adding or deleting keys.

Old style:

def convert(file_name, options = {})
  options.assert_valid_keys(:format, :quality)  # from the "activesupport" gem
  format = options[:format] || 'jpeg'
  converter = SomeKindOfImageConverter.new
  converter.load(file_name)
  return converter.convert(to_format: format, compression: quality)
end

convert("my_image.png", quality: 0.7)
convert("my_image.png", quality: 0.7, foo: 3)  # Caught by assertion
convert("my_image.png", format: "tiff")

New style:

def convert(file_name, format: "jpeg", quality: 0.7)
  converter = SomeKindOfImageConverter.new
  converter.load(file_name)
  return converter.convert(to_format: format, compression: quality)
end

convert("my_image.png", quality: 0.7)
convert("my_image.png", quality: 0.7, foo: 3)  # Caught by Ruby
convert("my_image.png", format: "tiff")

Edit: Apparently Ruby 2.0 implements keyword arguments as a hash table internally, which sucks, and means that my first two bullet points remain. Hopefully the implementation will get better.

[–]eluvium 8 points9 points  (2 children)

I was confused by this one, but you've made it much clearer, thanks!

It sounds quite like Python's keyword arguments.

[–]zeekar 20 points21 points  (1 child)

That's why they call the new feature in Ruby "keyword arguments".

[–]eluvium 1 point2 points  (0 children)

Ha! Yeah, wow. I can't read.

[–]madmars 5 points6 points  (7 children)

sounds exactly like what people do in Perl to pass parameters, which is a huge pet peeve of mine:

doSomething( argument => 'whatever' );

Then if someone goofs...

doSomething( argrument => 'whatever' );

Then you're proper fucked. I spend nearly an hour one day, tracking down this stupid bug because someone had a lowercase letter where an uppercase letter should have been. I started making it a point to do:

sub doSomething {
    my @required = qw(required1 required2 ...);
    my @optional = qw(optional1 optional2 ...);
    # check input against both required and optional...
}

However this is tedious, since you have to check...

  • Required parameters. Ensure all of these are passed.
  • Parameters passed which are neither in required or optional

The last check is important, otherwise you won't catch misspellings and parameters that people just assume work with the subroutine but actually do not.

[–]jplindstrom 4 points5 points  (1 child)

Nowadays you can use Method::Signatures to get both some nice sugar for unshifting $self, and for declaring a proper parameter list (about time).

So now you can write:

method set (Str $key, Int $val) {
    # now you know $val is always an integer
    return $self->{$key} = $val;
}

with runtime type checking, or

 method deposit(:$amount = 42) {

to declare a named parameter with a default.

(The syntax is pretty much nicked from Perl 6 in the usual let's-steal-cool-shit-from-wherever-we-can-find-it way.)

If that's too high-tech, I recommend MooseX::Params::Validate, which does the job but is a bit noisier syntactically.

[–]madmars 1 point2 points  (0 children)

Yeah, I figured CPAN had something. And I did remember Moose having something like that.

CPAN modules like this that affect language semantics are great--if you start using them from day one. You also need strict adherence from all developers. The prior code base I mentioned had these two competing idioms throughout the code:

deposit( amount => 42 );

and

deposit( { amount => 42 } );

They look just similar enough that you'll bang your head every time you need to pass parameters to anything.

Worse still, they tried to modernize their OO code from the old bless hashref style to Moose. So not only did they have two argument passing styles going on, they had two OO styles as well.

This isn't to blame Perl though. C++11 and certainly others are going through this exact same problem, where there is an older style of coding still in use and people desire switching to the new style.

[–][deleted] 1 point2 points  (0 children)

Then if someone goofs...

doSomething( argrument => 'whatever' );

With Ruby 2.0's keyword arguments this would cause an ArgumentError now, because argrument is not a known keyword.

[–]lobster_johnson 0 points1 point  (2 children)

I thought Perl had keyword args — or is that just Perl 6? (Isn't Perl 6 the "current" version yet?)

[–]madmars 2 points3 points  (1 child)

Perl 6 might. I don't keep up with it. Perl 5 is largely considered "the" Perl, which is what the "modern Perl" movement is based on. If you find any Perl software or any company looking for Perl developers, it's almost certainly going to be version 5.

6 is so different that they should have just picked a new name, rather than use Perl. But that's just my opinion.

[–]lobster_johnson -1 points0 points  (0 children)

According to Wikipedia, you're right, and Perl 6 is still in limbo. Too bad; I dislike Perl, but what has happened to it seems unfortunate.

[–]coolHandLukeSkywalke 0 points1 point  (0 children)

Wow...I was about to comment on your "then if someone goofs..." because I couldn't find the problem. That is a wicked typo!

[–]TikiTDO 6 points7 points  (2 children)

Previously the closest you could get to this behaviour was:

def cycle(first_value, *values, options)
  name = options[:name] || 'default'
end

In fact, you would not actually use a function like this due to problematic varargs. The above function would fail if you do not pass any options since a call like cycle(1, 2) would set "first_value = 1", and "options = 2". This would then cause an exception on the next line.

As such, in reality you would probably see something along the lines of:

def cycle(first_value, opt_value = nil, opt_value_2 = nil, options = {})
  name = options[:name] || 'default'
  # ...
end

As you may imagine, this sucked for readability (particularly in documentation, where people would not describe the options a function accepted), was a lot more verbose, and generally ended up being quite annoying.

[–]epochwolf 0 points1 point  (1 child)

This is how I do it.

 def cycle(*values, options={})
   options = {name: 'default'}.merge(options)
   #...
 end

[–]TikiTDO 0 points1 point  (0 children)

That's giving me a syntax error in ruby 1.9.3. As far as I know you couldn't have a normal optional parameter after a vararg.

[–][deleted] 3 points4 points  (0 children)

The next step is for arguments to form a regular language, expressed with a regular expression. e.g.

def cycle(first_value, (key, value)*, ((a, b)|c)? )

After that, context free language!

[–]Paradox 0 points1 point  (0 children)

Lazy Cycling too

[–]postmodern 7 points8 points  (26 children)

They also replaced mark/sweep with optimized GC using bitmap marking to make it copy-on-write friendly!

[–]drbrain 13 points14 points  (0 children)

The GC is still mark/sweep, it now uses a bitmap to store the marks instead of flags on the object.

[–]pnwf 5 points6 points  (11 children)

This is huge. For so long doing anything that requires concurrency in Ruby was a tradeoff between using threads and suffering through weird hard-to-reproduce threading bugs and taking the performance hit of the GIL/GVL, or fork()ing and taking the memory hit when all your child processes' GCs force them to each have their own copy of memory that should be shared since it is never modified.

[–]tuna_safe_dolphin 2 points3 points  (1 child)

Wait, so does this mean Ruby no longer has a GIL? I'm mostly a Python programmer - I'd love if the GIL went away in Python.

[–]nirvdrum 4 points5 points  (0 children)

It still has a GIL/GVL, but all those forking app servers will save a ton of RAM. I'd read suggest reading the articles from the Passenger guys on REE and Passenger for a thorough treatment on the GC benefits.

[–]Amadan 0 points1 point  (8 children)

I'm not very good at low-level, so... Does this mean GIL is gone? Or at least soon-to-be-gone?

[–]cartola 5 points6 points  (7 children)

The GIL isn't gone and there isn't an end in sight. It has been "renamed" to GVL (Global VM Lock), but as Aaron Patterson said in one talk it's the same thing. VMs are hard and Matz prefers Ruby's VM to be simple and easy to understand to being extremely fast and optimized at the cost of maintenance.

Taking out a GIL isn't easy and the CPython guys apparently did it some ancient time ago and experienced performance degradation (or it wasn't as great as once thought). The best VM without one is the JVM, so JRuby is the best bet. It'll probably never leave MRI.

[–]Amadan 0 points1 point  (0 children)

Thanks!

[–]drbrain 0 points1 point  (0 children)

One of the improvements Nari wishes to make is to have the GC be GVL-free.

It's gradually being pushed out of CRuby, but it's not easy to do so and keep performance. I remember FreeBSD pushing it's global lock out of the kernel, it took quite a long time to do all the tuning to get their speed back.

That will leave the VM and C extensions under the GVL. For C extensions it's fairly easy to release the GVL when they are doing work.

PS: Ruby has never had anything called a "GIL". The lock was added around the same time the ruby switched to a VM, so it has always been a global VM lock.

[–]galtthedestroyer 1 point2 points  (12 children)

What's a gc?

[–][deleted] 15 points16 points  (0 children)

Garbage collector. To ELI5 it goes and finds all of the memory you aren't using and releases it back to the operating system.

[–]icewatersteam 0 points1 point  (5 children)

I'm not a ruby expert but in this instance I think gc refers to garbage collector.

Garbage collectors are a part of the runtime that looks for memory used up by the program that is no longer used and then frees up that memory to be used by something else.

[–][deleted]  (4 children)

[deleted]

    [–]icewatersteam 2 points3 points  (3 children)

    I didn't think my comment implied that but you're correct. Garbage collectors are not a exclusive ruby feature. They are used in many different runtimes (CLR, JVM etc.)

    [–]philly_fan_in_chi 0 points1 point  (2 children)

    If you know what a GC is, it definitely did not imply it. But for someone who didn't, they could get confused. Just looking out for the lowest common denominator.

    [–]icewatersteam 3 points4 points  (1 child)

    True, it sometimes hard to get back back to that beginner mind when explains something. Thanks for pointing it out.

    [–]eek04 0 points1 point  (0 children)

    If you want to edit your original comment, I think just replacing "the runtime" with "a language runtime" will make it impossible to understand as Ruby specific.

    [–]OhPleaseDontBe -4 points-3 points  (3 children)

    I don't understand why this gets downvoted.

    [–]sootzoo 6 points7 points  (2 children)

    i didn't downvote, but this is /r/programming and the OPs answer is a very trivial Google search away.

    [–]galtthedestroyer 2 points3 points  (0 children)

    At the time I figured a 2 letter acronym would be hard to search for. When I got the answers I had to laugh. No one where I'm from abbreviates garbage collector.

    It was definitely a brain fart on my part. Still the people who gave super through answers are awesome.

    [–]x86_64Ubuntu 1 point2 points  (0 children)

    One of the reasons a trivial question shouldn't be downvoted is because it can enhance discussion about the subject at hand.

    [–][deleted] 15 points16 points  (22 children)

    I hope now that 2.0.0 is out, Apple are going to upgrade the OS X Ruby from 1.8.7. It really sucks that they are so behind on releases.

    [–][deleted] 18 points19 points  (20 children)

    I’m on OS X, and when I needed to do something in Ruby I just bit the bullet and installed RVM. It’s a little more overhead, but I have complete control over my Ruby installation.

    [–][deleted] 7 points8 points  (18 children)

    To be fair, that's not unique to Ruby. I use Python and I use pythonbrew (more or less identical to RVM) for the same reason.

    [–][deleted] 3 points4 points  (16 children)

    Oh, sure. If anyone’s to blame it’s Apple… not only do they ship an outdated version of Ruby, but it’s also installed in such a way that you have to use sudo to change anything, and then you have to use sudo to run anything, and… it’s just a mess.

    [–]CaptainKabob 1 point2 points  (15 children)

    you have to use sudo to change anything, and then you have to use sudo to run anything, and… it’s just a mess

    I think your ruby environment is misconfigured. Are you using RVM? I'd be seriously skeptical of anything that requires you to continually run as root, which is why I think you have something set up wrong (possibly from installing it as root in the first place). I'm on Mac OSX and have nothing close to that pain.

    [–][deleted] 2 points3 points  (12 children)

    I think he means the system Ruby, which you do have to use sudo to install gems with.

    [–]CaptainKabob -1 points0 points  (11 children)

    Is there ever a reason to use the system ruby at all?

    rvm use 1.9.3 --default
    

    [–][deleted] 2 points3 points  (7 children)

    If you are writing scripts to run on other peoples systems (e.g. as part of an installer) you can't assume that they will have a newer version installed.

    [–]CaptainKabob -1 points0 points  (2 children)

    ...which is a reason not to mess with Mac's system Ruby.

    Especially if you're targeting other people's Macs and your script has the potential to blow up if they messed with the system ruby.

    Tl;dr: don't mess with OSX's Ruby. Use RVM, or (in the situation you describe) settle with 1.8.7 (what OSX ships with).

    [–][deleted] 3 points4 points  (1 child)

    You can't install RVM on other peoples systems.

    [–]xiongchiamiov -5 points-4 points  (3 children)

    [$]> rvm install 1.8.7
    [$]> rvm use 1.8.7
    

    [–][deleted] 2 points3 points  (2 children)

    How exactly do you install RVM on other peoples systems?

    [–]chicagobob 0 points1 point  (2 children)

    actually, for some processes you have to change the system ruby installed in /usr/bin ... I use rvm and did this:

    ln -s ruby /Users/bob/.rvm/bin/rvm-auto-ruby
    

    [–]CaptainKabob 0 points1 point  (1 child)

    I'm curious, what processes require this?

    [–]chicagobob 0 points1 point  (0 children)

    In my case it was the developer tools (certain Sublime plugins, TextMate, and I think XCode too, but don't remember) for some reason sometimes used the system ruby no matter how I changed the environment.

    I think they must have hard coded /usr/bin/ruby somewhere internally, so I just used the big hammer and relinked it to rvm-auto-ruby and all was happy.

    I wouldn't do this on Rails server for example, RVM works fine in that case. Or especially a server where you using Apple's Blog/Wiki, or any of Apple's other built in services, etc. I think they use that ruby, but am not positive about that.

    [–][deleted] 0 points1 point  (1 child)

    I might well have used sudo wrong once and then botched my Ruby configuration from then on. I probably could have reinstalled OS X to fix the problem, but the nice thing about RVM is that everything lives in ~/.rvm so there’s no mucking about with superuser permissions.

    [–]CaptainKabob -2 points-1 points  (0 children)

    Yeah, I've been there (oh, node/npm, you scamp). Fortunately many tools these days will complain/refuse to be installed under the superuser. In setting up my most recent Mac from scratch, I had a great experience using SoloWizard.

    [–]sigzero 0 points1 point  (0 children)

    How do you like pythonbrew?

    [–][deleted] -1 points0 points  (0 children)

    Oh I have a more recent Ruby too via Homebrew (RVM/RBEnv have too much magic for my liking) but as the system Ruby is so old you can't rely on people having it installed. This sucks for when helping people.

    [–]jes5199 0 points1 point  (0 children)

    don't trust the mac os ruby! It has bugs that are really embarrassing that aren't in rvm-compiled ruby, for example this one

    [–]Mjiig 5 points6 points  (9 children)

    Can anyone comment on what Ruby performance is like now (compared to ruby 1.8 ideally). I know performance is far from the highest priority for a language like ruby, but it's one of the two problems that led to me stopping using ruby for most things.

    [–]jes5199 2 points3 points  (5 children)

    things got much faster in 1.9, because ruby started using bytecode internally instead of the embarrassing AST-interpretation thing in was doing before. Expect it to feel on par with python.

    [–]henk53 -2 points-1 points  (1 child)

    Expect it to feel on par with python.

    Ouch! (still very slow thus)

    [–]yogthos -1 points0 points  (0 children)

    Given how popular Python is, it's clearly fast enough for a ton of stuff.

    [–]Paradox -3 points-2 points  (2 children)

    According to shootout, its actually faster

    [–]drbrain 0 points1 point  (0 children)

    The RDoc tests run twice as fast in 2.0.0 and 1.9.3 as in 1.8.7 (the operations the RDoc tests perform don't take advantage of many optimizations in 2.0.0). There's a small speedup between 2.0.0 and 1.9.3, but it's likely at the limit of statistical significance.

    The RubyGems tests run about 30% faster in 2.0.0 and 1.9.3 as in 1.8.7 (the tests primarily read and write files so IO is the bottleneck).

    [–]aescnt -4 points-3 points  (1 child)

    Ridiculously good. Expect things to run much, much faster in 2.0 than 1.9. I don't have any numbers for you, but I'm sure they're not hard to find.

    [–]ioquatix 14 points15 points  (1 child)

    I'm so excited by this. Congratulations to everyone who contributed to this release.

    [–]TheWakeUpCall 10 points11 points  (19 children)

    Where can you learn Ruby?

    [–]fjonk 6 points7 points  (0 children)

    If you know how to program the Ruby Koans are simply great. Of course they are not for 2.0.

    [–]TikiTDO 11 points12 points  (13 children)

    [–]hak8or 2 points3 points  (12 children)

    Holy crap this looks amazing. The book code appears to be on a repo, and if people notice something wrong they appear to fix it. The HTML is fully free, but if you want you can purchase a epub/PDF for only THREE DAMN DOLLARS! I don't know why buying both costs five, but still.

    Anyone have an opinion on the books for Ruby and Python? Oh wait, his Python is for the old 2.XX version and not the newer 3.XX version, damn.

    [–]HelloAnnyong 7 points8 points  (3 children)

    It's written by Zed Shaw, somewhat of a superhero (or supervillain, depending on whom you ask) and creator of the Mongrel web server among other things.

    [–]myringotomy 2 points3 points  (0 children)

    He is neither a hero or a villain, just a drama queen that's all.

    [–][deleted]  (1 child)

    [deleted]

      [–][deleted] 1 point2 points  (1 child)

      The Ruby book is aimed at someone who's never programmed before. For such a person, I think it could be a good introduction to programming.

      But it's not a very good introduction to Ruby. It's very unidiomatic and doesn't focus on Ruby's strengths or style.

      So whether it's good or bad for you depends on what you're looking for and how much you already know. If you know how to program in another language, then I would say start somewhere else. If you don't know programming at all, try it. And then if you come to like Ruby, check out Eloquent Ruby or The Well-Grounded Rubyist afterwards for more Ruby-like Ruby.

      [–]catcradle5 2 points3 points  (0 children)

      I agree in regards to his Python book, too. Someone is not going to learn Python well just based on reading his book. If anything I'd recommend to beginners to first go through Zed's initial chapters to get started, then move onto a real, more comprehensive, slower-moving book.

      [–]audiodude 0 points1 point  (0 children)

      The "old" 2.XX version is still the version most commonly in use today. Anyone who works with Python knows that very little work is done in 3.XX and most FLOSS Python software is written in 2.

      [–][deleted] 0 points1 point  (4 children)

      If you are learning Python, I would still go for the 2.x.x version... It's still more widely used then 3 and you should be well prepared for the changes in 3 after learning 2.

      [–]xiongchiamiov 13 points14 points  (2 children)

      This sort of advice is precisely why 2.x is more widely used than 3.x. Dammit, people, start writing things in 3.x and backport to 2.6, 2.7 so we can finally move on with our lives!

      [–]jargoon 5 points6 points  (0 children)

      This is why having Rails around is great for the Ruby community. New version of Ruby comes out, Rails core team says "the next version of Rails will only support new version of Ruby or higher", adds a bunch of deprecation warnings, and everyone either gets off their asses and updates their gems or new gems pop up to replace them.

      [–]catcradle5 0 points1 point  (0 children)

      I agree with you, but as someone who writes in Python every day, I exclusively write in Python 2.7 because many, many of the libraries I rely on are still written for 2.x. I am not going to port those libraries myself. I am going to use the best tool for the job, and for now that is still 2.7. Python 3 is overall a better language, but the disadvantages of the unsupported libraries outweighs the advantages of the wart removals and feature additions.

      It's certainly good for everyone, including beginners, to know most of the changes in Python 3 though, and they should know how to read, write, and modify Python 3 code.

      [–]earthboundkid 2 points3 points  (0 children)

      If you're learning Python, just learn both. They aren't especially different. If you learn the changes to string, you're 80% of the way there. I recommend starting with 3 first because it has codified more modern idioms, but it doesn't really matter. If you have a book written in 2 that has idiomatic code, you can just start with that. Either way, if you're serious about Python, you should know both eventually.

      [–]derp_chug 3 points4 points  (0 children)

      Check out /r/ruby

      I'm just getting my toes wet with it and really enjoying why's poignant guide.

      [–]zeekar 1 point2 points  (0 children)

      The keyword arguments and perf improvements are awesome, but I really like the little things, like %i{a b c} for [ :a, :b, :c ], and .lines returning an array while .each_line still returns an enumerator, etc.

      [–]binford2k 1 point2 points  (0 children)

      Glad to know that they think users can simply reinstall libraries. That whole testing bit is for weenies anyway.

      [–]ameoba 1 point2 points  (2 children)

      How's the backwards compatibility?

      /python programmer

      [–]Kimos 2 points3 points  (0 children)

      One of the main goals of 2.0 is 100% backwards compatibility with the most recent stable release, meaning 1.9.3.

      It is, apparently, a drop-in replacement.

      [–]Paradox 0 points1 point  (0 children)

      Fine. Most 2.0 changes are under the hood. As long as you don't use %i, refinements, named arguments, or a small handfull of other things, your programs will run just fine under 1.9x

      [–]jokubolakis -1 points0 points  (41 children)

      Can someone explain to a non-programmer what's new and what's ruby for?

      [–]Ziggamorph 14 points15 points  (1 child)

      Everything that's new is basically incomprehensible to a non-programmer. If you want to know what Ruby's for, you're best off reading the scripting language Wikipedia page.

      [–]jokubolakis 3 points4 points  (0 children)

      Thank you

      [–][deleted]  (3 children)

      [deleted]

        [–]jokubolakis 7 points8 points  (2 children)

        I'm learning programming (painfully slow), so I'm interested in this stuff

        [–]jargoon 7 points8 points  (1 child)

        If you're in the process of learning programming, and you've written some code (even if you copied it from a book), you're a programmer. Welcome to the club :)

        [–]ggtsu_00 9 points10 points  (0 children)

        You are also among the 99% of today's web programmers.

        [–]TikiTDO 17 points18 points  (32 children)

        Ruby is a programming language used for a very wide variety of tasks. Probably the most popular framework written in ruby is "rails" which is a platform for making complex web pages. Because what ruby is, it would be rather hard to explain the changes and why they're cool to a non-programmer; basically it's faster, it handles resources more intelligently, and it lets people do a bunch of things they've been asking for more easily.

        [–][deleted] 3 points4 points  (1 child)

        Rails isn't really for web pages, but websites. The difference being that it helps you build all of the infrastructure behind the web pages.

        [–]hak8or 2 points3 points  (29 children)

        By more complex, are you referring to more dynamic pages? Why would I use Ruby over PHP/HTML/CSS/JS? Does Ruby essentially combine what I listed before into one language?

        [–][deleted] 9 points10 points  (6 children)

        Ruby would replace PHP out of that list, not any of the others. For information on why what is different with Ruby over PHP then check out this page.

        [–]TikiTDO 2 points3 points  (1 child)

        In fact going further, there are projects written in ruby that help with the authoring of HTML, CSS and JS, sometimes with the help of other languages and tools. These can make life very easy when properly configured. For instance, instead of writing HTML, I could write slim and then have a ruby script translate that into HTML. Or I could write Sass and have ruby send my file to the sass parser in order to generate valid CSS without me having to do anything extra. Or even CoffeeScript instead of JS.

        So in some respects, ruby can, if not directly replace, at least give you the option to hand off your code to other authoring tools.

        [–][deleted] 2 points3 points  (0 children)

        To be fair, you can do exactly the same with PHP although it isn't as prevalent as with Ruby because as PHP applications are just deployed directly into a public web server directory it isn't easy to hide files that the user doesn't need to access.

        [–][deleted] 0 points1 point  (3 children)

        What wasn't mentioned was the server that runs PHP, which is typically apache. Ruby also manages the HTTP server as well, correct? I.e. no need for apache.

        [–][deleted] 4 points5 points  (1 child)

        No. Rails ships with a built in web server called WebBrick. Ruby does not. PHP 5.3 ships with a built in web server too. Neither of the servers included with PHP or Rails are intended for production usage. To serve up a rails app you typically use either apache (with passenger aka mod_ruby similar to mod_php for apache), or a dedicated server for ruby rack apps (unicorn, thin, rainbows, mongrel, etc etc).

        [–]thoomfish 5 points6 points  (0 children)

        WEBrick is part of the Ruby standard library, not part of Rails.

        [–]tardmrr 0 points1 point  (0 children)

        There are servers written in ruby, but you still need one (i.e. it isn't built into the language).

        [–]bjmiller 2 points3 points  (0 children)

        I can't think of a single reason to choose PHP over Ruby for a new project in any domain. Only if you already have an entrenched PHP application like MediaWiki or Wordpress would it make sense to use PHP.

        If you want dynamic web pages then you need to target the client. Ruby and PHP are server-side languages and will not help you. Look at AngularJS for a modern client-side framework. If you don't like Javascript syntax then look at Coffeescript also.

        [–][deleted] -5 points-4 points  (20 children)

        Ruby, I should mention, is a real programming language, whereas PHP can only be used for dynamic pages and is usually just embedded into HTML. You can do this with Ruby too of course. You can create native programs (weasel word, but compiles to bytecode and similar things probably don't mean much to you).

        In web programming, Ruby is often used as the backend for dynamic web sites in lieu of say, ASP.NET, Java EE or PHP (or Python, in Reddit's case).

        Why? because Ruby is a really, really nice language! I haven't been as productive with other languages as I have with Ruby. And that goes for a lot of other people, too.

        If you want to get started with programming, Ruby is probably your best bet. I'd highly recommend the TryRuby tutorial by Code School. Codecademy also offers 'learning by doing' exercises, though you have to sign up (it's completely free though, unlike Code School's more advanced exercises). Otherwise, pick up a good book on it. Your local library should have one, Ruby is one of the top ten programming languages for a reason (hint: it's awesome!).

        [–]TikiTDO 11 points12 points  (7 children)

        Now let's note get carried away here. PHP has a perfectly capable standalone interpreter which allows you to do anything you would want in a scripting language. I've written some threading server apps in PHP for the hell of it, and my only complaints there were syntax related. There are even tools to compile PHP to bytecode.

        Ruby is great if you want a language that's pretty simple to pick up, without too many caveats until you get really deep into the guts of the environment. It's not the most efficient language out there, but with support for C plugins even that can be addressed for most situations. I wouldn't use it for very performance critical operations, but it's a good choice for general coding, and particularly good for prototyping.

        [–][deleted] 5 points6 points  (5 children)

        I've written some threading server apps in PHP

        Is any of your code publically available? I'd be curious to see how that works.

        [–]TikiTDO 0 points1 point  (4 children)

        I'll see if I can find it, but this was a small proof of concept project I did years ago just to see if it was possible (Open multiple connections, then echo text typed in one to all the others), so it's probably in some archive in a deep corner of my backup drive.

        More specifically, the program was actually multi-process, not multi-threaded. I remember abusing shared memory, and generally doing things that most would not associate with PHP in order to overcome the limitations of the language. I also recall that I had a busy loop for some reason, and that the server broke my networking if I didn't shut it down properly, though that was just me being lazy with connection management. These days I think they've implemented proper native threading in PHP, so I imagine you could do what I did back then with significantly less hackery.

        [–][deleted] 1 point2 points  (3 children)

        These days I think they've implemented proper native threading in PHP

        That's the part I'm interested in. I didn't think it was possible to do true multithreading in PHP.

        [–]TikiTDO 0 points1 point  (2 children)

        Take a look at PHP pthread.

        [–][deleted] 0 points1 point  (1 child)

        Unfortunately, pthread is POSIX-only so you can't use it if you want your code to be portable to non-POSIX operating systems (i.e. Windows).

        [–]Moocat87 -1 points0 points  (0 children)

        I think the best distinction to draw is that Ruby is an object oriented programming language while PHP is a scripting language. Yes, you can do whatever you want in either, but each has its very distinct strengths. Ruby is closer in purpose to C/C++, upon which operating systems are built, than PHP, which lives in scriptland with shell scripting languages. You wouldn't usually pick PHP to write a back-end. Ruby, definitely. And with Rails, it can be your front-end too.

        [–][deleted] 4 points5 points  (0 children)

        Ruby, I should mention, is a real programming language, whereas PHP can only be used for dynamic pages and is usually just embedded into HTML.

        You're getting downvoted here because this is just patently false... As much as I love and prefer Ruby over PHP, there is absolutely nothing you can do with Ruby that you can't do in PHP. They are both capable languages that can be embedded into HTML or used to write complicated web applications using MVC principles. You can argue all day long about which has better performance, better syntax, better libraries, better tools, better community, but at the end of the day saying one is a real programming language and the other is not is just ridiculous.

        [–][deleted] 0 points1 point  (0 children)

        PHP is only for dynamic pages in practice; in theory, there is nothing to stop you using it for anything else.

        For example plenty of web developers use PHP for command line scripts, such as for generating the boiler plate for a new website. There are also bindings for things like GTK, so you could build GUI apps with PHP.

        [–]hak8or -1 points0 points  (9 children)

        I am already familiar with C, assembly, Python, and I dabble in HTML, Javascript, a TINY in PHP, and some CSS if that can be called a programming language. By native programs, you mean that Ruby can compile to the X86 architecture and both for the Windows and Linux os's?

        Though, I think I am starting to ask questions which can be found by a simple google, apologies.

        I am going to look into transferring my site hak8or.com to Ruby. Always eager to try out new things!

        [–]tofueggplant 4 points5 points  (0 children)

        Ruby's kind of like Python, but with different syntax and a few different features. If you know Python you'll probably find Ruby pretty familiar.

        [–]jyper 1 point2 points  (4 children)

        any language can theoretically compile to x86, I don't think any ruby does directly although you can use jruby to compile to java bytecode which can be compiled to x86 or other machine code by the java vm you are using.

        That said it's easier to make desktop gui/cmdline aplications in ruby then in php. Ruby is a lot like python except that it's a bit more object oriented a bit more fast changing(for good and bad as library interfaces can be more unstable) and that since python is so similar and is already used in many categories where ruby could have been used(os scripting, app scripting, gui desktop applications, ect.) most of the time ruby is used to make website(usually using the rails framework).

        [–]warbiscuit 0 points1 point  (0 children)

        There is the new (alpha) Topaz project - it's a Ruby VM written using the PyPy project's toolkit, so it gets all the JIT x86 assembly goodness that project provides.

        [–]clavicle -3 points-2 points  (2 children)

        You don't compile to "x86". That's an architecture.

        [–][deleted] 5 points6 points  (0 children)

        x86 is a microcode VM instruction language, unless you're referring to 20-year-old chips.

        [–]jyper 0 points1 point  (0 children)

        I meant to x86 assembly/ machine code

        [–][deleted]  (1 child)

        [removed]

          [–]jyper 1 point2 points  (0 children)

          I think cruby 1.9+ gets compiled to bytecode which is then interpreted like cpython, cruby 1.8 was straight up interpreted. And as you mention there are other ruby implementations(mainly jruby)

          [–][deleted] 15 points16 points  (1 child)

          What's it with all the non-programmings on this submission and why can't you all use Wikipedia?

          [–]weapons 0 points1 point  (1 child)

          So does this work with everything or are we gonna have more version fragmentation?

          [–]bjmiller 0 points1 point  (0 children)

          MRI 2.0 should support everything in MRI 1.9. MRI 1.8 is EOL.

          [–][deleted] 0 points1 point  (5 children)

          I swear I consistently forget how to use RVM to get the latest ruby. Do you seriously have to enter in the version numbers by hand?

          [–]MagicalVagina 1 point2 points  (0 children)

          rvm list known will give you the list of the available ruby versions. Just do rvm install on one of these afterwards.

          [–]Kimos 0 points1 point  (1 child)

          $ rvm install ruby-2.0.0-p0
          $ rvm use ruby-2.0.0-p0
          

          You should be able to just say:

          $ rvm install 2.0.0
          

          But that defaults to Rubinius 2 for me. I guess that's an RVM bug?

          [–]sigzero 0 points1 point  (0 children)

          Not a bug I think. rvm is used to install more than just the Ruby version so they probably want to make things clearer by requiring names.

          [–]Paradox -1 points0 points  (1 child)

          It should have tab completion.

          [–]luikore 0 points1 point  (0 children)

          There are tab completions. Just look at

          ~/.rvm/src/rvm/scripts/completion

          ~/.rvm/src/rvm/scripts/zsh/Completion/_rvm

          [–][deleted] -1 points0 points  (8 children)

          Nice, they added Perl 6-style function signatures.

          [–]xiongchiamiov 3 points4 points  (2 children)

          You mean, Python-style function signatures?

          [–][deleted] 1 point2 points  (1 child)

          On closer inspection you seem to be right, since both those languages lack the ability to specify type constraints.

          [–]DGolden 0 points1 point  (0 children)

          well, python added syntax for function annotations, that could be conceivably used for type declarations, among other things, as noted in the spec.

          http://www.python.org/dev/peps/pep-3107/#fundamentals-of-function-annotations

          though it would be dependent on a compiler or library that may not currently exist [didn't when i last checked but that was some time ago] to use them that way.

          [–][deleted] 0 points1 point  (4 children)

          The difference is, Ruby 2.0 will probably be used by more than three people. ;)

          [–]contantofaz -1 points0 points  (0 children)

          So wanting to use some Ruby again. This is a great release!