Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
methods to create toys arrays and hashes
class Array
def self.toy(n=10, &block)
block_given? ? Array.new(n,&block) : Array.new(n) {|i| i+1}
end
end
class Hash
def self.toy(n=10)
Hash[Array.toy(n).zip(Array.toy(n){|c| (96+(c+1)).chr})]
end
end
@konung

This comment has been minimized.

Copy link

commented Mar 2, 2011

Hi. I followed this from stack overflow.
Why not just extend the classes?

class Array
  def self.toy(n=10,&block)
    block_given? ? Array.new(n,&block) : Array.new(n) {|i| i+1}
  end
end   
class Hash
  def self.toy(n=10)
    Hash[Array.toy(n).zip(Array.toy(n){|c| (96+(c+1)).chr})]
  end
end

then when you need a toy hash or array to play with in irb you can just:

Hash.toy
Array.toy

Seems a little more rubyesque? No?

@lucapette

This comment has been minimized.

Copy link
Owner Author

commented Mar 2, 2011

Yep very very nice I think i'll steal them :)

@konung

This comment has been minimized.

Copy link

commented Mar 2, 2011

Well then we are even since I stole your idea for my .irbrc :-)

@lucapette

This comment has been minimized.

Copy link
Owner Author

commented Mar 2, 2011

:D perfect!

@dx7

This comment has been minimized.

Copy link

commented Jun 9, 2011

good idea! thanks!

@lucapette

This comment has been minimized.

Copy link
Owner Author

commented Jun 9, 2011

glad you like it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.