From: Dave Thomas Date: 2000-11-23T11:14:28+09:00 Subject: [ruby-talk:6540] Re: Question about the behavior of write attributes in blocks "Christoph Rippel" writes: > I lifted the following two lines from your (great) book - Page 285 > ************ > a = ["a","b","c","d"]. > a.collect! {|x| x+"!"} > p a > ************* > which has the output > >> undefined method `a' for ["a", "b", "c", "d"]:Array (NameError) You have an extraneous '.' at the end of the first line. Ruby says "there should be a message name following the dot" so it continues scanning and finds the 'a' at the start of the next line. But [1,2,3].a isn't going to work, as arrays don't handle a message called 'a'. > ************ > p 34.id == 34.id > ************ > returns >> true - in contrast to > ************ > p "x".id == "x".id > *************** > which returns >>false. From this I would conclude that ``34'' is > semantically a singleton ``34'' object - but the interpreter barks at the > following lines Yes - numbers are effectively singletons, while string literals generate a new string every time they are evaluated. > ********* > module Withbla > def bla > end > end > x = 34 > x.extend(Withbla) > *********** > with the complain >> `extend_object': can't define singleton (TypeError) - > what is the semantic reasoning behind this? My guess is that it's because Fixnums have value semantics, and hence there is no real object behind each one. (They are objects, but they don't have objects). This means that Ruby has nothing in memory to hang the additional definitions onto. For the same reason, you can't do x = 23 def x.ddd end or class <