Author Topic: Ruby Discussion and Help  (Read 11277 times)

0 Members and 1 Guest are viewing this topic.

Ashbad

  • Guest
Ruby Discussion and Help
« 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 ;)
« Last Edit: May 13, 2011, 09:52:40 pm by Ashbad »

Ashbad

  • Guest
Re: Ruby Discussion and Help
« Reply #1 on: May 14, 2011, 02:00:26 pm »
To all those non-Ruby-programmers:

go to www.tryruby.org.  It'll help you understand the power of Ruby ;)

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Ruby Discussion and Help
« Reply #2 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
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Ashbad

  • Guest
Re: Ruby Discussion and Help
« Reply #3 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.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Ruby Discussion and Help
« Reply #4 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
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Ashbad

  • Guest
Re: Ruby Discussion and Help
« Reply #5 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?

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Ruby Discussion and Help
« Reply #6 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?
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Ashbad

  • Guest
Re: Ruby Discussion and Help
« Reply #7 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 ;)

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Ruby Discussion and Help
« Reply #8 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
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Ashbad

  • Guest
Re: Ruby Discussion and Help
« Reply #9 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

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Ruby Discussion and Help
« Reply #10 on: May 16, 2011, 05:36:00 pm »
I hope this is not a quadratic solver :P

Ashbad

  • Guest
Re: Ruby Discussion and Help
« Reply #11 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?

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55941
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Ruby Discussion and Help
« Reply #12 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

Ashbad

  • Guest
Re: Ruby Discussion and Help
« Reply #13 on: May 16, 2011, 05:38:03 pm »
:thumbsup: nice catch! XD

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Ruby Discussion and Help
« Reply #14 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?
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y