@p Hey, I really liked that ruby one liner you posted the other day. And, remembered that I've never really looked at ruby. (but I've often heard it talked about)
> And, remembered that I've never really looked at ruby. (but I've often heard it talked about)
Oh, wow. It's basically what I have been using for work since forever, so it feels boring to me sometimes, like not really worth mentioning. But it's really great for mangling arrays and strings, lot of easy support for talking to random databases (SQL or Redis or whichever), very nice REPL, so that makes it useful for interactively poking at data. There's some disdain for it, but it's like Lisp, Smalltalk, and Perl had a baby. Like, the fact that you can pass a block to gsub is already enough to put it on top of most string-manglers:
irb(main):001:0> i=0; "This is a test of the string substitutions.".gsub(/i/) { |m| "[number #{i+=1}]" } => "Th[number 1]s [number 2]s a test of the str[number 3]ng subst[number 4]tut[number 5]ons."
It's what I usually reach for if it looks like awk won't cut it.
> Any thing else you can point me at to see the shine of ruby?
I still like "_why's (Poignant) Guide to Ruby", but when he pushed that out, I already knew Ruby. The way I learned was I half-read and half-skimmed the Pickaxe book (which is now notorious for being a terrible way to learn the language) and basically having done some Perl and some Common Lisp, I was able to more or less guess how things like map worked, and I hung out on the mailing list. Back in The Day, if someone managed to use Ruby at work, they'd send email to the list, and people were on there puzzling things out or making up challenges or doing code golf.
[1,2,3].map{|i|i**2}.inject(&:+)
I think the best way to learn Ruby is the REPL. Like fire up irb and go for it. Matz's goal was to make a language that was fun to use, and it's pretty successful at that.
_why is really fun. Left when he got doxed. Like, DFE-and-nuke left. He explained some in the book he released "posthumously".
> I noticed some of the things, that require leg work to do in JavaScript, are the default in Ruby.
This is one of the things I like a lot about it: I don't want to rewrite some things that are primitive operations in Lisp or Perl or even awk, C, whatever. That also makes it very convenient for interactive use, like I'm looking at a new dataset or a new Postgres DB or something like that, I don't have to bash out a lot of basic stuff to do basic analysis or data exploration.
> Why does python even exist? (though after talking with a friend, the answer seems to be, people prefer a simpler smaller syntax)
Neither was very popular at the time they were created, so they probably weren't aware of each other, ha. Ruby is slightly older but I think it's by months. The languages appeal to different sensibilities, though, and that has made different communities spring up around them, which means different libraries and whatnot, and so some stuff is lower-friction in one language versus the other. Python's not likely
> My primary interest in coding these days, is either a micro controller, or to make something with graphical user interaction.
Ah, you might be interested in mruby, designed to be an embeddable subset of the language. Lua-but-it's-Ruby; I believe it runs on some microcontrollers.
To be honest, if I am doing a GUI with Ruby, I usually just open up a pipe to wish (though I have played with the SDL library a lot). I don't think I have attempted to use ruby2d before.
> So, I am guessing there will be some kind of "make Window current context" command to be covered.
I had a look at https://www.ruby2d.com/learn/get-started/ and it's actually pretty cool, looks like fun. Their sample code runs for me. I don't know where you got your sample code from, but it looks like they create the basic window and then merge that with the top-level object, and it looks like `Window` gives you (per https://www.ruby2d.com/learn/window/ ) a reference to the Window singleton.
You can actually tell it's added things to the toplevel by doing this in irb:
@p I've been enjoying the book. And, I really like some of the design decisions. I noticed some of the things, that require leg work to do in JavaScript, are the default in Ruby. Why does python even exist? (though after talking with a friend, the answer seems to be, people prefer a simpler smaller syntax)
My primary interest in coding these days, is either a micro controller, or to make something with graphical user interaction. So, of course I am doing the silly thing of reading Why's and playing with Ruby2D.
Running their getting started examples feels like, PFM xD RubyMine IDE doesn't seem to know what is going on either. But, the answer seems right under my nose.
Through experimentation, update, set, and show, all seem to be instance methods for Window. So, I am guessing there will be some kind of "make Window current context" command to be covered.
The third version, feels the most like something in another language, make a new instance of Window.
Anyway, just wanted to let you know that I am enjoying Ruby, it is pretty neet.
Oh! Well that is obvious after you say it lol. And, I had to reference Window (which I believe is initialized as a singleton) to "add" an object, because the add method wasn't added to the top-level object.
You know, I kept thinking, "Ruby is just inferring what my code means, and piping my request to the correct object." But, that seems pretty irrational in hindsight xD
I'll take some time to digest the rest of your message :D
@p ahh, you can see I referenced the wrong object, that should have been Object::methods. So I jumped the gun on that message xD I'll get back to you on that...
@hazlin Well, '.method' just returns a lambda, right?
add5 = 5.method(:+) puts add5.call(6) # .call and [] do the same thing in this case. puts add5[7] puts 5.method(:method)[:method][:method][:method] == 5.method(:method) puts 5.class.instance_method(:+).bind(6)[7] # instance_method returns an unbound method, you have to bind it to an actual instance to call it. You can use this to get around shadowing by subclasses.
And 'methods' returns a list of symbols that represent the names of the methods. (It is also very useful.) In fact, this might answer some questions:
builtins = methods.sort require 'ruby2d' additions = methods.sort - builtins puts *additions.map { |mname| m = method(mname) if m.nil? "#{mname} is implemented in C." else "#{mname} ->\t#{m.source_location[0]}:#{m.source_location[1]}" end }
@p Okay, got that straightened out. Object::methods is different than methods. methods holds what is in the current context, and I can find set show and update there, but not add. (mystery solved xD)
You can get a reference to a method regardless of language but since the parser doesn't see the stuff on the other side of the FFI, it can't tell you the source location.
> Ruby doesn't seem to have a keyword to stick all the new stuff into its own name space.
*Usually* it is considered a sign of bad library design for the top-level namespace to get mangled like that. In this case, since it's effectively a graphics library DSL that wraps SDL2, it makes sense.
> So I am going to set that aside for now, and actually get some stuff painted to the screen...
It's actually a fun library, it's on my list, I wanted to play with animations for a generative art thing and it handles a lot of stuff that is fun to implement but is less fun to implement if you want to be working on something else.
> Though I did wonder if the * in your version had special meaning
Argument-splattering. Essentially flattens an array inline, as if the elements of an array had been included individually. It works both ways. Very useful, and some patterns with iterators rely on it. Here are some contrived examples:
@p That makes perfect sense. (screenshot[0] of, what will be my diagnostic code going forward :D. Ruby doesn't seem to have a keyword to stick all the new stuff into its own name space. So I am going to set that aside for now, and actually get some stuff painted to the screen... or procrastinate xD)
Though I did wonder if the * in your version had special meaning (screenshot[1]). I pulled up some language cheatsheets, but didn't see anything about it.