Skip to content

Instantly share code, notes, and snippets.

@mrb

mrb/types.rb

Created Apr 10, 2014
Embed
What would you like to do?
Ruby's types
irb(main):002:0> 10*"*"
TypeError: String can't be coerced into Fixnum
from (irb):2:in `*'
from (irb):2
from /Users/mrb/.rbenv/versions/1.9.3-p545/bin/irb:12:in `<main>'
irb(main):003:0> "*"*10
=> "**********"
@404pnf

This comment has been minimized.

Copy link

@404pnf 404pnf commented Apr 23, 2014

It makes sense to say 3 times a string.
It doesn't make sense to say 'some string' times 10.
Therefore, I am all for this operator overloading. :)

What happened under the hood:

"some string ".send(:*, 3)
=> "some string some string some string "

10.send(:_, 'some string')
TypeError: String can't be coerced into Fixnum
from (irb):12:in _' from (irb):12 from /Users/anon/.rvm/rubies/ruby-2.1.0/bin/irb:11:in

'

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.