[#401849] If statement — Masoud Ahmadi <lists@...>

Will anyone be able to point out what I am doing wrong.

15 messages 2012/12/02

[#401987] Trying to get "translator" to work — JD KF <lists@...>

So, basically, I'm trying to get the below code to work properly for

12 messages 2012/12/06

[#402012] Need help to select some listbox item in different listbox together — Jonathan Masato <lists@...>

Hello,

10 messages 2012/12/07

[#402045] if n belongs to set a and m belongs to set b repeat some steps, How? — "zubair a." <lists@...>

We can do so in java and similar languages like:

11 messages 2012/12/08

[#402078] Time.new(2001, 12, 3).to_i returns wrong value — Robert Buck <lists@...>

I am doing something that not many do, I am writing a database driver

9 messages 2012/12/09

[#402145] How I can create/extract a variable/hash into the current binding in Ruby? — Ramon de C Valle <rcvalle@...>

Hi,

12 messages 2012/12/12

[#402205] Wondering About Flatiron School — "Kevin Y." <lists@...>

Hi everyone!,

35 messages 2012/12/15
[#402207] Re: Wondering About Flatiron School — Chad Perrin <code@...> 2012/12/15

On Sat, Dec 15, 2012 at 11:51:08AM +0900, Kevin Y. wrote:

[#402214] Ruby quick reference arranged in ASCII sequence? — Old Grantonian <lists@...>

As a ruby beginner, I would be grateful for any links to a ruby

17 messages 2012/12/15

[#402226] print - and strip text between tags using Nokogiri — Paul Mena <lists@...>

I'm a Ruby Newbie trying to write a program to process thousands of HTML

13 messages 2012/12/15

[#402332] Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...>

Hello all,

37 messages 2012/12/19
[#402342] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

First of all, thanks for the fast responses!

[#402352] Re: Perl to Ruby: regex captures to assignment. — Robert Klemme <shortcutter@...> 2012/12/20

On Thu, Dec 20, 2012 at 1:38 AM, Derrick B. <lists@ruby-forum.com> wrote:

[#402357] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

Robert Klemme wrote in post #1089733:

[#402359] trying to strip characters from a line — Paul Mena <lists@...>

I'm reading a table from a MySQL database and then processing it row by

18 messages 2012/12/20

[#402394] simple division: -9 / 5 = -2 what? — "Derrick B." <lists@...>

$ irb

13 messages 2012/12/22

[#402412] POLS and string-handling — Paul Magnussen <lists@...>

Hi,

14 messages 2012/12/22

[#402460] "Open" dialog of Windows — "Damián M. González" <lists@...>

Hi guys, been researching about pop up the "open" file dialog of

11 messages 2012/12/24

[#402466] How do I install Ruby on my Ubuntu 12.10 partition. — Kaye Ng <lists@...>

I already have Ruby installed on my Windows 7 partition.

23 messages 2012/12/25

[#402510] Ruby Association Certified Ruby Programmer — Sean Westfall <lists@...>

How well respected is this certification in the industry: Ruby

27 messages 2012/12/27
[#402528] Re: Ruby Association Certified Ruby Programmer — Peter Hickman <peterhickman386@...> 2012/12/27

On 27 December 2012 01:28, Sean Westfall <lists@ruby-forum.com> wrote:

[#402519] using shebang with rvm? — Wesley Rishel <lists@...>

What would be the appropriate path to use after a shebang in the first

10 messages 2012/12/27

[#402555] numeric? — Brandon Weaver <keystonelemur@...>

I've found a bit of an annoyance trying to find out if a number is numeric

20 messages 2012/12/27

[#402580] Ruby Koans regarding Hashes. — "Derrick B." <lists@...>

I am trying to understand this, so let me know how I do. :) I know

18 messages 2012/12/28

[#402609] can't open new ruby program under "new" context menu — "Lee V." <lists@...>

I'm stuck on the new version at trying to do something very simple.

10 messages 2012/12/28
[#402618] Re: can't open new ruby program under "new" context menu — "Lee V." <lists@...> 2012/12/28

I just uninstalled what I had and reinstalled using the steps given in

[#402645] Re: can't open new ruby program under "new" context menu — "Derrick B." <lists@...> 2012/12/29

Lee V. wrote in post #1090514:

[#402653] Re: can't open new ruby program under "new" context menu — Lee Veinot <lee_veinot@...> 2012/12/30

Well, I'm up to page 43 in Chris Pine's book and having a lot of fun, but I still can't figure out two basic things.  One is what I've already asked you about.  I'm just going to paste what his book says so you can see what I'm having trouble with:

[#402642] require "test/unit" — "Mattias A." <lists@...>

Hi,

17 messages 2012/12/29
[#402667] Re: require "test/unit" — "Mattias A." <lists@...> 2012/12/31

Hi Dami叩n M. Gonz叩lez!

[#402747] Re: require "test/unit" — "Derrick B." <lists@...> 2013/01/04

Mattias A. wrote in post #1090700:

[#402749] Re: require "test/unit" — sto.mar@... 2013/01/04

Am 04.01.2013 19:48, schrieb Derrick B.:

Re: Help me improve Hash#rekey

From: Robert Klemme <shortcutter@...>
Date: 2012-12-11 11:55:32 UTC
List: ruby-talk #402130
On Mon, Dec 10, 2012 at 8:44 PM, Intransition <transfire@gmail.com> wrote:
>
>
> On Monday, December 10, 2012 7:22:17 AM UTC-5, Robert Klemme wrote:
>>
>> On Sun, Dec 9, 2012 at 7:19 PM, Intransition <tran...@gmail.com> wrote:
>> > The note `TODO: Improve Hash#rekey code!!!` has been in my docs for too
>> > long. I could use other's insights and thought others might enjoy the
>> > challenge. So here's the code:
>>
>> I think the problem is in the API.  That's what makes it too complex.
>> There are too many cases of specific handling and options (see below):
>>
>> 1. Unnecessary option: if the key is supposed to stay intact the block
>> should just return the original key.
>
> I agree. Just wanted someone else to confirm. Out it goes!

:-)

>> >     #
>> >     #   foo = { :name=>'Gavin', :wife=>:Lisa }
>> >     #   foo.rekey{ |k| k.to_s }  #=>  { "name"=>"Gavin", "wife"=>:Lisa }
>> >     #   foo                      #=>  { :name =>"Gavin", :wife=>:Lisa }
>> >     #
>> >     # If no key map or block is given, then all keys are converted
>> >     # to Symbols.
>>
>> 2. Why that default?  In my mind this is too much implicit logic.
>> Also this can be easily achieved with
>>
>> hash.rekey(&:to_sym)
>
>
> I can understand that. I used the default b/c the vast majority of the time
> I was using it to convert to symbols. Rather then have yet another method
> like ActiveSupport's `symbolize_keys` it made more sense to me to just give
> #rekey this as the default.
>
> I can't really take that back now. It's been too long part of the API.

Well, then you mustn't change *anything* which affects observable behavior.

>> >     #
>> >     # Note that if both a +key_map+ and a block are given, the +key_map+
>> > is
>> >     # applied first then the block.
>>
>> 3. I would change that to EITHER block OR map argument, but not both.
>
>
> Yea, I thought about that when I added support for the mapping. That was
> actually something that came later that the original block form. At first I
> had thought about making an entirely different method, but then thought that
> was a waste and that it made more sense as part of #rekey. So when I first
> added it I did an "either or", just as you suggest. But then I thought
> "why?" it certainly can handle both even if people will almost never use
> both.
>
> What do you think? Does it really matters enough to change it now? Like I
> said, I doubt anyone has used both, so this is something that could be
> change if it really is worth it.

I would get rid of that behavior.  After all, if someone wants do do
Hash lookups as part of conversion they can still do that in the block
without much effort.

I also noticed a slight asymmetry between block and Hash: a block will
return something for every key passed in.  But since a Hash is fixed
at the time of method call (modulo default_proc of course) there is
not really a way to handle absent keys ad hoc.

>> > Not the use of `facets/na`. That is defined as:
>> >
>> >     class << NA = ArgumentError.new
>> >       def inspect ; 'N/A' ; end
>> >       def method_missing(*); self; end
>> >     end
>> >
>> > But it is really nothing more than a dummy object used to mean Not
>> > Applicable. So in the case of #rekey, if the block returns NA then the
>> > key
>> > goes unchanged. Thinking about it again now, it's probably unnecessary,
>> > but
>> > I had wanted a way to say "leave it alone" while also making sure that
>> > `nil`
>> > could still be used as a key (even if that's rare). Feel free to remove
>> > the
>> > NA business, but if you do please explain why you think its not needed.
>>
>> If one needs a special key one can use a Symbol for that as
>> efficiently as nil.  nil is the value return if something is absent
>> and I believe it does not make for a good key in a Hash.
>
>
> Wouldn't use symbols b/c then you have a special exception. NA was made just
> for such cases. But you probably right that `nil` doesn't make a good hash
> key no matter what. Nonetheless, it doesn't really matter b/c as you said
> above, they can just return the original key.
>
>>
>> > Best solution will get their name put in front of CREDITs for the next
>> > release of Facets.
>>
>> :-)
>>
>> class Hash
>>   def rekey(mapping = nil, &convert)
>>     c = convert || mapping
>>     dup.tap do |h| # preserve type and defaults
>>       h.clear
>>       each_pair {|k, v| h[c[k] || k] = v}
>>     end
>>   end
>> end
>>
>
> Sweet. Much smaller than mine, that's for damn sure!!!

Yeah, but that's easy when one removes the complexity in behavior.

> Put the default
> :to_sym back in and we could have a deal :-)

Well, that's easy, isn't it?

def rekey(mapping = nil, &convert)
  c = convert || mapping || :to_sym.to_proc

> I'm need to test and benchmark it first though.

Have fun!

> Oh, and nice use of polymorphism using #[] for both proc and hash retrieval!

Thank you! :-)

Kind regards

robert



--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

In This Thread