Module: Kernel
- Defined in:
- lib/source/ruby.rb
Overview
The Kernel
module contains methods that are mixed in to the window
object and are available in any context.
Instance Method Summary collapse
-
#block_given? ⇒ Boolean
FIX: Incomplete.
-
#fail ⇒ Object
FIX: Incomplete.
- #format(string) ⇒ Object
-
#global_variables ⇒ Object
FIX: Incomplete.
-
#lambda(func) ⇒ Object
call-seq: proc { |…| block } -> a_proc lambda { |…| block } -> a_proc.
-
#local_variables ⇒ Object
FIX: Incomplete.
-
#loop(&block) ⇒ Object
call-seq: loop { || block }.
-
#p ⇒ Object
FIX: Incomplete.
-
#print ⇒ Object
FIX: Incomplete.
-
#printf ⇒ Object
FIX: Incomplete.
-
#proc(func) ⇒ Object
call-seq: proc { |…| block } -> a_proc lambda { |…| block } -> a_proc.
-
#putc(int) ⇒ Object
call-seq: putc(int) -> int.
-
#puts(obj) ⇒ Object
call-seq: puts(obj, …) -> nil.
-
#raise ⇒ Object
FIX: Incomplete.
-
#rand(num = 0) ⇒ Object
call-seq: rand(num = 0) -> numeric.
-
#sleep(duration) ⇒ Object
call-seq: sleep() -> integer.
-
#sprintf(string) ⇒ Object
FIX: Incomplete.
-
#srand ⇒ Object
FIX: Incomplete.
-
#x ⇒ Object
:nodoc:.
Instance Method Details
#block_given? ⇒ Boolean
FIX: Incomplete
1297 1298 1299 |
# File 'lib/source/ruby.rb', line 1297 def block_given? `typeof(arguments[0])=='function'` end |
#format(string) ⇒ Object
1305 1306 1307 |
# File 'lib/source/ruby.rb', line 1305 def format(string) `m$sprintf(string)` end |
#global_variables ⇒ Object
FIX: Incomplete
1310 1311 |
# File 'lib/source/ruby.rb', line 1310 def global_variables end |
#lambda(func) ⇒ Object
call-seq:
proc { |...| block } -> a_proc
lambda { |...| block } -> a_proc
Equivalent to Proc.new …
FIX: Incomplete
1320 1321 1322 1323 1324 |
# File 'lib/source/ruby.rb', line 1320 def lambda(func) `result=new(c$Proc)()` `result._block=func` return `result` end |
#local_variables ⇒ Object
FIX: Incomplete
1327 1328 |
# File 'lib/source/ruby.rb', line 1327 def local_variables end |
#loop(&block) ⇒ Object
call-seq:
loop { || block }
Repeatedly executes the block.
1335 1336 1337 1338 1339 |
# File 'lib/source/ruby.rb', line 1335 def loop(&block) `var result=nil` `while(true){#{yield};}` return `result` end |
#proc(func) ⇒ Object
call-seq:
proc { |...| block } -> a_proc
lambda { |...| block } -> a_proc
Equivalent to Proc.new …
FIX: Incomplete
1360 1361 1362 1363 1364 |
# File 'lib/source/ruby.rb', line 1360 def proc(func) `result=new(c$Proc)()` `result._block=func` return `result` end |
#putc(int) ⇒ Object
call-seq:
putc(int) -> int
FIX: Incomplete
1370 1371 |
# File 'lib/source/ruby.rb', line 1370 def putc(int) end |
#puts(obj) ⇒ Object
call-seq:
puts(obj, ...) -> nil
FIX: Incomplete
1377 1378 1379 1380 1381 |
# File 'lib/source/ruby.rb', line 1377 def puts(obj) `var string=obj.m$toS&&obj.m$toS()||$q(''+obj)` `console.log(string._value.replace(/\\\\/g,'\\\\\\\\'))` return nil end |
#raise ⇒ Object
FIX: Incomplete
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 |
# File 'lib/source/ruby.rb', line 1384 def raise `var exception_class=c$RuntimeError,msg=$q('')` `if(arguments[0]&&arguments[0].m$isABool(c$Exception)){ var e=arguments[0]; }else{ if(arguments[0]&&arguments[0].m$class()==c$String){ msg=arguments[0]; }else{ if(arguments[0]!=null){ exception_class=arguments[0],msg=arguments[1]||msg; }; } }` `var e=e||exception_class.m$new(msg)` `e._stack=new Error().stack` `throw(e)` return nil end |
#rand(num = 0) ⇒ Object
call-seq:
rand(num = 0) -> numeric
Converts num to the integer max equivalent to num.to_i.abs
. If max is zero, returns a pseudo-random floating point number greater than or equal to 0 and less than 1. Otherwise, returns a pseudo-random integer greater than or equal to zero and less than max.
rand #=> 0.7137224264409899
rand(10) #=> 2
rand(100) #=> 79
1415 1416 1417 1418 |
# File 'lib/source/ruby.rb', line 1415 def rand(num = 0) `var max=Math.abs(parseInt(num))` `max==0?Math.random():parseInt(Math.random()*max)` end |
#sleep(duration) ⇒ Object
call-seq:
sleep([duration]) -> integer
Suspends activity for duration seconds (which may be fractional), then returns the number of seconds slept (rounded). Zero arguments causes sleep
to sleep forever.
Time.new #=> Wed Apr 09 08:56:32 CDT 2003
sleep 1.2 #=> 1
Time.new #=> Wed Apr 09 08:56:33 CDT 2003
sleep 1.9 #=> 2
Time.new #=> Wed Apr 09 08:56:35 CDT 2003
1433 1434 1435 1436 |
# File 'lib/source/ruby.rb', line 1433 def sleep(duration) `if(duration==null){while(true){};}else{var awaken=new(Date)().valueOf()+(1000*duration);while(new(Date)().valueOf()<awaken){};}` return `Math.round(duration)` end |
#sprintf(string) ⇒ Object
FIX: Incomplete
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 |
# File 'lib/source/ruby.rb', line 1439 def sprintf(string) `var i=1,source=string._value,result=[],m=$u,arg=$u,val=$u,str=$u,dL=$u,chr=$u,pad=$u; while(source){ if(m=source.match(/^[^%]+/)){result.push(m[0]);source=source.slice(m[0].length);continue;}; if(m=source.match(/^%%/)) {result.push('%'); source=source.slice(m[0].length);continue;}; // 1(0)2(wdth) 3(prec) 4(field-type ) if(m=source.match(/^%(0)?(\\d+)?(?:\\.(\\d+))?([bcdEefGgiopsuXx])/)){ arg = arguments[i]._value||arguments[i]; switch(m[4]){ case'b':str=parseFloat(arg).toString(2);break; case'c':str=String.fromCharCode(arg);break; case'd':val=parseInt(arg);str=''+val;break; case'E':val=parseFloat(arg);str=''+(m[3]?val.toExponential(m[3]):val.toExponential()).toUpperCase();break; case'e':val=parseFloat(arg);str=''+(m[3]?val.toExponential(m[3]):val.toExponential());break; case'f':val=parseFloat(arg);str=''+(m[3]?val.toFixed(m[3]):val);break; case'G':str='-FIX-';break; case'g':str='-FIX-';break; case'i':val=parseInt(arg);str=''+val;break; case'o':str=parseFloat(arg).toString(8);break; case'p':str=$q(arg).m$inspect()._value;break; case's':val=arg.m$toS&&arg.m$toS()._value||arg;str=(m[3]?val.slice(0,m[2]):val);break; case'u':val=parseInt(arg);str=''+(val<0?'..'+(Math.pow(2,32)+val):val);break; case'X':str=parseInt(arg).toString(16).toUpperCase();break; case'x':str=parseInt(arg).toString(16);break; }; if((dL=m[2]-str.length)!=0){for(chr=m[1]||' ',pad=[];dL>0;pad[--dL]=chr);}else{pad=[]}; result.push(pad.join('')+str); source=source.slice(m[0].length); i+=1; continue; }; throw('ArgumentError: malformed format string') }` return `$q(result.join(''))` end |
#x ⇒ Object
:nodoc:
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 |
# File 'lib/source/ruby.rb', line 1479 def x #:nodoc: #function m$sprintf(){ # var i=0,arg,f=arguments[i++],ary=[],m,p,c,x; # while (f) { # if(m=f.match(/^[^%]+/)){ # ary.push(m[0]); # }else{ // ['mtch',[1]:'digit-final?',[2]:'plus?',[3]:'0 or \' plus non-$ ?',[4]:'minus?',[5]:'digit?',[6]:'. plus digit?',[7]:'flag'] # if(m = f.match(/^%%/)){ary.push('%');}else{ # if(m=f.match(/^%(?:(\\d+)\\$)?(\\+)?(0|'[^$])?(-)?(\\d+)?(?:\\.(\\d+))?([bcdEefGgiopsuXx])/)){ # if(((arg = arguments[m[1] || i++])==null)||(arg==undefined)){throw('ArgumentError: too few arguments.');}; # if(/[^sp]/.test(m[7])&&!(typeof(arg)=='number')){throw('TypeError: can\\'t convert '+''+' into Integer');}; # switch (m[7]) { # case'b':str=arg.toString(2);break; # case'c':str=String.fromCharCode(arg);break; # case'd':str=parseInt(arg);break; # case'E':str=(m[6]?arg.toExponential(m[6]):arg.toExponential()).toUpperCase();break; # case'e':str=m[6]?arg.toExponential(m[6]):arg.toExponential();break; # case'f':str=m[6] ? parseFloat(arg).toFixed(m[6]) : parseFloat(arg); break; // Needs work # case'G':str='';break; # case'g':str='';break; # case'i':str=parseInt(arg);break; # case'o':str=arg.toString(8);break; # case'p':str=$q(arg).m$inspect();break; # case's':str=((arg = String(arg)) && m[6] ? arg.substring(0, m[6]) : arg); break; // Does this work? # case'u':str=arg<0?'..'+(Math.pow(2,32)+arg):arg;break; # case'X':str=arg.toString(16).toUpperCase();break; # case'x':str=arg.toString(16);break; # }; # a = (/[def]/.test(m[7]) && m[2] && str > 0 ? '+' + str : str); # c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' '; # x = m[5] - String(a).length; # if(m[5]){for(var c2=c,x2=x,ary2=[];x2>0;ary2[--x2]=c2);p=ary2.join('');}else{p='';}; # ary.push(m[4]?str+p:p+str); # }else{throw('ArgumentError: malformed format string');}; # }; # }; # f = f.substring(m[0].length); # } # return ary.join(''); #} end |