[#407177] Downloading a CSV using URI - mechanize — Rochit Sen <lists@...>

Hi All,

13 messages 2013/05/06

[#407178] %tempdir% location and folder clean up? — "Abinash Y." <lists@...>

Hi Guys,

12 messages 2013/05/06

[#407187] Gem Help - Newbie Question — Michael Tepfer <lists@...>

Hi,

17 messages 2013/05/07

[#407234] Problem with FileUtils move command. — Peter Bailey <lists@...>

Hello,

25 messages 2013/05/08
[#407235] Re: Problem with FileUtils move command. — Joel Pearson <lists@...> 2013/05/08

"no implicit conversion of nil into String" means you're probably

[#407236] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Joel Pearson wrote in post #1108209:

[#407238] Re: Problem with FileUtils move command. — Chris Hulan <chris.hulan@...> 2013/05/08

how is the script invoked?

[#407239] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Chris Hulan wrote in post #1108213:

[#407242] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

can you post some of the script?

[#407246] Re: Problem with FileUtils move command. — Love U Ruby <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108224:

[#407250] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

What do you get if you puts "ARGV == #{ARGV[0]}" ?

[#407251] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108238:

[#407252] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

So it's like the other guys mentioned, there appears to be no file(or

[#407255] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108240:

[#407257] Re: Problem with FileUtils move command. — Hassan Schroeder <hassan.schroeder@...> 2013/05/08

On Wed, May 8, 2013 at 6:54 AM, Peter Bailey <lists@ruby-forum.com> wrote:

[#407261] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Hassan Schroeder wrote in post #1108253:

[#407263] Re: Problem with FileUtils move command. — Hassan Schroeder <hassan.schroeder@...> 2013/05/08

On Wed, May 8, 2013 at 8:22 AM, Peter Bailey <lists@ruby-forum.com> wrote:

[#407265] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Hassan Schroeder wrote in post #1108265:

[#407258] Connect to a switch with SSH — "Rudá G." <lists@...>

I need connect to a Cisco Switch with SSH and Ruby. The main problem is

17 messages 2013/05/08

[#407298] Trucking All Http Request from a web page — Ja Tse <lists@...>

Hi,

10 messages 2013/05/09

[#407383] Enum#each issue — Love U Ruby <lists@...>

I am not able to understand why I am getting the error for the method

26 messages 2013/05/13

[#407464] Is it 'safe' to upgrade to ruby 2.0 by now? — Panagiotis Atmatzidis <atma@...>

Hello,

11 messages 2013/05/16

[#407471] Could you give me some Website for Learning Ruby — haxuan lac <lists@...>

I'm learning Ruby and I search some Website for learning Ruby

10 messages 2013/05/16

[#407517] Ruby Programming Practice — "buzz k." <lists@...>

Hey guys have been learning the basics of ruby and practicing some

30 messages 2013/05/19
[#407725] Re: Ruby Programming Practice — "buzz k." <lists@...> 2013/05/29

@Chad Perrin

[#407726] Re: Ruby Programming Practice — Stu <stu@...> 2013/05/29

On Wed, May 29, 2013 at 2:37 AM, buzz k. <lists@ruby-forum.com> wrote:

[#407740] Re: Ruby Programming Practice — Chad Perrin <code@...> 2013/05/29

On Wed, May 29, 2013 at 05:26:44PM +0900, Stu wrote:

[#407742] Re: Ruby Programming Practice — Brandon Weaver <keystonelemur@...> 2013/05/29

Skip FreeBSD and put armored plating on that bike by going OpenBSD.

[#407743] Re: Ruby Programming Practice — Chad Perrin <code@...> 2013/05/29

On Thu, May 30, 2013 at 02:54:51AM +0900, Brandon Weaver wrote:

[#407523] new, with an idea, and not sure what to learn next — Wendy Randquist <lists@...>

Hi. I've been going through the CodeAcademy courses for Ruby over the

13 messages 2013/05/19

[#407527] How to run shoes built via gem — Ruby Student <ruby.student@...>

This is perhaps a silly question.

14 messages 2013/05/19

[#407565] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...>

Hi,

15 messages 2013/05/21
[#407567] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — botp <botpena@...> 2013/05/21

On Tue, May 21, 2013 at 9:05 PM, Samuel Williams <

[#407585] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...> 2013/05/21

Hi botp, you can create any configuration of DNS you like. RubyDNS doesn't

[#407586] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — botp <botpena@...> 2013/05/22

On Wed, May 22, 2013 at 7:02 AM, Samuel Williams <

[#407587] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...> 2013/05/22

Hi, you can use geo-ip for finding the location, geographically speaking.

[#407621] Regular expression to find a break in a pattern — Joel Pearson <lists@...>

I have a large file which lots of gibberish in and I'm trying to find

10 messages 2013/05/23

[#407639] On Green Shoes, do we have a broken arrow? — Ruby Student <ruby.student@...>

Team,

12 messages 2013/05/24

[#407715] AAARRRRGH! s="#$" => SyntaxError: compile error — Tadeusz Bochan <lists@...>

Hi.

13 messages 2013/05/28

[#407738] sending a gmail using ruby — David Munyaka <lists@...>

please help analyzing this code and please give me some recommendations.

26 messages 2013/05/29

[#407763] I can't unsubscribe, please help — Matthew Westerberg <matthew@...>

Hi,

11 messages 2013/05/29

[#407820] Eloquent Ruby Kaprekar's Number — Alphonse 23 <lists@...>

Which way is the eloquent ruby way to write this algorithm?

18 messages 2013/05/30

Re: Eloquent Ruby Kaprekar's Number

From: tamouse mailing lists <tamouse.lists@...>
Date: 2013-05-31 02:20:23 UTC
List: ruby-talk #407822
On Thu, May 30, 2013 at 9:08 PM, tamouse mailing lists
<tamouse.lists@gmail.com> wrote:
> On Thu, May 30, 2013 at 6:59 PM, Alphonse 23 <lists@ruby-forum.com> wrote:
>> Which way is the eloquent ruby way to write this algorithm?
>>
>> Kaprekar Number
>>
>> 9 is a Kaprekar number since
>> 9 ^ 2 = 81 and 8 + 1 = 9
>>
>> 297 is also Kaprekar number since
>> 297 ^ 2 = 88209 and 88 + 209 = 297.
>>
>> In short, for a Kaprekar number k with n-digits, if you square it and
>> add the right n digits to the left n or n-1 digits, the resultant sum is
>> k.
>>
>> !Spoiler Below!
>>
>> # way one
>> def kaprekar?(k)
>>   ks = k**2
>>   lenf = (ks.to_s.length)-1
>>   lenh = (ks.to_s.length / 2) - 1
>>   a = ks.to_s[0..lenh].to_i
>>   b = ks.to_s[lenh+1..lenf].to_i
>>   k == (a+b)
>> end
>>
>> # way two
>> def kaprekar?(k)
>>   a = (k**2).to_s[0..((((k**2).to_s.length) / 2) - 1)].to_i
>>   b = (k**2).to_s[(((((k**2).to_s.length) / 2) -
>> 1)+1)..(((k**2).to_s.length)-1)].to_i
>>   k == (a+b)
>> end
>>
>> # way three
>> def kaprekar?(k)
>>   k == ((k**2).to_s[0..((((k**2).to_s.length) / 2) -
>> 1)].to_i)+((k**2).to_s[(((((k**2).to_s.length) / 2) -
>> 1)+1)..(((k**2).to_s.length)-1)].to_i)
>> end
>>
>> def  simple_test(i)
>>   if kaprekar?(i) == true then p "success on #{i}" else "fail on #{i}"
>> end
>> end
>>
>> simple_test(9)
>> simple_test(297)
>>
>> All three pass my tests, but which of the three would be considered the
>> most eloquent written ruby way? As in, which would be consider best
>> style-wise? The first takes up the most lines, but uses less nesting.
>> The last uses only one line but uses lots of nesting.
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>
> This is the way I'd do it:
>
> def kaprekar?(k)
>
>   n = k.to_s.length
>   ks = (k*k).to_s
>   l = ((n-1)-(n%2))
>   l = 0 if l < 0
>
>   nl = ks[0..l].to_i
>   nr = ks[-n..-1].to_i
>
>   k == nl + nr
>
> end
>
>
> I could not say if that's more rubyist or not, though. I imagine there
> is probably a way to use map/reduce and pass a block or something ;)
>

Well, that will teach me not to test more thoroughly.

def kaprekar?(k)

  n = k.to_s.length
  ks = (k*k).to_s
  l = ((n-1)-(ks.length%2))
  l = 0 if l < 0

  nl = ks[0..l].to_i
  nr = ks[-n..-1].to_i

  if k == nl + nr
    puts "k=#{k}, n=#{n}, ks=#{ks}, l=#{l}, nl=#{nl}, nr=#{nr}, nl+nr=#{nl+nr}"
    true
  else
    false
  end


end

and some tests:

irb(main):056:0> 0.upto(1000).each {|i| puts "#{i} is kaprekar" if kaprekar?(i)}
k=0, n=1, ks=0, l=0, nl=0, nr=0, nl+nr=0
0 is kaprekar
k=9, n=1, ks=81, l=0, nl=8, nr=1, nl+nr=9
9 is kaprekar
k=45, n=2, ks=2025, l=1, nl=20, nr=25, nl+nr=45
45 is kaprekar
k=55, n=2, ks=3025, l=1, nl=30, nr=25, nl+nr=55
55 is kaprekar
k=99, n=2, ks=9801, l=1, nl=98, nr=1, nl+nr=99
99 is kaprekar
k=297, n=3, ks=88209, l=1, nl=88, nr=209, nl+nr=297
297 is kaprekar
k=703, n=3, ks=494209, l=2, nl=494, nr=209, nl+nr=703
703 is kaprekar
k=999, n=3, ks=998001, l=2, nl=998, nr=1, nl+nr=999
999 is kaprekar

In This Thread