Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Ashbad on May 13, 2011, 09:47:39 pm

Title: Ruby Discussion and Help
Post by: Ashbad on May 13, 2011, 09:47:39 pm
Hello

As you all know, I'm a true red ruby coder now, so I decided to set up a thread for us Rubiers.  Anyways, I'll start by asking a quick question.

Here is my code showing off my understanding of how lambda functions work, and I was wondering -- at the point of the while loop between the while and the end (of the nested lambda) , would the fixing of the string be returned simply?  or would it just keep on looping, or both (set the return item to be the string, but keep going)?  Here is the code, which is a Ruby version of a Cemetech-style 'L O L' to '0x5' converter:

Code: (Ruby) [Select]
StringFunction = lambda {|stringInput, pat, rep| #outwards lambda
  replaceAll = lambda {|inputString, pattern, replacement| #nested
    while inputString.sub!(pattern, replacement)
      inputString = inputString.sub(pattern, replacement)
    end
  }
  replaceAll.call(stringInput, pat, rep)
}

String NonLolzed = StringFunction.call(gets.chomp, " lol ", " 0x5 ")
#finally use all those lambdas :roll:
puts NonLulzed

It should work if it doesn't immediately return after the setting on line 4.

EDIT: added a small 'puts xxxx' for quick debugging ;)
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 14, 2011, 02:00:26 pm
To all those non-Ruby-programmers:

go to www.tryruby.org (http://www.tryruby.org).  It'll help you understand the power of Ruby ;)
Title: Re: Ruby Discussion and Help
Post by: ruler501 on May 14, 2011, 02:08:45 pm
I'm running through this and so far it seems to be much the same thing as python with slightly different sntax on the builtin functions
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 14, 2011, 03:17:31 pm
I'm running through this and so far it seems to be much the same thing as python with slightly different sntax on the builtin functions

well, of course the Basics are going to be the same :P higher level stuff is what makes Ruby shine.  However, this shows you the parts that make Ruby slightly superior on low levels.
Title: Re: Ruby Discussion and Help
Post by: ruler501 on May 14, 2011, 09:09:59 pm
I most of the way thorugh the turorial and so far I've only seen a few major differences. IMO python seems easier to use

This looks like an interesting twist on the same idea though. so I'll finish up these tutorials
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 14, 2011, 09:16:20 pm
Well, that '15 minute tutorial' (really more like half an hour) teaches you just the bare bones basics of what Ruby has to offer.  Ruby is known to be much more difficult than python -- but you can write 3 times as compressed code, and it's much more object oriented as well :)  I personally have tried python and I don't feel the same power behind it -- but these are all just opinions.

Any questions about ruby that you have so far?
Title: Re: Ruby Discussion and Help
Post by: ruler501 on May 14, 2011, 09:34:33 pm
How do things like this work books.values.each { |rate| ratings[rate] += 1 }. what is the syntax for them?
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 14, 2011, 09:42:07 pm
Code: [Select]
books.values.times {|rate| ++ratings[rate]}
exactly the same ;) that shows there are some things that can be identical between the two -- but Ruby is known to have more control over blocks and lambda expressions, which is where it shines along with its other powerful functional aspects ;)
Title: Re: Ruby Discussion and Help
Post by: ruler501 on May 14, 2011, 09:47:54 pm
I prefer to write little modules for most thins I do a lot in my different programs
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 16, 2011, 05:33:57 pm
Guess what this does ;)

Code: [Select]
Qvalue = lambda {|a, b, c|
  Qlambda = lambda{|a, b, c, plus|
    if plus; -b + Math.sqrt(b**2-(4*a*c)).div(2*a)
    else;-b - Math.sqrt(b**2-(4*a*c)).div(2*a);end
  }
  if a == 0;nil;else
    [Qlambda.call(a,b,c,true),Qlambda.call(a,b,c,nil)];end }

Args = Array.new(3);Names = ['A','B','C']

3.times do |location|
  puts "Input: " << Names[location] << "?"
  Args[location] = gets.chomp;end
Ans = Qvalue.call(Args[1].to_f.to_r,Args[2].to_f.to_r,Args[3].to_f.to_r)

if Ans;puts 'Answer is ' << Ans[0].to_s << ' and ' << Ans[1].to_s << ' '
else;puts 'No Real Answers';end
Title: Re: Ruby Discussion and Help
Post by: DJ Omnimaga on May 16, 2011, 05:36:00 pm
I hope this is not a quadratic solver :P
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 16, 2011, 05:36:23 pm
I hope this is not a quadratic solver :P

