Syntactic Sugar Question

Ruby has the nifty syntactic sugar by which

foo ||= “oink”

will set foo to “oink” if foo is uninitialized (or nil or false), but will
leave foo alone otherwise. I like this, but how the heck does it work?
This is evidently syntax sugar for

foo = (foo || “oink”)

which works the same way. But if foo is uninitialized, the expression

foo || “oink”

gives an error message like
NameError: undefined local variable or method `foo’ for #Object:0x401e5ce0

To add to the mystery,

foo ||= false unless not foo

sets foo to nil (not to false)! I’m a confused piggy.
Regards, Bret
http://www.rexx.com/~oinkoink

Hi,

This is evidently syntax sugar for

foo = (foo || “oink”)

This expression is an assignment.

To add to the mystery,

foo ||= false unless not foo

This equivalents to

foo = false unless foo unless not foo
foo = false if not foo if foo
foo = false if not foo and foo
foo = false if false

Therefore, the assignment never occur.

···

At Fri, 15 Nov 2002 09:01:26 +0900, Bret Jolly wrote:


Nobu Nakada

Hello Bret,

Friday, November 15, 2002, 3:01:26 AM, you wrote:

foo ||= "oink"
foo = (foo || “oink”)

from the “programming ruby”:

Variable/Method Ambiguity

When Ruby sees a name such as ``a’’ in an expression, it needs to determine if it is a local variable reference or a call to a method with no parameters. To decide which is the case, Ruby uses a heuristic. As Ruby reads a source file, it keeps track of symbols that have been assigned to. It assumes that these symbols are variables. When it subsequently comes across a symbol that might be either a variable or a method call, it checks to see if it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable; otherwise it treats it as a method call. As a somewhat pathological case of this, consider the following code fragment, submitted by Clemens Hintze.

def a
print "Function ‘a’ called\n"
99
end

for i in 1…2
if i == 2
print “a=”, a, "\n"
else
a = 1
print “a=”, a, "\n"
end
end

produces: a=1
Function ‘a’ called
a=99

During the parse, Ruby sees the use of a'' in the first print statement and, as it hasn't yet seen any assignment toa,’’ assumes that it is a method call. By the time it gets to the second print statement, though, it has seen an assignment, and so treats ``a’’ as a variable.

Note that the assignment does not have to be executed—Ruby just has to have seen it. This program does not raise an error.

a = 1 if false; a

···


Best regards,
Bulat mailto:bulatz@integ.ru

I’m confused.

I keep reading about graphical elements in GUIs being called a widget…

Merriam-Webster On-Line…

One entry found for widget.

Main Entry: wid·get
Pronunciation: 'wi-j&t
Function: noun
Etymology: alteration of gadget
1 : GADGET
2 : an unnamed article considered for purposes of hypothetical example

It just makes no sense.

Any pointers as to why it’s called a widget??

Now, the reason I’m asking this…

I’m creating a GUI along the lines of Qt, Tk, Tcl… but what do I call the
main graphical ‘object’ from which most of the GUI elements are
constructed??

-Rich

···

Date: 1926

Any pointers as to why it’s called a widget??

For what it’s worth:

http://kldp.org/~eunjea/jargon/index.php?query=widget+&keyword=on

Now, the reason I’m asking this…

I’m creating a GUI along the lines of Qt, Tk, Tcl… but what do I call the
main graphical ‘object’ from which most of the GUI elements are
constructed??

Which compels me to ask, why are you creating a GUI along the lines of Qt, Tk,
Tcl?

James

···

-Rich

Any pointers as to why it’s called a widget??

For what it’s worth:

http://kldp.org/~eunjea/jargon/index.php?query=widget+&keyword=on

I guess it’s just saying that the word ‘widget’ is a part of history…

I’m creating a GUI along the lines of Qt, Tk, Tcl…>

Which compels me to ask, why are you creating a GUI along the lines of Qt,
Tk,
Tcl?

Easier… Kinda like Ruby… it’s meant to make life easier…

Plus - the recent problems with FreeRIDE not working on Mac…

-Rich