Posted: Sun Dec 05, 2010 6:11 pm Post subject: RE:Ruby Questions.
Turing's "no echo" is build in; Ruby could run in many different terminals, so it's a bit more work to set up the right config options. The libraries do that, and you should take a look at their source to see what exactly they do.
Posted: Mon Dec 06, 2010 4:49 am Post subject: Re: Ruby Questions.
Continuation of Questions: AGAIN! (cause i don't wanna spam making new posts)
I have a peice of code that gets a string from user, and then it changes the strings vowels to "*" and numbers to "#".
however, in order for it to work, this has to be changed into an array, and i have to check each element of the array, and change it one by one.
In my example, here ill post to show you.
code:
def getName
puts "Please enter your name: " #for ex put "Derek523" for both voul/numbers
result = gets.chomp #users input
end
name = getName
def removeVowels (name_) #gets name
if name_ =~ /a|e|i|o|u/ #if name (all of it) has a voul
name_ = name_.sub(/a|e|i|o|u/, "*") #we replace that voul
end
return name_ #only replaces ONE voul tho,does it have to be array to check?
end
name = removeVowels(name) #only changes one vowel?
puts name #test and see
def removeNumbers (name_) #test 2, we get the name
x = 0 #counter set to zero, don't know how to loop in ruby without it
name_ = name_.split(//) #name_ turn into an array
while x <= name_.length #this loops threw our array
if name_[x] =~ /1|2|3|4|5|6|7|8|9|0/ #check for numbers
name_[x] = name_[x].sub(/1|2|3|4|5|6|7|8|9|0/, "#") #replace our numbers
end
x += 1 #move threw the array
end
return name_.join #returns to us our modified array .join to make it string
end
name = removeNumbers(name) #the test works
puts name
My questions is: Does the =~ check the whole string for a match, or does it just skip down when it finds the first match. and also, Why doesn't the .sub (vowels, "*") work in a string. I would think it should look at the whole string and sub everything in that string but it seems to only do the first one it comes across, and im forced to change everything by makeing my string an array and doing it like the "numbers" in my example.
A few more questions to my example. I know theres gotta be a better way then to use my loop in there, but could you recommend me one to use, or maybe a read on it. All the reads i come across isn't clear at explaining how things work, Such as do.
furthermore, i try to create a variable out of existance such as |x| to use as a counter, but it never works, does such a variable need to be set with a value? |x=0| doesn't work, nor makes sense to have that in a code resetting the counter so im at a loss with such things, where im also unable to find a good read on it. I see examples of codes without explinations on how and why it works, And would love someone like wtd or other knowledgable person to explain. thanks. //ALSO, does the YEILD work just like return?
EDIT TO ADD: I was testing around with sub and i came across something i dont get. Using the .sub inside an if works but it doesn't work outside the if. why? I would think the if only directs a path to the .sub, but the path isn't really needed since if the .sub isn't meeting a requirement to change a letter, then it wouldn't change anything and move on, or am i wrong? the example is below, it wont work without the if statement, and i would think it should since if letter doesn't = the sub requirments its just ignored.
code:
puts ("Enter a string of letters/numbers and i'll make magic!")
myString = gets.chomp.split(//) #array of chars
x = 0
while x <= myString.length #0 up to upper array length
if myString[x] =~ (/1|2|3|4|5|6|7|8|9|0|a|e|i|o|u|/) #does ruby really need this to sub???
myString[x] = myString[x].sub(/a|e|i|o|u/, "*") #cant call SUB without the if =~? statement
myString[x] = myString[x].sub(/0|1|2|3|4|5|6|7|8|9/, "#") #will error, but why???
end
x += 1
end
myString.join
puts "Magic Complete: #{myString}"
Insectoid
Posted: Mon Dec 06, 2010 9:04 am Post subject: RE:Ruby Questions.
You could use an each loop to go over the string and add that letter (modified or no) to an array.
string.each do {|i| #stuff }
|x| is used in blocks to pass a parameter in. If you run an each loop, it pass each element of the array/list/string/etc. into the block. Similarly,
n.times do {|n| } will act as a for loop, passing incrementing values up to n into the block. If you want a regular for loop similar to Java or C, Ruby supports that syntax as well (though it's very rarely used).
TokenHerbz
Posted: Mon Dec 06, 2010 10:55 am Post subject: RE:Ruby Questions.
Thanks, FYI (for wtd or someone) i have questions on above posts still to be answered...
And some new ones here.... WITH FREAKING RAND!
so i really try not to compare turing with ruby but i think im fixated on turing "Rand.Int(x,y)" crap... cause....
ruby "num = rand(x)" works good right, But I want a number between x and y.. and that for some reason, seems really problematic....
code:
num = [rand(10) .. rand(15)]
#get 2 numbers, 1 range 0-9 // 2 range 0-14
num = {rand(10), rand(15)}
#get 1 number range 02 -- 913 on tests -WTF?
num = (rand(5),rand(10)) #ERRORS
num = (rand(5) .. rand(10))
#puts 2 numbers x .. y range within numbers
# so i failed at making a random number from #value X to value Y sigh
As you can see, the results i expected to see are non existant... I can maybe get where ruby's coming from with the x .. y using 2 values, but i havn't a clue how it outputs a freaking 913 on the first code you see there..
Anyways i've been unsuccessful at obchieveing my results even tho i can read from this what seems to be simple.
***the READ:***
rand rand( max=0 ) -> aNumber
Converts max to an integer using max1 = max.to_i.abs. If the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1. Kernel::srand may be used to ensure repeatable sequences of random numbers between different runs of the program.
jcollins1991
Posted: Mon Dec 06, 2010 11:32 am Post subject: Re: Ruby Questions.
Assuming x < y:
Ruby:
rand(y - x + 1) + x
+ 1 so you get x -> y endpoints inclusive
Insectoid
Posted: Mon Dec 06, 2010 12:02 pm Post subject: RE:Ruby Questions.
Alternatively (and this can be used in almost any language) you can use
code:
rand(2)*(a-b)+b
This does not rely on knowing that a < b.
Since rand(2) returns 0 or 1, you can apply math to that function to return one of any 2 numbers.
ie, to return either 5 or 8,
0*(5-8)=0, 0+8 = 8
1*(5-8)=-3, -3+8 = 5
Often basic math is better than complicated algorithms. It just requires a different way of thinking.
wtd
Posted: Mon Dec 06, 2010 12:53 pm Post subject: Re: Ruby Questions.
TokenHerbz @ Mon Dec 06, 2010 5:49 pm wrote:
code:
puts ("Enter a string of letters/numbers and i'll make magic!")
myString = gets.chomp.split(//) #array of chars
x = 0
while x <= myString.length #0 up to upper array length
if myString[x] =~ (/1|2|3|4|5|6|7|8|9|0|a|e|i|o|u|/) #does ruby really need this to sub???
myString[x] = myString[x].sub(/a|e|i|o|u/, "*") #cant call SUB without the if =~? statement
myString[x] = myString[x].sub(/0|1|2|3|4|5|6|7|8|9/, "#") #will error, but why???
end
x += 1
end
myString.join
puts "Magic Complete: #{myString}"
This is making my eyes bleed.
What's wrong with the following?
Ruby:
puts"Enter a string of letters/numbers and i'll make magic!"
Whereas sub only replaces the first instance, gsub method replaces every instance of the pattern with the replacement. In addition, both sub and gsub have sub! and gsub! counterparts. Remember, methods in Ruby that end in an exclamation point alter the variable in place, instead of returning a modified copy.
Note not all functions have a "!" counterpart.
TokenHerbz
Posted: Mon Dec 06, 2010 1:49 pm Post subject: RE:Ruby Questions.
yes i google that i must of missed. i got lots of info reading threw it but sometimes the definitions i come across arn't very clear and the examples are to say the least, pitiful...
Tony
Posted: Mon Dec 06, 2010 4:01 pm Post subject: RE:Ruby Questions.
And you can also concat it all together
Ruby:
puts"Enter a string of letters/numbers and i'll make magic!" puts"Magic Complete: #{gets.chomp.gsub(/[aeiou]/, '*').gsub(/\d/, '#')}"
Note that since we are not persisting the result, we're using #gsub instead of #gsub!
Posted: Thu Dec 09, 2010 12:26 pm Post subject: Re: Ruby Questions.
ANOTHER QUESTION!: (I figured I would post it here so the forums aren't spammed.
Arrays!: The 2D kind...
So I been playing with the arrays a lot and trying to use it as a 2D array, I don't get it.. To me it makes perfect sense and i can't find anything about even decalaring a 2D array like Turing, so easy right, array 1 .. x 1 .. y and use it as array(x,y) // But hmm, A little help on this? I been googling like crazy to and im starting to think there is no 2D arrays...
Ruby:
ROWS, COLUMNS = 5, 5#constants for the 2D array.
MINE_PERCENTAGE = 0.10#10% for mine spawn
mine_tile = Array.new(ROWS){Array.new(COLUMNS)}#2D array #array name = array.new 5 values of (new array with 5 values) 1 array space should have 5 values?
mine_amount = (MINE_PERCENTAGE * (ROWS * COLUMNS)).round#amount of mines to be used
#init the array with '*' FOR MINES or '.' FOR EMPTY SPACE 0.upto(ROWS){|rows| 0.upto(COLUMNS){|columns| mine_tile[rows][columns] = "."}} #we want to add the correct amount of mines randomly into our 2D array. 0.upto(mine_amount){|mines| mine_tile[rand(ROWS)][rand(COLUMNS)] = "*"} #Code doesn't RUN? array should have values and everything looks right...
wtd
Posted: Thu Dec 09, 2010 1:29 pm Post subject: RE:Ruby Questions.
There's no specific 2D array type. You can have an array of arrays, though.
Or, you could create your own class to accomplish the same.