Skip to content

Instantly share code, notes, and snippets.

@glurp
Created February 23, 2012 17:38
Show Gist options
  • Select an option

  • Save glurp/1893964 to your computer and use it in GitHub Desktop.

Select an option

Save glurp/1893964 to your computer and use it in GitHub Desktop.
litle calculette, with Ruiby, without list!
require 'ruiby'
class App < Ruiby_gtk
####################### last result is '_' alias
def _()
@lastValue || 0
end
######### context evaluation formula : the context is the first call of b()
def b
@bind_ || @bind_=binding
end
def calc(formule)
txt=formule.chomp.strip
value=(eval(txt,b) )
o=@lastValue
@lastValue=value
txt=txt.gsub(/_/,"("+o.to_s+")")
append_result(txt,value.to_s)
rescue
error("#{$!}\n #{$!.backtrace().join("\n ")}")
end
def append_result(txt,value)
append_to(@body) {
clickable(:selection) { flow {
sloti(label(txt + (value ? (" ==> " + value.to_s) : "" ))) }
}
}
@vb.scroll_to_bottom()
end
def selection(wi)
t=wi.children[0].children[0].text.split(/\s+==>\s+/)[0]
@edit.text=t
end
def component()
@lastValue=0
@excel=[]
@body=1
stack do
flowi do
sloti(label("Formule : "))
@edit=slot(entry("",50) {
calc(@edit.text=$1) if @edit.text =~ /(.*)[= ]+$/
})
sloti(button("Go") { calc(@edit.text) })
end
@vb =vbox_scrolled(200,300) { stack { @body=stacki{} } }
sloti(button("Clear") { clear_append_to(@body) {} ; @excel=[] })
end
end
end
Ruiby.start { App.new("Calculate",200,100) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment