This is just a minimal example. In real life, the objects in the hash
have been generated programatically, so I don't have variable
references for them -- just the keys.
Is it possible that there is a way around this error? If not, is there
another way to do what I'm trying to do? I'm pretty new to this, so
apologies in advance if it is an obvious mistake.
The problem here is that you can't involve a method call/dispatch (or two)
in the method definition. "a_hash.fetch(:a).to_s" has to make at least one
method call (a_hash.fetch(:a)), and then one defines the method on the
resulting object. Method definitions don't work this way (they need to be
resolvable in the current context, I believe). But there's a way to get
there:
a ={:a => "q"}
=> {:a=>"q"}
b = a[:a]
=> "q"
a[:a].object_id
=> -606856898
b.object_id
=> -606856898
def b.some_method
puts "Hi."
end
=> nil
b.some_method
Hi.
=> nil
a[:a].some_method
Hi.
=> nil
By the by, I'm not sure redefining a String's #to_s will have any effect. It
sounds strange - perhaps you're better off building a class for those
objects and defining to_s on that?
Cheers,
Arlen
···
On Sun, Mar 9, 2008 at 10:34 AM, mike leonard <mikeleonard@gmail.com> wrote:
This is just a minimal example. In real life, the objects in the hash
have been generated programatically, so I don't have variable
references for them -- just the keys.
Is it possible that there is a way around this error? If not, is there
another way to do what I'm trying to do? I'm pretty new to this, so
apologies in advance if it is an obvious mistake.
This is just a minimal example. In real life, the objects in the hash
have been generated programatically, so I don't have variable
references for them -- just the keys.
Is it possible that there is a way around this error? If not, is there
another way to do what I'm trying to do? I'm pretty new to this, so
apologies in advance if it is an obvious mistake.
def a\_hash\.fetch\(:a\)\.to\_s
"ALPHA"
end
This doesn't work because ruby thinks you want to define a_hash.fetch and is
then confused by the symbol in the parameter list. This however will work:
Congratulations Sean O'Halpin, you beat me to send button.
By the by, I'm not sure redefining a String's #to_s will have any effect. It
sounds strange - perhaps you're better off building a class for those
objects and defining to_s on that?
I have no idea what the possible purpose of redefining String#to_s is.
However, I think this brings to mind an interesting idea. As part of
Ruby's duck typing initiative, many methods call to_str on objects
they expect to be strings. Having String#to_str return something other
than self could produce some extremely confusing results.
Daniel Brumbaugh Keeney
···
On Sat, Mar 8, 2008 at 5:47 PM, Arlen Cuss <celtic@sairyx.org> wrote:
On Sun, Mar 9, 2008 at 10:34 AM, mike leonard <mikeleonard@gmail.com> wrote:
By the by, I'm not sure redefining a String's #to_s will have any effect. It
sounds strange - perhaps you're better off building a class for those
objects and defining to_s on that?
Agreed. I'm really wanting to redefine #to_s for a String; it was just
the first method that I thought of when I was typing up a minimal
example of what I was trying to do. The important bit was the thing
with the singleton method. Many thanks for your help.
That got it. Thanks to everyone who chimed in to clear that up for me.
···
On Mar 9, 4:29 am, Sebastian Hungerecker <sep...@googlemail.com> wrote:
mike leonard wrote:
> def a_hash.fetch(:a).to_s
> "ALPHA"
> end
This doesn't work because ruby thinks you want to define a_hash.fetch and is
then confused by the symbol in the parameter list. This however will work:
def (a_hash.fetch(:a)).to_s
"ALPHA"
end
HTH,
Sebastian
--
Jabber: sep...@jabber.org
ICQ: 205544826
Sorry, that should read I'm *not* really wanting to redefine #to_s for
a String.
···
On Mar 8, 9:26 pm, mike leonard <mikeleon...@gmail.com> wrote:
> By the by, I'm not sure redefining a String's #to_s will have any effect. It
> sounds strange - perhaps you're better off building a class for those
> objects and defining to_s on that?
Agreed. I'm really wanting to redefine #to_s for a String; it was just
the first method that I thought of when I was typing up a minimal
example of what I was trying to do. The important bit was the thing
with the singleton method. Many thanks for your help.