Ruby

[21:03] <Riddell> ladies and gentlmen...
[21:04] <Riddell> my very favourite apachelogger is going to introduce us to Ruby
[21:04] <Riddell> go apachelogger!
[21:04] <apachelogger> Well, rgreening did intro himself, something I didn't plan on :S
[21:04] <apachelogger> https://edge.launchpad.net/~apachelogger
[21:04] <apachelogger> that is me
[21:04] <apachelogger> and I am the Chief Unicorn of Kubuntu
[21:04] <apachelogger> meaning, I am responsible for breaking things so that others have something to work on
[21:05] <apachelogger> well, not seriously, but that is also not why you are here, right?
[21:05] <apachelogger> so
[21:05] <apachelogger> Who is here for the ruby talk?
[21:05] <IndigoJo> me!
[21:05] <Kabal458> me!
[21:05]  * mzaugg applauds
[21:05] <ulysses__> me!
[21:05] <Dhraakellian> me
[21:05] <PolyC> me
[21:05] <micmord> me too
[21:05] <WielkieG> simply me too
[21:05] <Monika|K> me
[21:05] <mattbd> me
[21:05] <pan_de_> me
[21:05] <gcala> me
[21:05] <adrian__> me
[21:05] <Dhraakellian> actually, I thought it was going to be a python talk! ohnoes! ;)
[21:05] <CarotNL> me
[21:05]  * YenTheFirst <-
[21:05] <nantal> me
[21:05] <dvda> me too
[21:05]  * Quintasan is raising slowly form his steat, ME!
[21:05] <shadeslayer> mee
[21:06] <Quintasan> s/steat/seat
[21:06] <apachelogger> Good, I depend on your questions
[21:06] <apachelogger> everytime I talk about something, the content is worth 10 minutes
[21:06] <apachelogger> so unless you want a 50 minutes break you better ask a lot of questions
[21:06] <apachelogger> though
[21:07] <apachelogger> let's get started
[21:07] <apachelogger> # I LUV MY RUBY!!!!!
[21:07] <apachelogger> #
[21:07] <apachelogger> # With Apachelogger aka Harald
[21:07] <apachelogger> talk = Apachelogger::Talks::Ruby.new(parent="apachelogger")
[21:07] <apachelogger> talk.start
[21:07] <apachelogger> that is fancy for: I am going to start ;-)
[21:07] <apachelogger> talk.say_hello
[21:07] <apachelogger> Hello and welcome to my monologue about my ruby.
[21:07] <apachelogger> #################################################################################################
[21:07] <apachelogger> talk.install_deps(["ruby","irb","ruby-kde4"])
[21:07] <Dhraakellian> (so if this tutorial is more KDE-specific than kubuntu-specific...)
[21:08] <apachelogger> First we ought to get dirty and install stuff necessary for this to work properly. So please all run the following command in a terminal
[21:08] <apachelogger> sudo apt-get install ruby irb ruby-kde4
[21:08] <apachelogger> Dhraakellian: isn't it the same anyway? ;-)
[21:08] <apachelogger> That command should equip you with everything you'll need for basic Ruby(KDE) development.
[21:08] <apachelogger> REALLY IMPORTANT!
[21:08] <Dhraakellian> apachelogger: there are other KDE distros
[21:09] <Dhraakellian> it's all a matter of which computer I'm working from
[21:09] <apachelogger> ................
[21:11] <apachelogger> ok, lets go ahead
[21:11] <apachelogger> #################################################################################################
[21:11] <apachelogger> talk.why_ruby
[21:12] <apachelogger> First, lets sort one of the most important questions in all the universe. Why would I want to use Ruby. The answer is considerable simple.
[21:12] <apachelogger> You want to use Ruby because C is a PITA, C++ code is bloated, C# is slow, Python code is a mess and Ruby code is simply beautiful. It's as simple as that :P
[21:13] <apachelogger> #################################################################################################
[21:13] <apachelogger> talk.basics
[21:13] <apachelogger> While you're downloding all that stuff I'll just go ahead and outline some basics.
[21:13] <apachelogger> a) irb = interactive ruby (shell)
[21:13] <apachelogger> b) irb is especially used for code testing
[21:14] <apachelogger> c) irb is super verbose and likes to call almost everyone nil :P
[21:14] <apachelogger> quite rude TBH
[21:14] <apachelogger> Everyone finsihed downloading now?
[21:14] <apachelogger> start irb from a terminal
[21:14] <apachelogger> irb
[21:14] <apachelogger> easy, huh? ;-)
[21:15] <pan_de_> irb(main):001:0>
[21:15] <apachelogger> ######### UNO
[21:15] <apachelogger> 2+3
[21:16] <apachelogger> I know without using irb that this returns 5 ;-)
[21:16] <micmord> => 5
[21:16] <apachelogger> if it does not, your PC is broken and you should buy a new one
[21:16] <apachelogger> visit dell.com for options
[21:16] <apachelogger> </ad>
[21:16] <apachelogger> 3/2
[21:16] <apachelogger> what does that return?
[21:16] <IndigoJo> 1
[21:16] <apachelogger> AHHHH!!! broken!!!!
[21:17] <shankhs> 3.0/2.0
[21:17] <apachelogger> shankhs: yep, very good
[21:17] <IndigoJo> => 1.5
[21:18] <apachelogger> you don't need to make it 3.0/2.0 but any of those needs to be float to return a float
[21:19] <apachelogger> ######### Hola!
[21:19] <apachelogger> Next take a look at strings:
[21:19] <apachelogger>   "Hola!"
[21:20] <apachelogger> Clearly the "" make the string a string, like when we reuse the calculation from above:
[21:20] <apachelogger> let's try something different
[21:20] <apachelogger>   "2" + "3"
[21:20] <apachelogger> Fancy? Ain't it? ;-)
[21:20] <dvda> => "23"
[21:20] <apachelogger> Though, we might want to output that stuff to the ouside world:
[21:21] <apachelogger>   puts "Hola World!
[21:21] <Dhraakellian> is there any difference between ' and "?
[21:21] <apachelogger> Dhraakellian: not really
[21:22] <apachelogger> Now you might wonder why the method is called puts, while it is called print almost every other programming language. To answer that, just try with print:
[21:22] <apachelogger>   print "Hola World!"
[21:23] <apachelogger> The most visible difference is that puts attaches a newline (i.e. line break) to the string, while print does not, so in almost all cases puts > print.
[21:25] <apachelogger> Of course you can do all sorts of fancy things with a string:
[21:25] <apachelogger>   puts "Hola World!".reverse.upcase
[21:25] <apachelogger> basic encryption that is :P
[21:25] <odites> !DLROW ALOH
[21:26] <apachelogger> ######### Everything comes to an end :(
[21:26] <apachelogger> In Ruby, everything comes to an end, quite literally, for example an if statement looks like this:
[21:26] <apachelogger>   if 0 == 0
[21:26] <apachelogger>     puts "Hola World"
[21:26] <apachelogger>   end
[21:26] <apachelogger> This applies to about everything you start... if, for, class, module...
[21:27] <apachelogger> ######### ...unless you are trying to write a book in Ruby syntax...
[21:27] <apachelogger> In Ruby you can also do:
[21:27] <apachelogger> puts "Hola World" if 0 == 0
[21:27] <apachelogger> Sounds a lot nicer, looks a lot nicer, is a lot nicer. "if not" is also not nice enough, we use "unless":
[21:27] <apachelogger> puts "Hola World" unless 0 == 1
[21:27] <apachelogger> And while we are writing a book:
[21:28] <apachelogger> 3.times { print "Oh dear!\n" }
[21:28] <apachelogger> just without the brackets
[21:29] <apachelogger> We learn: write books in Ruby syntax and you'll probably be pronounced nerd of the century.
[21:29] <apachelogger> Really something you want to do
[21:29] <apachelogger> #################################################################################################
[21:29] <apachelogger> talk.kde_basics
[21:29] <apachelogger> Now we get to the really nice stuff :P
[21:30] <apachelogger> You can obtain a simple hello world application at http://aplg.kollide.net/kubuntu/ktd-ruby/hola1.rb
[21:30] <apachelogger> just save it some directory and be ready to run it
[21:30] <apachelogger> You can do that by ruby hola1.rb
[21:32] <apachelogger> ruby hola1.rb
[21:32] <khaije|amalt> apachelogger: is the button supposed to do anything?
[21:32] <apachelogger> nope
[21:33] <apachelogger> Please open it in your editor of choice
[21:34] <apachelogger> We're going to walk through it, so we have at least covered the boring basics
[21:34] <apachelogger> If you know a bit about KDE programming, there won't be anything new in here, the ruby syntax is not too different either :)
[21:34] <apachelogger> The first thing you'll need to do when creating a RubyKDE application is require korundum4...
[21:35] <apachelogger> so really, if the require fails, the app fails
[21:36] <apachelogger> require is the method of choice when loading a library into your application, in this case we require korundum4, which is the all fancy name for KDE's ruby bindings.
[21:36] <apachelogger> shadeslayer: symbol viewer most importantly
[21:37] <apachelogger> Now that we have required korundum ... really, don't ask me about the name ...
[21:37] <apachelogger> Next are the about data and command line argument handling, for about any application you'll need this portion or Ruby will blow up. Fortunately you can copy that stuff and edit as necessary.
[21:37] <apachelogger> Here we also see the first variable
[21:37] <apachelogger> Called 'about'
[21:38] <apachelogger> So, who doesn't know what object oriented programming is?
[21:38] <apachelogger> because, we really need to clear that before we can continue
[21:39] <apachelogger> not that kind of object :P
[21:39] <apachelogger> Simply put: what we do is create objects and let them interact
[21:39] <rdale_> another useful printing method is called 'p' - you can try 'p about'
[21:39] <apachelogger> an object holds certain properties that we can influence, or not
[21:39] <apachelogger> depending ont he underlying construction plan of that object
[21:39] <apachelogger> this construction plan is called a class
[21:40] <shankhs_> nantal: go to http://www.aonaware.com/OOP1.htm nice place to start
[21:40] <apachelogger> KDE::AboutData is a class
[21:40] <apachelogger> we use it to create an object with the name about
[21:40] <apachelogger> that is basically what line 6 does
[21:41] <apachelogger> lets jump right ahead to line 12
[21:41] <apachelogger> Now comes the fun part. First it creates an object "a" of the class KDE::Application (aka KApplication). For both KDE and Qt classes the name scheme is KDE:: or Qt::, so QWidget would be Qt::Widget.
[21:42] <apachelogger> So again, we have the class KDE::Application which is the construction plan we use to create the object.
[21:43] <apachelogger> The KApplicaiton object is followed by the creation of a "hello" object of the class Qt::PushButton which is ultimately able to use a translated string (considering it is available ;-)).
[21:44] <shadeslayer> apachelogger: what does a.i18n do??
[21:44] <apachelogger> allows the string to be translated via the KApplication object
[21:45] <apachelogger> so, this hello world application could actually be translated into another language ;-)
[21:45] <apachelogger> Then it just defines hello as the application's top widget, displays hello and executes the application.
[21:45] <javi> OT, what does i18n stands for? i can understand l10n, but no idea of i18n
[21:45] <WielkieG> what's the second argument of Qt::PushButton.new?
[21:45] <apachelogger> javi: internationization
[21:47] <apachelogger> at times you want or need to do it later
[21:47] <apachelogger> for example a.topWidget = hello
[21:47] <apachelogger> we influence the topWidget property of the KApplication
[21:47] <apachelogger> shankhs_: api.kde.org ;-)
[21:48] <apachelogger> so, let's move on, or we aren't gonna make it ;-)
[21:48] <apachelogger> #################################################################################################
[21:48] <apachelogger> talk.kde_advanced
[21:48] <apachelogger> halo1.rb is really boring stuff, so let's take a look at more advanced Ruby syntaxing. You can find hola2.rb at http://aplg.kollide.net/kubuntu/ktd-ruby/hola2.rb
[21:48] <apachelogger> again you can run it with ruby hola2.rb
[21:48] <apachelogger> Then open it in an editor, so we can walk through it again.
[21:49] <apachelogger> hola2.rb is actually based on hola1.rb
[21:49] <apachelogger> The advanced version still requires korundum4, and there didn't change a whole lot at the bottom either. So lets take a look at those changes first.
[21:49] <apachelogger> a) "a" became $aPony
[21:49] <apachelogger> b) Instead of our Qt::PushButton it now uses My::Widget????
[21:49] <apachelogger> actually Insane::Widget :D
[21:50] <apachelogger> much more fancy name
[21:50] <apachelogger> I'll explain a) later, b) however is quite easy to figure out. Above the old code I have added a module Insane and a class Widget, which implements a Qt::Widget.
[21:50] <apachelogger> so, the class is the "construction plan" of this new widget
[21:50] <apachelogger> So, first we need to find out what a module is. Simply put: a module is some kind of namespace. It can contain classes as well as methods or constants. So, really a module is for the better part just a logical group. For example the group Qt contains all Qt classes, methods and constants...
[21:50] <shankhs_> whats this: #!/usr/bin/env ruby
[21:51] <IndigoJo> shankhs: it tells bash to run the script using ruby
[21:51] <apachelogger> shankhs_: the fancy version of #!/usr/bin/ruby
[21:51] <apachelogger> Though, modules have another great advantage. They provide Mixins. Explaining this in depth would take quit some time, so let's just say a Mixin is a really fancy thing you can use across classes to provide similar methods etc.
[21:51] <apachelogger> The french electro duo Justice also does a lot of mixins, though I doubt that is related to Ruby.
[21:51] <apachelogger> If you want to go serious with Ruby, you really should be checking out Mixins, though it's not necessary for this tutorial :)
[21:53] <apachelogger> Every Ruby class got an initialize method, whether you have one in your code or not, whether you like it or not, really, Ruby is quite the dictator in this regard. This method gets called once you create an object using new() (as hola2.rb does using My::Widget.new). Our Widget class is a special example because it implements a Qt::Widget, so we need to ensure the original initialize of Qt::Widget gets taken into account. We
[21:53] <apachelogger>  archive this by using "super". So whenever you implement a KDE or Qt widget you'll have to call at least super in the initialize method, otherwise your application will probably explode.
[21:53] <apachelogger> The following stuff is really Qt basics. The application creates a vertical box layout (Qt::VBoxLayout) and adds to that layout a label, a lineedit (i.e. a text field to enter text) and the old hello button.
[21:54] <apachelogger> You should be noticing some things here. First of all we are referencing $aPony, and this is the reason the variable got a $ to begin with :)
[21:54] <apachelogger> Ruby knows 4 variable types (though technically 6), they are differing by their scope. The 3 most important ones you'll need are the local, instance and global variable. So what would they be looking like?
[21:55] <apachelogger> local: aPony
[21:55] <apachelogger> instance: @aPony
[21:55] <apachelogger> global: $aPony
[21:55] <apachelogger> When do I use which one?
[21:55] <apachelogger> local: always, unless you need another one
[21:55] <apachelogger> instance: when you need to share a variable across methods in one class instance (e.g. @lineEdit in hola2.rb, which is created in the initialize method and checked in the hi method)
[21:56] <apachelogger> global: you shouldn't ;-) it is accessibile from everywhere within the application, I only used one to showcase it really
[21:56] <apachelogger> Another thing you should notice in the present code is that Ruby does not care all that much about brackets (well, most of the time anyway ;-)). This is showcased by "addWidget label" vs. "addWidget(@lineEdit)".
[21:56] <apachelogger> And the last important thing is that KDE's Ruby bindings are quite dynamic in property editing. For example you can use "text = 'something'" as well as "setText 'somethign else'".
[21:57] <apachelogger> Let's move on...
[21:57] <apachelogger> oh dear... time!!!
[21:57] <apachelogger> ....
[21:57] <apachelogger> Last, but not least we will take a look at Qt's signal and slots system in Ruby. If you don't require arguments for the methods, you just create a new method using
[21:57] <apachelogger>   def name
[21:57] <apachelogger>     code
[21:57] <apachelogger>   end
[21:57] <apachelogger> and add "slots :name" somewhere in your code, or "signals :name".
[21:58] <apachelogger> Then you just need to use connect (as shown in the code at hand) and you're done :)
[21:58] <apachelogger> #################################################################################################
[21:58] <apachelogger> talk.questions.each{|question|
[21:58] <apachelogger>   talk.answer(question) unless question.downcase.include?("python")
[21:58] <apachelogger> }
[21:58] <apachelogger> Nail me!
[21:58] <YenTheFirst> how would you rewrite Insane::Widget without $aPony ?
[21:59]  * Dhraakellian grabs a hammer
[21:59] <apachelogger> YenTheFirst: you could pass it to each method that needs it for example
[21:59] <khaije|amalt> what do the local, instance and global scopes coorespond to specifically ?
[21:59] <apachelogger> YenTheFirst: though, in most cases it is easiest to use it as a global
[21:59] <nhn> heya
[21:59] <nhn> :-D
[21:59] <micmord> i got an error with --author option
[21:59] <YenTheFirst> also, is there agood tutorial on QT programming somewhere? I know my ruby, but not my QT
[21:59] <shankhs_> apachelogger: but declaring global in OOP is dangerous
[22:00] <Riddell> I see we're very lucky in having the main man behind Ruby on KDE present here today, the amazing rdale!
[22:00] <rdale_> KDE::Application is always save in a global variable called '$kapp' if you want to use that
[22:00] <apachelogger> khaije|amalt: correspond in which context? they are both symbols to the objects but eventually just limited by their scope
[22:00] <sdein> how to asign a method/function to a variable?
[22:00] <apachelogger> micmord: we didn't define an author really :)
[22:00] <javi> YenTheFirst: qt documentation is great, it is well documented and has a lot of examples
[22:00] <markey> ahoy :)
[22:00] <markey> apachelogger++
[22:00] <markey> good idea, that
[22:01] <shankhs_> apachelogger+=2
[22:01] <micmord> apachelogger: lol... https://launchpad.net/~we-love-harald
[22:01] <shadeslayer> apachelogger+=4 (for giving us neon ;) )
[22:01] <apachelogger> sdein: I'll query you :)
[22:01] <pan_de_> ty for this intro into ruby
[22:01] <apachelogger> #################################################################################################
[22:01] <apachelogger> talk.showcases
[22:01] <apachelogger> * Project Neon (https://code.edge.launchpad.net/project-neon/trunk) simple design using loads of modules and variables :)
[22:01] <apachelogger> * KDE Extragear Release script (https://code.edge.launchpad.net/~apachelogger/+junk/release-script-refactor) simple frontend design using batch code access via gateway point using loads of requires
[22:01] <apachelogger> * Synergy Plasmoid (http://kde-look.org/content/show.php/Synergy?content=106711) Plasmoid example doing fancy stuff with classes, Qt Designer generated .ui files and applying the same base interfaces for different uses.
[22:01] <Monika|K> omg, schülervz on launchpad ;)
[22:01] <shadeslayer> yeah
[22:01] <apachelogger> * Kubuntu-Dev-Tools (https://code.edge.launchpad.net/kubuntu-dev-tools/trunk) mostly Ninja scripts for release packaging. The most useful Ruby tools ever :)
[22:01] <khaije|amalt> apachelogger: iow is local attached to the most restrictive scope or to a module, and is global available to linking code
[22:01] <javi> thank you!
[22:02] <apachelogger> #################################################################################################
[22:02] <apachelogger> talk.kill
[22:02] <apachelogger> talk.freaing_die_alredy unless talk.dead?

Kubuntu/Archives/KubuntuTutorialsDay/Ruby (last edited 2013-04-07 14:35:07 by HSI-KBW-078-043-071-031)