----------------------------------- TokenHerbz Sat Dec 04, 2010 6:51 am Ruby Questions. ----------------------------------- I'm not fully understanding why i can't compact my block of code thats spread out, into a line... [code] 1.upto(10) { |x| print "#{x} " if x == 1 or x == 3 or x == 6 puts end } [/code] I try to put this into one line, [code] 1.upto(10) {|x| print x} #So far so good.... prints it out, but i want that puts to make my numbers ordered better! [/code] [code] 1.upto(10) {|x| print "#{x} " if x == 2 puts end} #doesn't matter what i try, with/without end, commas, outsdie the block, it just wont work... [/code] any ideas to why? I don't think i understand ruby's "block" formats yet, explaining it to me would be nice. Also some other quick questions. [code] name = gets #this gets "Name\n" name = gets.chomp #this gets "Name" name = gets.strip #What does STRIP do? [/code] I read about ARGF.each, It seems to always be stuck in a loop, and if i try to use gets instead its pretty problematic, I managed to get it to work but it only does it once, how do i get it do it loops only a certain amount of times that i want? And i keep failing at DO. i cant ever get it to work on my own even tho i read about it, can someone explain how this works? I need something to use for my loops, figured do would be best. ------- Also I would love a few simple ideas for me to tackle that strenghtens me ruby skills and knowledge. I picked this up not to long ago, and its really a language i'm going to get into. Tired of turings lack of everything, So this will be my new, dedicated language to learn. Thanks for help/tips that come. /cheers. ----------------------------------- DtY Sat Dec 04, 2010 8:35 am RE:Ruby Questions. ----------------------------------- The reason you can't condense that into a single line is that if...end are not blocks, they must have line breaks (I'm pretty sure). There is the alternate inline mode of if, rather than doing Re: blocks In Ruby, functions are not first class data, so you cannot pass them as arguments to functions or return them from functions. This makes doing a lot of loopy stuff really inconvenient because only the internal control stuff would be possible (if, for, while, loop), whereas other languages have functions that can call other functions over and over and do stuff with it (functions like map, filter, reduce), which makes programming a lot easier. Rather than making functions first class data, Ruby decided to go for another type that it kinda like functions: procs (or procedures). Procedures are like functions, except that you need to call proc#call to execute them, because they're objects. Ruby blocks are just a special syntax for creating a proc and then passing it directly to a function. This will be familiar if you've done any functional programming. Re: String#split gets is a function, so when you call gets.chomp, it will call gets, and then call chomp on the string it returns. This is important to keep in mind when you are looking for information about a method, chomp is not gets' method, it is string's. Split splits a string into an array at a given identifier. [url=http://ruby-doc.org/core/classes/String.html#M000803]More information on String#split I don't have time to answer your other questions right now, but I will hopefully later today, if no one does before me. I probably did a pretty bad job explaining blocks, so if someone else can elaborate on it, that'd be great. ----------------------------------- jcollins1991 Sat Dec 04, 2010 9:17 am Re: Ruby Questions. ----------------------------------- You can do a one line if-else, you just need to use THEN (or use the "blah if cond" thing). a = 1 if a then puts 'good' else puts 'bad' end If by a do loop you mean do-while, I don't believe Ruby has that. If you want to do something a certain number of times just do n = 10 n.times do |a| puts a end Or if you want to do the more familiar loops like while and for check out http://www.tutorialspoint.com/ruby/ruby_loops.htm If you want to get a good understanding of Ruby I'd suggest reading http://mislav.uniqpath.com/poignant-guide/, it gives a really nice overview and has random yet awesome comics. ----------------------------------- wtd Sat Dec 04, 2010 11:13 am Re: Ruby Questions. ----------------------------------- You can do a one line if-else, you just need to use THEN (or use the "blah if cond" thing). a = 1 if a then puts 'good' else puts 'bad' end If by a do loop you mean do-while, I don't believe Ruby has that. Why not? Ruby has this: [code]something while some_condition[/code] So naturally, we can replace something with some block of code.: [code]begin .... end while some_condition[/code] ----------------------------------- wtd Sat Dec 04, 2010 11:16 am Re: Ruby Questions. ----------------------------------- I'm not fully understanding why i can't compact my block of code thats spread out, into a line... The semi-colon ninja strikes again! [code]1.upto(10) { |x| print "#{x} "; if x == 1 or x == 3 or x == 6 then puts end }[/code] ----------------------------------- wtd Sat Dec 04, 2010 11:20 am RE:Ruby Questions. ----------------------------------- Also... [code]puts (1..10).collect { |x| case x when 1,3,6 then "#{x}\n" else "#{x} " end }.join[/code] ----------------------------------- TokenHerbz Sat Dec 04, 2010 12:46 pm RE:Ruby Questions. ----------------------------------- could you explain collect and join. i know case is a simplar if/elseif/else thing. ----------------------------------- wtd Sat Dec 04, 2010 12:51 pm RE:Ruby Questions. ----------------------------------- [code]irb(main):248:0> (1..10).collect { |x| case x when 1,3,6 then "#{x}\n" else "#{x} " end } => ["#x\n", "2 ", "#x\n", "4 ", "5 ", "#x\n", "7 ", "8 ", "9 ", "10 "][/code] ----------------------------------- jcollins1991 Sat Dec 04, 2010 12:52 pm Re: RE:Ruby Questions. ----------------------------------- could you explain collect and join. i know case is a simplar if/elseif/else thing. Collect goes through each element of an array and returns an item in the same spot with whatever you return in the block. In your case, it'd return an array of elements that are either "#{x}\n" or "#{x}" (with proper numbers in of course). Then join is just a simple function that takes an array and joins the elements into a string. ----------------------------------- TokenHerbz Sat Dec 04, 2010 12:55 pm RE:Ruby Questions. ----------------------------------- awesome ----------------------------------- Insectoid Sat Dec 04, 2010 12:59 pm RE:Ruby Questions. ----------------------------------- collect runs the block and returns an array of the output. ie ["a", "b", "c"].collect {|x| x+"!"} returns ["a!", "b!", "c!"] join converts an array to a string. So, what wtd's code is doing is creating an array of #{x}'s and #{x}\n's, then converting it to a string, then printing it. EDIT: Damn. [url=http://www.ruby-doc.org/]For future reference. ----------------------------------- TokenHerbz Sun Dec 05, 2010 9:28 am RE:Ruby Questions. ----------------------------------- Continuation: In Turing your able to get a users input and change the output as the user types, example would be collecting the string "TESTERS" and having the program output a "*******" per char the user types. the gets in ruby seems pretty limiting to that aspect since i always see what im typing, unless there is some hidden ".replace" thing i cant find on the internet. Thanks! I'm trying to get a "password" program creator thinggy going, and its problematic to see the password being typed in this case... ----------------------------------- Insectoid Sun Dec 05, 2010 9:43 am RE:Ruby Questions. ----------------------------------- You might try [url=http://raa.ruby-lang.org/project/ruby-password/]googling [url=http://stackoverflow.com/questions/133719/how-to-read-a-password-in-ruby]around a bit. ----------------------------------- TokenHerbz Sun Dec 05, 2010 9:57 am RE:Ruby Questions. ----------------------------------- I'd prefer the answer without a library or gem to add and use. I want to know how to do it raw. thx for links tho. Edit to Add: Iv'e been googleing hard! ----------------------------------- jcollins1991 Sun Dec 05, 2010 10:29 am Re: Ruby Questions. ----------------------------------- Well if you're just using the command line I'm not sure if there's anything you can do. I'm sure Turing must be using their own proprietary stuff for user input so you can do stuff like that, with Ruby you're using whatever your operating system offers (command prompt, x11, etc) so you don't have as much freedom. Also, try actually reading through the libraries Insectoid offered you, they have perfectly good (and easily readable) source code you can read if they happen to have the features you're looking for. ----------------------------------- Tony Sun Dec 05, 2010 6:11 pm 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. ----------------------------------- TokenHerbz Mon Dec 06, 2010 4:49 am 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 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 Mon Dec 06, 2010 11:32 am Re: Ruby Questions. ----------------------------------- Assuming x < y: rand(y - x + 1) + x + 1 so you get x -> y endpoints inclusive ----------------------------------- Insectoid Mon Dec 06, 2010 12:02 pm RE:Ruby Questions. ----------------------------------- Alternatively (and this can be used in almost any language) you can use [code] rand(2)*(a-b)+b[/code] 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 Mon Dec 06, 2010 12:53 pm Re: Ruby Questions. ----------------------------------- This is making my eyes bleed. What's wrong with the following? puts "Enter a string of letters/numbers and i'll make magic!" my_string = gets.chomp my_string.gsub! / ----------------------------------- TokenHerbz Mon Dec 06, 2010 1:22 pm RE:Ruby Questions. ----------------------------------- ooohh awesome, yeah i guess that '!' rewrites it eh. i gotta remember that. why do you use .gsub over .sub dont they do the same thing? ----------------------------------- jcollins1991 Mon Dec 06, 2010 1:31 pm Re: RE:Ruby Questions. ----------------------------------- ooohh awesome, yeah i guess that '!' rewrites it eh. i gotta remember that. why do you use .gsub over .sub dont they do the same thing? Have you bothered googling anything before posting questions here? First response from google: 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 Mon Dec 06, 2010 1:49 pm 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 Mon Dec 06, 2010 4:01 pm RE:Ruby Questions. ----------------------------------- And you can also concat it all together puts "Enter a string of letters/numbers and i'll make magic!" puts "Magic Complete: #{gets.chomp.gsub(/ Note that since we are not persisting the result, we're using #gsub instead of #gsub! ----------------------------------- TokenHerbz Thu Dec 09, 2010 12:26 pm 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... 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 ----------------------------------- wtd Thu Dec 09, 2010 1:29 pm 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. [code]class Array2D # ... def [](x, y) # ... end end[/code]