Posted: Sat Dec 04, 2010 6:51 am Post subject: 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
}
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:
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...
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?
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.
Sponsor Sponsor
DtY
Posted: Sat Dec 04, 2010 8:35 am Post subject: 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
code:
if condition
dothis
end
You would do:
code:
dothis if condition
If you really want to condense this down to one line, I would suggest using inline if to conditionally insert a newline character.
code:
1.upto(10){|x| print "#{x} #{"\n" if x==1 or x==3 or x==6}"}
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. 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
Posted: Sat Dec 04, 2010 9:17 am Post subject: Re: Ruby Questions.
You can do a one line if-else, you just need to use THEN (or use the "blah if cond" thing).
Ruby:
a = 1 if a thenputs'good'elseputs'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
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
Posted: Sat Dec 04, 2010 11:13 am Post subject: Re: Ruby Questions.
jcollins1991 @ Sat Dec 04, 2010 10:17 pm wrote:
You can do a one line if-else, you just need to use THEN (or use the "blah if cond" thing).
Ruby:
a = 1 if a thenputs'good'elseputs'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
So naturally, we can replace something with some block of code.:
code:
begin .... end while some_condition
wtd
Posted: Sat Dec 04, 2010 11:16 am Post subject: Re: Ruby Questions.
TokenHerbz @ Sat Dec 04, 2010 7:51 pm wrote:
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
}
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 }
wtd
Posted: Sat Dec 04, 2010 11:20 am Post subject: RE:Ruby Questions.
Also...
code:
puts (1..10).collect { |x| case x when 1,3,6 then "#{x}\n" else "#{x} " end }.join
TokenHerbz
Posted: Sat Dec 04, 2010 12:46 pm Post subject: RE:Ruby Questions.
could you explain collect and join.
i know case is a simplar if/elseif/else thing.
wtd
Posted: Sat Dec 04, 2010 12:51 pm Post subject: 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 "]
Sponsor Sponsor
jcollins1991
Posted: Sat Dec 04, 2010 12:52 pm Post subject: Re: RE:Ruby Questions.
TokenHerbz @ Sat Dec 04, 2010 12:46 pm wrote:
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
Posted: Sat Dec 04, 2010 12:55 pm Post subject: RE:Ruby Questions.
awesome
Insectoid
Posted: Sat Dec 04, 2010 12:59 pm Post subject: RE:Ruby Questions.
collect runs the block and returns an array of the output.
Posted: Sun Dec 05, 2010 9:28 am Post subject: 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
Posted: Sun Dec 05, 2010 9:43 am Post subject: RE:Ruby Questions.
Posted: Sun Dec 05, 2010 9:57 am Post subject: 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
Posted: Sun Dec 05, 2010 10:29 am Post subject: 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.