Ruby/Tk newbie question 
Author Message
 Ruby/Tk newbie question

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

root = TkRoot.new
aFrame = TkFrame.new(root)
positionWindow(root, 300, 600)

Tk.mainloop
==================================

Sigh,
Alan Walkington
United Defense, San Jose



Sat, 09 Apr 2005 11:24:49 GMT  
 Ruby/Tk newbie question
Quote:
----- Original Message -----

Newsgroups: comp.lang.ruby

Sent: Monday, October 21, 2002 11:16 PM
Subject: Ruby/Tk newbie question

> 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.

What you want is something like one
of these:

  geom = "+#{x}+#{y}"
  geom = "+" + x.to_s + "+" + y.to_s

Hal



Sat, 09 Apr 2005 12:23:37 GMT  
 Ruby/Tk newbie question

Quote:

> ----- Original Message -----

> > ===============================
> > 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

> 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?

Can we use the shortcut of

        geom = "+" + x + "+" + y
??

Thanks
--Andrew



Sat, 09 Apr 2005 12:49:47 GMT  
 Ruby/Tk newbie question

Quote:
----- Original Message -----


Sent: Monday, October 21, 2002 11:49 PM
Subject: Re: Ruby/Tk newbie question

> 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



Sat, 09 Apr 2005 13:42:02 GMT  
 Ruby/Tk newbie question

Quote:


>> 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?

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



Sat, 09 Apr 2005 14:02:30 GMT  
 Ruby/Tk newbie question
Hi Andrew,

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
===========================================================================

Quote:

> 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



Sat, 09 Apr 2005 21:44:36 GMT  
 Ruby/Tk newbie question
Thanks, Hal
Alan


Quote:
> 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.

> What you want is something like one
> of these:

>   geom = "+#{x}+#{y}"
>   geom = "+" + x.to_s + "+" + y.to_s

> Hal



Sun, 10 Apr 2005 00:34:40 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. newbie question about solaris and ruby/tk

2. Tk under Ruby [newbie to ruby]

3. interactive ruby/tk app (was: ruby/tk internals)

4. Ruby/Tk Newbie

5. Ruby newbie uninstall question?

6. Ruby newbie question

7. Newbie Question - Problems with Ruby on My Mac

8. -yet another (ruby) newbie question -string#concat

9. Newbie question: How are strings stored in Ruby?

10. Newbie question: Calling a shell script from ruby

11. newbie question about OOP - in Ruby

12. installing ruby on linux: simple newbie question

 

 
Powered by phpBB® Forum Software