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?(x) ⇒ 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(*args) ⇒ 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?(x) ⇒ Boolean
FIX: Incomplete
1307 1308 1309 |
# File 'lib/source/ruby.rb', line 1307 def block_given?(x) `typeof(x)=='function'` end |
#format(string) ⇒ Object
1315 1316 1317 |
# File 'lib/source/ruby.rb', line 1315 def format(string) `m$sprintf(string)` end |
#global_variables ⇒ Object
FIX: Incomplete
1320 1321 |
# File 'lib/source/ruby.rb', line 1320 def global_variables end |
#lambda(func) ⇒ Object
call-seq:
proc { |...| block } -> a_proc
lambda { |...| block } -> a_proc
Equivalent to Proc.new …
FIX: Incomplete
1330 1331 1332 1333 1334 1335 |
# File 'lib/source/ruby.rb', line 1330 def lambda(func) `var result=new(c$Proc)()` `result.__block__=func` `result.__block__.__id__=Red.id++` return `result` end |
#local_variables ⇒ Object
FIX: Incomplete
1338 1339 |
# File 'lib/source/ruby.rb', line 1338 def local_variables end |
#loop(&block) ⇒ Object
call-seq:
loop { || block }
Repeatedly executes the block.
1346 1347 1348 1349 1350 |
# File 'lib/source/ruby.rb', line 1346 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
1371 1372 1373 1374 1375 1376 |
# File 'lib/source/ruby.rb', line 1371 def proc(func) `var result=new(c$Proc)()` `result.__block__=func` `result.__block__.__id__=Red.id++` return `result` end |
#putc(int) ⇒ Object
call-seq:
putc(int) -> int
FIX: Incomplete
1382 1383 |
# File 'lib/source/ruby.rb', line 1382 def putc(int) end |
#puts(obj) ⇒ Object
call-seq:
puts(obj, ...) -> nil
FIX: Incomplete
1389 1390 1391 1392 1393 |
# File 'lib/source/ruby.rb', line 1389 def puts(obj) `var string=obj.m$to_s&&obj.m$to_s()||$q(''+obj)` `console.log(string.__value__.replace(/\\\\/g,'\\\\\\\\'))` return nil end |
#raise(*args) ⇒ Object
FIX: Incomplete
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 |
# File 'lib/source/ruby.rb', line 1396 def raise(*args) `var exception_class=c$RuntimeError,msg=$q('')` `if(arguments[0]&&arguments[0].m$is_a_bool(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
1427 1428 1429 1430 |
# File 'lib/source/ruby.rb', line 1427 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
1445 1446 1447 1448 |
# File 'lib/source/ruby.rb', line 1445 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
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 |
# File 'lib/source/ruby.rb', line 1451 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$to_s&&arg.m$to_s().__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:
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 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 |
# File 'lib/source/ruby.rb', line 1491 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 |