r/programming • u/HornedKavu • Feb 24 '13
Ruby 2.0.0
http://www.ruby-lang.org/en/news/2013/02/24/ruby-2-0-0-p0-is-released/75
38
u/TikiTDO Feb 24 '13 edited Feb 24 '13
# Ruby 2.0:
def cycle(first_value, *values, name: 'default')
# ...
endMay 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.
14
u/galtthedestroyer Feb 24 '13
How is that different than before? ( yes I'm new. )
33
u/lobster_johnson Feb 24 '13 edited Feb 24 '13
Ruby 1.x only has positional arguments with defaults:
def foo(a, b = nil)z ... endYou 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 2There 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.
11
u/eluvium Feb 24 '13
I was confused by this one, but you've made it much clearer, thanks!
It sounds quite like Python's keyword arguments.
21
4
u/madmars Feb 24 '13
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.
4
u/jplindstrom Feb 24 '13
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.
2
u/madmars Feb 24 '13
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.
2
Feb 24 '13
Then if someone goofs...
doSomething( argrument => 'whatever' );With Ruby 2.0's keyword arguments this would cause an ArgumentError now, because
argrumentis not a known keyword.1
u/lobster_johnson Feb 24 '13
I thought Perl had keyword args — or is that just Perl 6? (Isn't Perl 6 the "current" version yet?)
3
u/madmars Feb 24 '13
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.
0
u/lobster_johnson Feb 24 '13
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.
1
u/coolHandLukeSkywalke Feb 26 '13
Wow...I was about to comment on your "then if someone goofs..." because I couldn't find the problem. That is a wicked typo!
5
u/TikiTDO Feb 24 '13
Previously the closest you could get to this behaviour was:
def cycle(first_value, *values, options) name = options[:name] || 'default' endIn 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' # ... endAs 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.
1
u/epochwolf Feb 24 '13
This is how I do it.
def cycle(*values, options={}) options = {name: 'default'}.merge(options) #... end1
u/TikiTDO Feb 25 '13
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.
4
Feb 24 '13
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!
1
7
u/postmodern Feb 24 '13 edited Feb 24 '13
They also replaced mark/sweep with optimized GC using bitmap marking to make it copy-on-write friendly!
12
u/drbrain Feb 24 '13
The GC is still mark/sweep, it now uses a bitmap to store the marks instead of flags on the object.
6
u/pnwf Feb 24 '13
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.
3
u/tuna_safe_dolphin Feb 24 '13
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.
5
u/nirvdrum Feb 24 '13
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.
1
u/Amadan Feb 24 '13
I'm not very good at low-level, so... Does this mean GIL is gone? Or at least soon-to-be-gone?
6
u/cartola Feb 24 '13
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.
1
1
u/drbrain Feb 25 '13
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.
-5
u/KagakuNinja Feb 25 '13
In other words, Matz would prefer that Ruby remain a toy language, because writing a VM is hard.
4
u/AngelLeliel Feb 25 '13
I dont think "toy language" is a good term to refer languages not put the performance as the first priority.
0
u/KagakuNinja Feb 25 '13
Today, even cell phones have multi core processors in them. Moore's law continues on, meaning the number of cores will be doubling every 3 years. A global VM lock is simply no longer acceptable for serious programming, especially for server applications.
1
u/brodock Feb 28 '13
Thread is only one way of doing multi-processing. It´s more like vertical scalling vs horizontal scalling problem. Both can do the job with they're advantages/disadvantages.
1
0
u/galtthedestroyer Feb 24 '13
What's a gc?
16
Feb 24 '13
Garbage collector. To ELI5 it goes and finds all of the memory you aren't using and releases it back to the operating system.
1
u/icewatersteam Feb 24 '13
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.
0
Feb 24 '13
[deleted]
3
u/icewatersteam Feb 24 '13
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.)
3
u/philly_fan_in_chi Feb 24 '13
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.
4
u/icewatersteam Feb 24 '13
True, it sometimes hard to get back back to that beginner mind when explains something. Thanks for pointing it out.
1
u/eek04 Feb 25 '13
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.
-1
u/OhPleaseDontBe Feb 24 '13
I don't understand why this gets downvoted.
6
u/sootzoo Feb 24 '13
i didn't downvote, but this is /r/programming and the OPs answer is a very trivial Google search away.
3
u/galtthedestroyer Feb 25 '13
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.
2
u/x86_64Ubuntu Feb 25 '13
One of the reasons a trivial question shouldn't be downvoted is because it can enhance discussion about the subject at hand.
16
Feb 24 '13
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.
18
Feb 24 '13
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.
8
Feb 24 '13
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.
3
Feb 24 '13
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
sudoto change anything, and then you have to usesudoto run anything, and… it’s just a mess.2
u/CaptainKabob Feb 24 '13
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.
5
Feb 24 '13
I think he means the system Ruby, which you do have to use
sudoto install gems with.0
u/CaptainKabob Feb 24 '13
Is there ever a reason to use the system ruby at all?
rvm use 1.9.3 --default5
Feb 24 '13
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.
2
u/CaptainKabob Feb 24 '13
...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).
2
-3
1
u/chicagobob Feb 25 '13
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-ruby1
u/CaptainKabob Feb 25 '13
I'm curious, what processes require this?
1
u/chicagobob Feb 25 '13
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.
1
Feb 24 '13
I might well have used
sudowrong 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.-1
u/CaptainKabob Feb 24 '13
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.
1
0
Feb 24 '13
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.
1
u/jes5199 Feb 25 '13
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
5
u/Mjiig Feb 24 '13
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.
4
u/jes5199 Feb 25 '13
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.
-1
-2
u/Paradox Feb 25 '13
According to shootout, its actually faster
4
1
u/igouy Feb 25 '13
According to the benchmarks game, some of the Ruby programs using Ruby 2.0 are faster than some of the Python programs using Python 3.3.0 -- and some of the Python programs are faster than some of the Ruby programs.
1
u/drbrain Feb 25 '13
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).
-4
u/aescnt Feb 24 '13
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.
13
u/ioquatix Feb 24 '13
I'm so excited by this. Congratulations to everyone who contributed to this release.
10
u/TheWakeUpCall Feb 24 '13
Where can you learn Ruby?
6
u/fjonk Feb 24 '13
If you know how to program the Ruby Koans are simply great. Of course they are not for 2.0.
14
u/TikiTDO Feb 24 '13
2
u/hak8or Feb 24 '13
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.
10
u/HelloAnnyong Feb 24 '13
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.
3
0
4
Feb 24 '13
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.
3
u/catcradle5 Feb 24 '13
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.
1
u/audiodude Feb 24 '13
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.
-1
Feb 24 '13
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.
13
u/xiongchiamiov Feb 24 '13
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!
6
u/jargoon Feb 24 '13
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.
1
u/catcradle5 Feb 25 '13
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.
3
u/earthboundkid Feb 24 '13
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.
5
u/derp_chug Feb 24 '13
Check out /r/ruby
I'm just getting my toes wet with it and really enjoying why's poignant guide.
2
u/zeekar Feb 24 '13
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.
3
u/binford2k Feb 25 '13
Glad to know that they think users can simply reinstall libraries. That whole testing bit is for weenies anyway.
2
u/ameoba Feb 24 '13
How's the backwards compatibility?
/python programmer
3
u/Kimos Feb 25 '13
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.
1
u/Paradox Feb 25 '13
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
0
u/jokubolakis Feb 24 '13
Can someone explain to a non-programmer what's new and what's ruby for?
15
u/Ziggamorph Feb 24 '13
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.
3
18
u/TikiTDO Feb 24 '13
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.
3
Feb 24 '13
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.
3
u/hak8or Feb 24 '13
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?
11
Feb 24 '13
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.
4
u/TikiTDO Feb 24 '13
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.
3
Feb 24 '13
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.
1
Feb 24 '13
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.
4
Feb 24 '13
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).
5
1
u/tardmrr Feb 24 '13
There are servers written in ruby, but you still need one (i.e. it isn't built into the language).
4
u/bjmiller Feb 24 '13
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.
-5
Feb 24 '13
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!).
10
u/TikiTDO Feb 24 '13 edited Feb 24 '13
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.
4
Feb 24 '13
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.
1
u/TikiTDO Feb 24 '13 edited Feb 24 '13
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.
2
Feb 24 '13
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.
1
u/TikiTDO Feb 24 '13
Take a look at PHP pthread.
1
Feb 24 '13
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).
→ More replies (0)0
u/Moocat87 Feb 24 '13
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.
5
Feb 24 '13
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.
1
Feb 24 '13
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.
0
u/hak8or Feb 24 '13
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!
7
u/tofueggplant Feb 24 '13
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.
2
u/jyper Feb 24 '13
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).
-2
u/clavicle Feb 24 '13
You don't compile to "x86". That's an architecture.
5
Feb 24 '13
x86 is a microcode VM instruction language, unless you're referring to 20-year-old chips.
1
1
Feb 24 '13
[removed] — view removed comment
2
u/jyper Feb 24 '13
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)
16
Feb 24 '13
What's it with all the non-programmings on this submission and why can't you all use Wikipedia?
0
Feb 24 '13
[deleted]
7
u/jokubolakis Feb 24 '13
I'm learning programming (painfully slow), so I'm interested in this stuff
7
u/jargoon Feb 24 '13
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 :)
11
1
u/weapons Feb 24 '13
So does this work with everything or are we gonna have more version fragmentation?
1
1
Feb 25 '13
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?
2
u/MagicalVagina Feb 26 '13
rvm list knownwill give you the list of the available ruby versions. Just dorvm installon one of these afterwards.1
u/Kimos Feb 25 '13
$ rvm install ruby-2.0.0-p0 $ rvm use ruby-2.0.0-p0You should be able to just say:
$ rvm install 2.0.0But that defaults to Rubinius 2 for me. I guess that's an RVM bug?
1
u/sigzero Feb 25 '13
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.
0
u/Paradox Feb 25 '13
It should have tab completion.
1
u/luikore Mar 01 '13
There are tab completions. Just look at
~/.rvm/src/rvm/scripts/completion
~/.rvm/src/rvm/scripts/zsh/Completion/_rvm
0
Feb 24 '13
Nice, they added Perl 6-style function signatures.
4
u/xiongchiamiov Feb 24 '13
You mean, Python-style function signatures?
2
Feb 24 '13
On closer inspection you seem to be right, since both those languages lack the ability to specify type constraints.
1
u/DGolden Feb 25 '13
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.
-1
Feb 24 '13 edited Feb 24 '13
The difference is, Ruby 2.0 will probably be used by more than three people. ;)
-13
Feb 24 '13
And it'll probably bring down more than three high profile sites with SQL injection exploits amirite?
Grow up.
3
Feb 24 '13
Way to miss the joke. However, ignoring that it is a fact that Perl 6 is dead in the water. It is almost half the age of Perl yet has effectively nil marketshare. The majority of Perl developers are still happily targeting Perl 5 and to my knowledge nobody ships Perl 6 out of the box instead of Perl 5.
1
-7
Feb 24 '13
I don't see any joke there. Try "tactless and unnecessary snark".
But I'll leave, since it's obvious from the numbers that the anti-Perl circlejerk is in full effect by now. All we need now is a typical sub-lucid comment from shevegen and the stereotypical Ruby Users Thread will be complete.
0
-9
u/b103 Feb 24 '13
Awesome, I'm sure this will be adopted quickly, and in a few years time there won't be anyone still using 1.x.
cough Perl 6 and Python 3 cough
9
u/theOnliest Feb 24 '13
Perl 6 doesn't yet have a stable release (by Christmas, we hear). And the jump from Perl 5 to Perl 6 is on a different scale than the jump from Ruby 1.9.3 to 2.0.0. Perl 6 is essentially a completely new language, with new syntax and a totally different backend.
1
7
u/kazagistar Feb 24 '13
It looks like ruby 2.0.0 is much more incremental then the others you mentioned. On their page it says that there should be less breakage from 1.9 to 2.0 then there was from 1.8 to 1.9.
1
4
Feb 24 '13
I don't see why not. Practically all Ruby 1.9 programs should run without modification under 2.0. This isn't a big change as the version number appears to imply as Ruby is versioned different to Perl and Python.
1
u/xiongchiamiov Feb 24 '13
It's not a surprise the Semantic Versioning spec was created by someone working a lot with Ruby. I remember upgrading from 1.9.1 to 1.9.2 and it breaking Rake - the fuck?
2
u/MatmaRex Feb 24 '13
Well, Ruby 1.8 to 1.9 was a much larger difference (and much more fuss when trying to port code, or make it work on both), and it seems like 1.9 is pretty much fully adopted now (I have yet to find a library which isn't compatible with it).
1
u/aceofears Feb 24 '13
You must be new here, upgrading things will always take a long time. If people are satisfied with what they're currently using and there is any kind of effort required there will always be some people that won't make the upgrade for what seems like an insane amount of time for people such as yourself. I don't know enough about perl 6 to comment on that specifically but this is definitely true of python 3. Backwards compatibility was broken so that the language could move forward and leave behind some of the issues that built up over the years. That means its not only a matter of getting users to want to move to the new version but also the library creators, and that will create a chicken and egg problem. Upgrading things is really not all that simple in many cases so just be patient.
-28
u/__Cyber_Dildonics__ Feb 24 '13
For when performance absolutely does not matter.
9
Feb 24 '13
You mean "most cases"? Of course. The situations where performance is of primary importance are rare, the cases where performance considerations really do dictate choice of language, even rarer.
1
u/dmitry_sychov Feb 24 '13
Thats not true. For desktop apps performance is very important, especially in the light of ultramobile underpowered computers raise.
3
Feb 24 '13
That must be why you're all writing your desktop apps in highly-optimised assembler, then.
0
3
u/bjmiller Feb 24 '13
Sometimes you need to optimize for run time, most of the time you need to optimize for development time.
6
u/esquilax Feb 24 '13
That appears to be Perl: http://benchmarksgame.alioth.debian.org/u64q/which-programs-are-fastest.php
1
u/Midasx Feb 24 '13
I swear I saw a comparison of scripting languages where perl was fastest. I think that was using examples of scripts.
5
u/Categoria Feb 24 '13
Maybe you saw it a long time ago? Ruby underwent a lot optimizations recently.
-16
Feb 24 '13
hmm so you prove shitty languages are still shitty; and that ruby is in a close 2nd.
3
u/esquilax Feb 24 '13
Um, no, it seems like JRuby's spanking the other scripting languages to me.
3
Feb 24 '13
JRuby is the only peformance-improved alternative implementation still listed on the shootout. It's a little bit biased without PyPy and LuaJIT on there. PHP could also use some more information on its setup, specifically whether APC is enabled with apc.optimization.
1
u/esquilax Feb 24 '13
I hadn't noticed that change. Why is that, do you think?
1
Feb 24 '13
Seemingly removed because the maintainer couldn't please anyone. There's a previous thread about it from a year ago. I guess JRuby was placed back in because it can use JVM libs and is a bit more than just an alternative implementation (though to be fair, that would also apply to Jython).
1
Feb 24 '13 edited Feb 24 '13
PHP could also use some more information on its setup, specifically whether APC is enabled with apc.optimization.
Benchmarks are useless unless you are benchmarking on the default settings. Anything else would be biased as there are literally millions of performance tweaks you could make which realistically nobody uses.
2
u/TikiTDO Feb 24 '13 edited Feb 24 '13
This is why you use ruby to link stuff together, and then write C extensions for the performance critical routines. Before anything else ruby is a scripting language. You need to treat it as such, and that means knowing when you need to drop to a lower level language.
-9
u/tubbo Feb 24 '13
fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiinally
8
u/zendak Feb 24 '13
You could've at least made it somewhat entertaining.
'F' + 'i' * 43 + 'nally'1
u/X8qV Feb 25 '13
'F' + 'i' * 43 + 'nally'What you find entertaining about this is a mystery to me.
1
0
Feb 24 '13
[deleted]
3
u/zendak Feb 24 '13
Yes, more Rubyish.
Warning, bad joke coming up: Enharmonically, the same as
"Gb{'i'*43}nally"0
2
35
u/darkestpart Feb 24 '13 edited Feb 24 '13
Some hands on basic examples (links are from the announcement, way at the bottom):
http://blog.marc-andre.ca/2013/02/23/ruby-2-by-example/
https://speakerdeck.com/shyouhei/whats-new-in-ruby-2-dot-0
Most complete list: https://github.com/marcandre/ruby/blob/news/NEWS.rdoc