O.o seriously, how did you know by looking at it that fast?
Title: Re: Ruby Discussion and Help
Post by: DJ Omnimaga on May 16, 2011, 05:37:44 pm
I spotted the word Math, sqrt and the letters a b and c, and I figured it might be a math program, and since there are often Quadratic solver jokes in the TI community, I guessed it was one. :P
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 16, 2011, 05:38:03 pm
:thumbsup: nice catch! XD
Title: Re: Ruby Discussion and Help
Post by: ruler501 on May 16, 2011, 05:59:17 pm
I just looked at the first two lines of code and it is a quadratic solver :O
why do you need that much code for a quadratic solver?
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 16, 2011, 06:19:50 pm
well, it's more of me just practicing the use of lambda expressions :P
Title: Re: Ruby Discussion and Help
Post by: Yeong on May 16, 2011, 06:37:30 pm
The only place that I heard about ruby was in RPG Maker XP
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 16, 2011, 06:40:54 pm
The only place that I heard about ruby was in RPG Maker XP

how is it used there?  Just curious.
Title: Re: Ruby Discussion and Help
Post by: Yeong on May 16, 2011, 06:53:17 pm
I think the whole scripting is in Ruby, so you can change the configuration and add mods.
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 16, 2011, 06:54:08 pm
makes sense -- it's a simple language to learn, but extremely hard to master :)
Title: Re: Ruby Discussion and Help
Post by: DJ Omnimaga on May 17, 2011, 07:03:07 pm
Ruby is used in RPG Maker XP and VX. To be honest, though, RM is supposed to be a tool for game creators who don't want to code any single line, so XP purpose was kinda defeated, but by using Ruby you had so much more freedom apparently, although many RPG Maker users had troubles learning it from what I remember.

Also in some versions of XP/VX, the Ruby editor built-in the software used japanese characters. :(
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 17, 2011, 07:37:16 pm
I wouldn't be surprised it was hard for them to learn -- it's not a good language to start out with, it took me months to finally declare myself a Ruby Knight, since it's so different from things like C++, Java, BASIC, and the like -- but once you get your foot in the door, the Principle of Least Astonishment (Meaning that if you know the language well, you can decipher and write straightforward code with little trouble) kicks in.

I'm even less surprised they have Japanese characters in a Ruby scripting system -- if it came out before 1999, then it was during the era when 95% of Ruby coding took place in Japan :)

Though, it's cool to see that XP and VX used it :D
Title: Re: Ruby Discussion and Help
Post by: DJ Omnimaga on May 17, 2011, 07:51:26 pm
Plus, most RPG Maker users are used to just clicking stuff to insert pre-made events in a pre-made engine. They were not used to code. X.x
Title: Re: Ruby Discussion and Help
Post by: Ashbad on May 17, 2011, 08:06:28 pm
very true.  That's why it's called a 'Maker' ;D though then again, some like Game Maker have somewhat-decent coding abilities.

also, good question from cemetech:

Quote from: christop
Maybe I'm misunderstanding something here (since I don't use Ruby), but couldn't you do exactly the same thing with regular (non-lambda) functions? Or are lambda functions somehow different than regular functions in Ruby? I thought one of the points of lambda functions was the ability to pass a (lambda) function to a second function as an argument, wherein the second function calls the lambda function inside of it. I don't see any of that going on in your code, which would make using lambda functions pointless.

response:

Quote from: Ashbad
I did it purely for practice of point ^-^ since they can be easily misused, I'm practicing to make sure I have them down right.

Yeah, it's a bit more complex, but they're more useful for higher-level math concepts, and even better for managing code snippets.

EDIT: I only answered part of your question methinks  :o so I'll go over how lambda functions can be invaluable:

let's say you have this working piece of code:

Code: [Select]
def Exponent(number,root)
   return number**root.to_f;end

if you want to load a variable such as 'Peanut' with an expression like "2 ** 5" you would constantly have to do:

Code: [Select]
Peanut = Exponent(2,5)
which is actually no problem.  But calling to lambdas is a better Ruby approach.  the exact same working code:

Code: [Select]
Peanut = lambda{|number,root| number**root}

Peanut.call(2,5)

you see, with this method, Peanut is directly attached to the function -- lambda functions are considered actual values, not functions themselves.  This allows for a higher level of abstraction in code, and nesting lambdas can be extremely useful when solving for complex equations -- it's easier to read and keep track of.  Such as in my Quadratic solver (which looks difficult but really is just a simple example of what I'm talking about) -- the lambda function isn't really a function -- it's a value that is determined by calling 'arguments' (I use that loosely, I forgot the true word for it)

This is one way to achieve 'anonymous functions' in Ruby, but actually isn't the most powerful way.  Another way that provides even less hassle is by declaring a Proc Function:

