Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started 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:
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 ;)
-
To all those non-Ruby-programmers:
go to www.tryruby.org (http://www.tryruby.org). It'll help you understand the power of Ruby ;)
-
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
-
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.
-
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
-
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?
-
How do things like this work books.values.each { |rate| ratings[rate] += 1 }. what is the syntax for them?
-
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 ;)
-
I prefer to write little modules for most thins I do a lot in my different programs
-
Guess what this does ;)
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
-
I hope this is not a quadratic solver :P
-
I hope this is not a quadratic solver :P
O.o seriously, how did you know by looking at it that fast?
-
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
-
:thumbsup: nice catch! XD
-
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?
-
well, it's more of me just practicing the use of lambda expressions :P
-
The only place that I heard about ruby was in RPG Maker XP
-
The only place that I heard about ruby was in RPG Maker XP
how is it used there? Just curious.
-
I think the whole scripting is in Ruby, so you can change the configuration and add mods.
-
makes sense -- it's a simple language to learn, but extremely hard to master :)
-
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. :(
-
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
-
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
-
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:
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:
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:
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:
Peanut = Exponent(2,5)
which is actually no problem. But calling to lambdas is a better Ruby approach. the exact same working code:
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:
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
-
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
-
Cool, hopefully this is useful to some people here.
-
Ruby? Doesnt make sense to me, sorry.
-
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.
-
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.
-
I'll try it in a real Ruby interpreter.
-
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.
-
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
-
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.
-
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
-
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.
-
I just started programming with Ruby, and I must say I'm loving it.
-
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