Ruby TYS
Author
Message
wtd
Posted: Thu Sep 29, 2005 6:51 pm Post subject: Ruby TYS
Let's kick it off with a challenge.
Given a file containing dates in the format:
code: 092203
072105
061704
That is read into an array called 'dates'.
code: ["092203", "072105", "061704"]
Create a new array called 'new_dates' with each date in the format: mm/dd/yy, rather than mmddyy.
Don't use regular expressions or slices.
Sponsor Sponsor
Cervantes
Posted: Fri Sep 30, 2005 9:17 pm Post subject: (No subject)
Ruby:
new_dates = File::open ( "dates.txt" ) .readlines .collect { |d| d[ 0 ..1 ] + "/" +d[ 2 ..3 ] + "/" + d[ 4 ..5 ] }
p new_dates
Rather messy block I've got there.
wtd
Posted: Sat Oct 01, 2005 1:58 pm Post subject: (No subject)
Cervantes wrote:
Ruby:
new_dates = File::open ( "dates.txt" ) .readlines .collect { |d| d[ 0 ..1 ] + "/" +d[ 2 ..3 ] + "/" + d[ 4 ..5 ] }
p new_dates
Rather messy block I've got there.
Is a slice.
wtd
Posted: Fri Oct 28, 2005 5:46 pm Post subject: (No subject)
Write an HTTP server in at most 4 lines of code (no semi-colons) that sits on port 2000 and simply sends "Hello world!" to any clients that connect.
rdrake
Posted: Fri Nov 18, 2005 9:33 pm Post subject: (No subject)
wtd wrote:
Write an HTTP server in at most 4 lines of code (no semi-colons) that sits on port 2000 and simply sends "Hello world!" to any clients that connect.
Could only get it down to 5. code: require 'socket'
socket = TCPServer.new('127.0.0.1', 2000)
newSock = socket.accept
request = newSock.gets
newSock.print "HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\nHello World!\r\n"
Any way I can make it shorter?
wtd
Posted: Fri Nov 18, 2005 10:30 pm Post subject: (No subject)
Well, that kinda works. It only connects to one client, though.
On removing extraneous code... this line doesn't serve any purpose.
code: request = newSock.gets
rdrake
Posted: Sat Nov 19, 2005 11:11 am Post subject: (No subject)
wtd wrote:
Well, that kinda works. It only connects to one client, though.
The following will connect with an unlimited number of clients, but it's over the limit. code: require 'socket'
socket = TCPServer.new('127.0.0.1', 2000)
while newSock = socket.accept
puts newSock.gets
newSock.print "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n<html><head><title>Hello World!</title></head><body><p>Hello world!</p></body></html>\r\n"
newSock.close
end
wtd wrote:
On removing extraneous code... this line doesn't serve any purpose.
code: request = newSock.gets
That line was there so browsers could connect to it too. After playing around with different telnet clients, I've managed to get the code down to this. code: require 'socket'
socket = TCPServer.new('127.0.0.1', 2000)
socket.accept
newSock.print "Hello world!"
newSock.close
Any ideas how I could get this down smaller?
wtd
Posted: Sat Nov 19, 2005 1:38 pm Post subject: (No subject)
Untested because I'm feeling lazy at the moment.
code: require 'socket'
socket = TCPServer.new('127.0.0.1', 2000)
loop { Thread.start(server.accept) { |session| session.print "Hello world\n" } }
Sponsor Sponsor
wtd
Posted: Wed Nov 23, 2005 8:14 pm Post subject: (No subject)
Create two classes "Foo" and "Bar". They should both implement "baz" and "put_baz" methods. The "baz" method should return a string. The "put_baz" method should print that string.
You may use the "def" keyword only three times and the class keyword only twice, and Foo and Bar may not share a common base class, other than Object.
Cervantes
Posted: Wed Nov 23, 2005 9:15 pm Post subject: (No subject)
Ruby:
module Bazzy
def baz
"Bazzy!"
end
def put_baz
puts baz
end
end
class Foo
include Bazzy
end
class Bar
include Bazzy
end
Foo.new .put_baz
Bar.new .put_baz
wtd
Posted: Wed Nov 23, 2005 9:21 pm Post subject: Re: Ruby TYS
wtd wrote:
Let's kick it off with a challenge.
Given a file containing dates in the format:
code: 092203
072105
061704
That is read into an array called 'dates'.
code: ["092203", "072105", "061704"]
Create a new array called 'new_dates' with each date in the format: mm/dd/yy, rather than mmddyy.
Don't use regular expressions or slices.
The answer:
code: new_dates = dates.collect { |date| date.unpack("A2A2A2").join("/") }
wtd
Posted: Tue Nov 29, 2005 5:53 pm Post subject: (No subject)
code: irb(main):006:0> class String
irb(main):007:1>
irb(main):008:2>
irb(main):009:2>
irb(main):010:1> end
=> nil
irb(main):011:0> "hello"
=> *****
Fill in the missing code.
Note that with "hello" we get five stars. If one input "foo" on the next line, the result should be three stars.
wtd
Posted: Thu Dec 01, 2005 7:18 pm Post subject: (No subject)
code: irb(main):001:0> a = Array.new(4, [])
=> [[], [], [], []]
irb(main):002:0> a[0] << 4
=> [4]
irb(main):003:0> a
=> [[4], [4], [4], [4]]
Rewrite the first line of code such that the third line yields:
You may only remove one character, and add two others.
wtd
Posted: Mon Dec 05, 2005 1:23 pm Post subject: (No subject)
wtd wrote:
code: irb(main):006:0> class String
irb(main):007:1> def inspect
irb(main):008:2> '*' * length
irb(main):009:2> end
irb(main):010:1> end
=> nil
irb(main):011:0> "hello"
=> *****
Fill in the missing code.
Note that with "hello" we get five stars. If one input "foo" on the next line, the result should be three stars.
wtd
Posted: Mon Dec 05, 2005 1:24 pm Post subject: (No subject)
wtd wrote:
code: irb(main):001:0> a = Array.new(4, [])
=> [[], [], [], []]
irb(main):002:0> a[0] << 4
=> [4]
irb(main):003:0> a
=> [[4], [4], [4], [4]]
Rewrite the first line of code such that the third line yields:
You may only remove one character, and add two others.
code: irb(main):001:0> a = Array.new(4) { [] }
=> [[], [], [], []]
irb(main):002:0> a[0] << 4
=> [4]
irb(main):003:0> a
=> [[4], [], [], []]
Page 1 of 3 [ 36 Posts ]
Goto page 1 , 2 , 3 Next