Code: [Select]
def ProcExample(input, NUMBAR)
  return lambda {|input| input.chomp.flatten.reverse},
                            lambda {||NUMBAR.to_s.reverse};end

uno, dos = ProcExample

The only real difference between a normal lambda statement and a proc statement is actually usage -- lambdas are when you simply just bind a function to a value.  Procs are when you use them on the fly to return a value based on 'arguments' (again, loosely used) like lambdas, but the difference is that they are usually more block-like and aren't directly bound to an actual variable -- very often seen as arguments of other lambdas or procs or even normal functions, or as returned values (with a lambda, you would return the value by using a call statement, procs would simply be declared at the moment).

I hope that clears a few things up :P
Title: Re: Ruby Discussion and Help
Post by: Ashbad on June 15, 2011, 10:26:41 pm
For those interested, this is an awesome compilation of tricks used by advanced Ruby programmers, half I knew, the other half I didn't.  Its very useful: http://www.rubyinside.com/21-ruby-tricks-902.html
Title: Re: Ruby Discussion and Help
Post by: DJ Omnimaga on June 16, 2011, 12:49:53 am
Cool, hopefully this is useful to some people here.
Title: Re: Ruby Discussion and Help
Post by: aeTIos on June 16, 2011, 08:29:26 am
Ruby? Doesnt make sense to me, sorry.
Title: Re: Ruby Discussion and Help
Post by: Spyro543 on July 02, 2011, 12:32:40 pm
Oh, tryruby.org is FAULTY. I tried every single example EXACTLY the same way it was shown in the tutorials, and EVERY TIME I got this:

SyntaxError: <main>:69: syntax error, unexpected $undefined

It showed to enter 4 * 10, and I did. EVERY EXAMPLE shown threw that error when I tried using them in the tryruby built in prompt.

Oh and slightly off topic: EnvisionDev now includes Ruby support.
Title: Re: Ruby Discussion and Help
Post by: Ashbad on July 02, 2011, 12:36:48 pm
hmm, interesting, I just tried again and it worked.  Perhaps for some reason your browser wasn't compatible with the console?

From what the error was it sounds like it ran into a weird symbol somewhere, which means that it most likely wasn't using ANY of the standard resources.  It should be fine in an actual Ruby script, though.
Title: Re: Ruby Discussion and Help
Post by: Spyro543 on July 02, 2011, 12:38:29 pm
I'll try it in a real Ruby interpreter.
Title: Re: Ruby Discussion and Help
Post by: Ashbad on July 02, 2011, 12:42:34 pm
I would suggest using either YARV (http://www.atdot.net/yarv/#i-3-1) for speed's concerns.  However, it doesn't have full support for some 1.9.2 features so I would suggest either starting with The standard VM (http://www.ruby-lang.org/en/downloads/).  I don't suggest JRuby.
Title: Re: Ruby Discussion and Help
Post by: Scipi on July 02, 2011, 12:47:36 pm
A couple years ago I found Ruby, but then I switched to C++. :S

Now I want to get back into Ruby! I just need to get a development environment set up again. :P
Title: Re: Ruby Discussion and Help
Post by: Ashbad on July 02, 2011, 12:53:50 pm
If you have some money, I would suggest using RubyMine 3.0, which is a totally souped up Ruby-Exclusive IDE that is no less than a dream environment for a Ruby programmer.  Otherwise, use NetBeans 6.9.2 with Ruby support built in or 7.0 and download the Ruby plugin.
Title: Re: Ruby Discussion and Help
Post by: Scipi on July 02, 2011, 01:24:31 pm
I'm going to try out RubyMine. I just installed Ruby and I can't wait to get back into Rubygame development now that I have some C++ experience with SFML. XD

I love the gem feature. It makes installing libraries easy for once. O.O
Title: Re: Ruby Discussion and Help
Post by: Ashbad on July 03, 2011, 12:12:09 am
Indeed, gems are indeed extremely awesome :)

With RubyMine, you'll have to eventually buy it, but you can get a free demo for 30 days.  I personally use Netbeans right now due to my Mine license being in limbo ATM.
Title: Re: Ruby Discussion and Help
Post by: FinaleTI on July 06, 2011, 09:18:27 pm
I just started programming with Ruby, and I must say I'm loving it.
Title: Re: Ruby Discussion and Help
Post by: Ashbad on July 07, 2011, 07:45:40 pm
Sounds awesome! :D yeah, it I a fun language.  Thing is, unlike C/C++, there's always new principals to learn.  With C/C++ there aren't as many prinicipals and you'll get more into optimizing and little tricks than you will be learning a whole new part of the language.  With that being said, I would be happy to provide any help or comments on code here :) and, I have some real cool code condensing tricks I found that I'm dying to share :P