[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

change of Symbol to respond to intern?

Tom M

8/14/2007 3:26:00 PM

I often find myself writing methods with the following form:

def foo(sym_or_str)
@iv = sym_or_str
end

when I go to compare later vs @iv, I can do:

def compare_vs_iv(arg)
if arg.to_s == @iv.to_s then
...
end
...
end

but I can't do:

if arg.intern == @iv.intern then
...
end

This is because symbols don't respond to intern. Is there any good
reason Symbol doesn't respond to intern, while String responds to
to_s, Array responds, to to_a, etc.? If not, I would like to propose
that the intern method be added to Symbol at some point.

Thoughts?

5 Answers

Rubén Medellín

8/14/2007 4:47:00 PM

0

> Thoughts?

irb(main):005:0> :FOO.to_sym
=> :FOO
irb(main):006:0> "FOO".to_sym
=> :FOO

Or you can always do something like

class Symbol
def intern
self
end
end

Tim Hunter

8/14/2007 5:13:00 PM

0

On Aug 14, 11:26 am, thomas.mack...@gmail.com wrote:
> This is because symbols don't respond to intern. Is there any good
> reason Symbol doesn't respond to intern, while String responds to
> to_s, Array responds, to to_a, etc.? If not, I would like to propose
> that the intern method be added to Symbol at some point.

Symbol responds to #to_sym, as does String. Will that do?

Ilan Berci

8/14/2007 5:18:00 PM

0

unknown wrote:


>
> This is because symbols don't respond to intern. Is there any good
> reason Symbol doesn't respond to intern, while String responds to
> to_s, Array responds, to to_a, etc.? If not, I would like to propose
> that the intern method be added to Symbol at some point.

irb(main):001:0> class Symbol; alias_method :intern, :to_sym; end
=> Symbol
irb(main):002:0> :symbol.intern
=> :symbol

--
Posted via http://www.ruby-....

Tom M

8/14/2007 8:00:00 PM

0

On Aug 14, 11:26 am, thomas.mack...@gmail.com wrote:
> I often find myself writing methods with the following form:
>
> def foo(sym_or_str)
> @iv = sym_or_str
> end
>
> when I go to compare later vs @iv, I can do:
>
> def compare_vs_iv(arg)
> if arg.to_s == @iv.to_s then
> ...
> end
> ...
> end
>
> but I can't do:
>
> if arg.intern == @iv.intern then
> ...
> end
>
> This is because symbols don't respond to intern. Is there any good
> reason Symbol doesn't respond to intern, while String responds to
> to_s, Array responds, to to_a, etc.? If not, I would like to propose
> that the intern method be added to Symbol at some point.
>
> Thoughts?

I knew about overloading Symbol, but I think we can all agree that's
not really a practical solution for use in a bigger system. I didn't
know about to_sym though, and I think that's perfect! Shows what I
get for thinking that I knew the core API pretty well...

Thanks... --Tom

Simon Krahnke

8/15/2007 4:12:00 PM

0

* <thomas.macklin@gmail.com> (22:00) schrieb:

> I knew about overloading Symbol, but I think we can all agree that's
> not really a practical solution for use in a bigger system. I didn't
> know about to_sym though, and I think that's perfect! Shows what I
> get for thinking that I knew the core API pretty well...

But probably want to do the to_s anyway, because that doesn't pollute
the symbol table. Strings can be garbage collected, symbols stay for
ever.

mfg, simon .... l