Skip to content

Instantly share code, notes, and snippets.

@deepak
Created December 12, 2010 14:49
Show Gist options
  • Select an option

  • Save deepak/738089 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/738089 to your computer and use it in GitHub Desktop.
Crappy non-functional code. tried to add a prefix addition to fixnum like the c operator ++<var> and <var>++
# tried to add a prefix addition to fixnum like the c operator ++<var> and <var>++
# asked by linusoleander on #ruby irc, discussed with apeiros, banisterfiend and My_Hearing
# as fixnum is a kind of constant, the idea was to store one value but use and display another
class Fixnum
# there is no intialize method in fixnum so cannot hook @actual_value to initilization
# also += 1 is a keyword so cannot hook assignment to a variable into the fixnum
# would have been possible if ruby had a repl (reader macros) like lisp
def pre_plus
@actual_value ||= (pre = self)
#p "pre: #{pre}"
@actual_value = pre + 1
pre
end
def to_i
@actual_value ||= self
end
end
foo = 1
p "foo: #{foo}" #1
p "foo.pre_plus: #{foo.pre_plus}" #1
p "foo: #{foo}" #1
p "foo.to_i: #{foo.to_i}" #2
foo+=1
p "after foo+=1 | to_i #{foo.to_i.inspect}" #2 - sucks and blows
p "after foo+=1 | inspect #{foo.inspect}" #2 - sucks and blows - += works on actual value not to_i value
# Fixnum, NilClass, TrueClass, FalseClass are special in that
# they only have one copy per value and we cannot define singleton methods on them
p "object_id same: #{1.object_id == 1.object_id}" #true
@deepak

deepak commented Dec 12, 2010

Copy link
Copy Markdown
Author

suggestion to wrap it in a class, this is too fragile
foo, bar = 5, 5.pre_plus
p bar.to_to_i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment