Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Ruby TYS
Index -> Programming, Ruby -> Ruby Tutorials
Goto page Previous  1, 2, 3
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Sun May 14, 2006 7:10 am   Post subject: (No subject)

Cervantes wrote:
wtd wrote:
Does Ruby have a case ("switch" in some languages) statement?

No. Trick question: Ruby has a case expression.


Indeed.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sat May 20, 2006 12:27 pm   Post subject: (No subject)

There's quite a few Ruby TYS questions over at Freerange right now. I'll copy the questions only to here.
Cervantes wrote:
Let's start off with a fairly easy one.

code:
irb(main):001:0> true | puts("or")
or
=> true

Explain why "or" is output, despite the fact that the '|' operator should know the whole expression is true after seeing the initial 'true', since true or something is true.


Cervantes wrote:
This next TYS is tough, I think, but it's worth it. It's quite fun. Smile

In one line of code, write a program that computes the factorial of a given number. I say it in this manner because I'm not asking you to write a function. Indeed, there is to be no use of "def" or "while" or "loop" any other looping structure.

I'm being kind of vague about this because otherwise I'll give too much away. Feel free to post answers; if it's not what I'm looking for, I'll let you know.

Addendum: I want a recursive solution. But keep in mind, you can't define any methods!


Cervantes wrote:
In one line (no semi-colon cheating, of course), write a program that loops infinitely. It should not crash due to a stack overflow (ie. we're not using recursion, here). You may not use any keywords.


For discussion and answers, please visit the Freerange Ruby TYS thread.
zylum




PostPosted: Thu Jun 01, 2006 11:07 am   Post subject: (No subject)

Ruby:
puts (1..ARV[0].to_i).inject{|p,n|p*n}
Cervantes




PostPosted: Thu Jun 01, 2006 3:28 pm   Post subject: (No subject)

zylum wrote:
Ruby:
puts (1..ARV[0].to_i).inject{|p,n|p*n}


Since when has zylum been doing Ruby? Smile

That's essentially the solution wtd posted on the Freerange forums, which prompted me to add that addendum: I want a recursive solution.

In any case, the answers to all these are found in the Freerange TYS thread.

Here's two more:
Cervantes wrote:

Quit a standard IRB session. You cannot use any irb methods such as (exit, quit, irb_exit, irb_quit...). Things like Ctrl+D and Ctrl+Z do not count. IRB should not exit because of any error. The code you write into IRB that makes it exit must be valid Ruby code.

Cervantes wrote:
Write a short amount of code that makes the following possible:
code:
p Rubidium.new

output wrote:
#<Rubidium:0xb7cf9ed0>

(Of course, the 0xb7cf9ed0 will change.)
The code should apply generally; that is, we're not writing an empty Rubidium class, here.
zylum




PostPosted: Thu Jun 01, 2006 6:13 pm   Post subject: (No subject)

Cervantes wrote:
Since when has zylum been doing Ruby? Smile


since two days ago Smile
wtd




PostPosted: Thu Jun 01, 2006 7:09 pm   Post subject: (No subject)

zylum wrote:
Cervantes wrote:
Since when has zylum been doing Ruby? Smile


since two days ago Smile


<Mr. Burns> Excellent... </Mr. Burns>
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 3 of 3  [ 36 Posts ]
Goto page Previous  1, 2, 3
Jump to:   


Style:  
Search: