Folks:
A little help, please, with following script:
···
===============================
require ‘tk’
def positionWindow(w,x=300,y=300)
geom = ‘+300+600’ #<< why does this work, #geom = ‘+’, x, ‘+’, y #<< and this NOT work (when uncommented!),
print geom, “\n” #<< when they both print out the same?
w.geometry(geom) #<< the commented statement gives an error
here!
end
Folks:
A little help, please, with following script:
require ‘tk’
def positionWindow(w,x=300,y=300)
geom = ‘+300+600’ #<< why does this work, #geom = ‘+’, x, ‘+’, y #<< and this NOT work (when uncommented!),
print geom, “\n” #<< when they both print out the same?
w.geometry(geom) #<< the commented statement gives an error
here!
end
The commented statement assigns an
array to geom. It just happens to
look the same when you print it.
If you used p instead of print or
puts, you’d notice the difference.
Is there a way to concatonate strings without having to use the x.to_s method?
In other words, because “+” is a string to start the expression, wouldnt the
plus operator actually be the ‘+’ method within the string object? And thus
wouldnt the string want to use ‘string values’ of the objects they are concatonating?
Can we use the shortcut of
geom = "+" + x + "+" + y
??
Thanks
–Andrew
···
On Tue, Oct 22, 2002 at 01:23:37PM +0900, Hal E. Fulton wrote:
def positionWindow(w,x=300,y=300)
geom = ‘+300+600’ #<< why does this work, #geom = ‘+’, x, ‘+’, y #<< and this NOT work (when uncommented!),
print geom, “\n” #<< when they both print out the same?
w.geometry(geom) #<< the commented statement gives an error
here!
end
Is there a way to concatonate strings without having to use the x.to_s
method?
In other words, because “+” is a string to start the expression, wouldnt
the
plus operator actually be the ‘+’ method within the string object? And
thus
wouldnt the string want to use ‘string values’ of the objects they are
concatonating?
Can we use the shortcut of
geom = “+” + x + “+” + y
No… you can concatenate strings without to_s,
of course, but not a string and a number.
Hal
···
----- Original Message -----
From: “andrew delboy” andrew@cyber.com.au
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, October 21, 2002 11:49 PM
Subject: Re: Ruby/Tk newbie question
Look at Hal’s first suggestion. That basically does exactly what
you’re after (e.g., “+#{x}+#{y}” is the very same thing as the
second version and does exactly what you want). Doing #{} within a
string does an implicit #to_s call.
-austin
– Austin Ziegler, austin@halostatue.ca on 2002.10.22 at 02.00.47
···
On Tue, 22 Oct 2002 13:49:47 +0900, andrew delboy wrote:
On Tue, Oct 22, 2002 at 01:23:37PM +0900, Hal E. Fulton wrote:
What you want is something like one of these:
geom = “+#{x}+#{y}”
geom = “+” + x.to_s + “+” + y.to_s
Is there a way to concatonate strings without having to use the
x.to_s method?
In other words, because “+” is a string to start the expression,
wouldnt the plus operator actually be the ‘+’ method within the
string object? And thus wouldnt the string want to use ‘string
values’ of the objects they are concatonating?
As also already responded by other people, Ruby by default does not call
the “to_s” method for a string in its “+” method. In this regard, Ruby
wants you to be explicit about what you are doing. But of course, nothing
prevents you to redifine the string “+” method:
a = 'aString'
b = 1
#puts a + b # -> "failed to convert Fixnum into String"
class String
alias oldPlus +
def +(anObj)
oldPlus(anObj.to_s)
end
end
puts a + b # -> "aString1"
Or, as already been pointed out, you can just put the variables in #{} in
a double-quoted string, in which case the objects’ to_s methods are
automatically called for you. (Well… Ruby is really not “there is only
one way to do it”, but perhaps as some people call it, “there is always
the best way to do it” )
Regards,
Bill
···
===========================================================================
andrew delboy andrew@cyber.com.au wrote:
Is there a way to concatonate strings without having to use the x.to_s method?
In other words, because “+” is a string to start the expression, wouldnt the
plus operator actually be the ‘+’ method within the string object? And thus
wouldnt the string want to use ‘string values’ of the objects they are concatonating?