Ruby
åæ© ç¾æç¹ã§Rubyã®ææ°çã¯2.4, ãµãã¼ãããã¦ããã®ã¯2.2ã¾ã§ã§ã2.1以åã¯ã¡ã³ããã³ã¹å¯¾è±¡å¤ã§ããããã®ãããªå¤ããã¼ã¸ã§ã³ãåããã®ã¯æ±ºãã¦ãããã¨ã§ã¯ãªãã ãããå®ã®ã¨ãããç¾å¨ã§ãå¤ãã®Windowsãã·ã³ã§ãRuby1.8.1ã¨Ruby1.9.2p0ãåä½â¦
ä»æ¥ã¯çµå±ä½ãæ¸ããªãã£ãã®ã§ãã¾ã½ã¼ã(ãã´ã½ã¼ãã®ä»²é)ãé©å½ã«æ¸ããã #!/usr/bin/ruby1.9.1 # -*- coding: utf-8 -*- class Array def bozo_sort! defined? yield or return bozo_sort!{|a,b|a<=>b} until (1...size).inject(true){|s,i|s && yielâ¦
ããã¸ãããã§ãã¾ããï¼ Ruby1.9 def fun(n, s) s.scan(/#{"(.)"*n}/).transpose.map &:join end p fun(3, "123123123") æéã¨ã測ã£ã¦ãªããã©ããã¨ä¸ç¬ã ã£ãããããªãããªãhttp://ameblo.jp/programming/entry-10001721422.html
#!/usr/bin/ruby1.9 # -*- coding: utf-8 -*- ObjectSpace.each_object(Class).select{|i|i.method_defined? :floor }.each do|clazz| clazz.class_exec do alias_method :floor_old, :floor begin remove_method :floor def floor floor_old+0.2 end rescueâ¦
æçæ°ã®å¹³æ¹æ ¹ãããã®åè¨å¤ãå¼ãä¿æãã¦æ±ãã¾ãã(è¤ç´ æ°ã®ç¯å²ã¯ãµãã¼ããã¾ãã)Wikipediaã®ãã£ããããæ°#ä¸è¬é ã«ãããããªå¼ãæ±ã£ããã§ãã¾ããã³ã¼ã:RtNumber: handles sum of rational numbers' sqrt â Gistã³ã¼ãã«ä»å±ãã¦ããµã³ãã«: â¦
ãªãã§Primeãmathn.rbã«å ¥ã£ã¦ããã ããã 追è¨: Ruby1.9.0ã®Revision 19095ãããããprime.rbãä½æãããã®ã§ãã®ãããªæ¹æ³ã¯ä¸è¦ãªããã§ãã unless $".include? "mathn.rb" module Math alias sqrt_tmp sqrt end load "mathn.rb" class Fixnum remoâ¦
qnighy's gist: 178354 â Gist #!/usr/bin/ruby -Ku # -*- coding: utf-8 -*- r = /\A(([ãããããã-ããããã-ãã-ãã¢ã¤ã¦ã¨ãªã«-ã¢ã¤ã¦ã¨ã©-ãã¯-ã´]|ãã)[ããã ãããã ããã¡ã£ã¥ã§ã©ã£ã¥ã§ã®]?){2}[Ãâ]?(([ãããããã-ããããã-ãã-ãã¢ã¤â¦
ã¹ã¼ãã¼ãªãã¬ã¯ãã£ã³ã°ã¿ã¼ã¤ã qnighy's gist: 145639 â Gist 以ä¸ã®ããã«ã¤ããã import java.util.HashMap; import static org.acikelabo.RubyInspect.p; public class Test { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(</string,></string,>â¦
15ããºã«ã®ç¶æ ã¯2ã¤ã®éåã«ãããããçæ¹ããããçæ¹ã®ç¶æ ã«é·ç§»ãããã¨ã¯ãªããã©ã¡ãã®éåã«æå±ãã¦ãããããããå¤ã§è¡¨ç¾ãã¦ã¿ãã 並ã³ã®ããªãã£ã¼ 15ããºã«ã®åé¨åã«0ãã15ã®çªå·ãã¤ãããä»åã¯ãæ¿ã¯çªå·ãã®ã¾ã¾ãç©´ã¯0ã¨ãããã¾ããâ¦
map ãé¢åãªã®ã§ DelegateMap - ã¾ãããã«è§¦çºããã¦ããã¹ãã³ã¼ã require "with" class A attr_accessor :ins def test self.ins = "instance variable" lo = "local variable" with(["target variable"]) do # sorry can't use self... p ins p lo p aâ¦
#!/usr/bin/ruby require "rational" def p1 p (0...1000).select {|i|i%3==0||i%5==0}.inject(0) {|a,b|a+b} end def p2 a,b = 1,1 sum = 0 while b<=4000000 sum += b if b%2==0 a,b = b,a+b end p sum end def p3 a=600851475143;(2..a).each{|i|i
#!/usr/bin/ruby #ans 1 a=600851475143 i=2 while i
a,b = 1,1 sum = 0 while b<=4000000 sum += b if b%2==0 a,b = b,a+b end p sum
#!/usr/bin/ruby #ans 1 p (0...1000).select {|i|i%3==0||i%5==0}.inject(0) {|a,b|a+b} #ans 2 require "enumerator" p ((0...1000).to_enum(:step,3).to_a+(0...1000).to_enum(:step,5).to_a).uniq.inject(0) {|a,b|a+b}
nowa ãµã¼ãã¹çµäºã®ãç¥ãã Re: Enumerable#mins_by - ãããã ååï¼ //minsby.c #include <ruby.h> static ID id_cmp, id_each; static VALUE mins_by_i(VALUE i, VALUE *memo, int argc, VALUE *argv) { VALUE v; //ENUM_WANT_SVALUE(); if(argc == 0) { i = Q</ruby.h>â¦
STLã®ã½ã¼ã¹ã®ä¸¸åãã ãã class Array def swap_at(i, j) self[i], self[j] = self[j], self[i] end def reverse_in(first, last) return if first == last last -= 1 while first < last swap_at(first, last) first += 1 last -= 1 end end def next_perâ¦
ã¢ã«ã´ãªãºã ã®éããé度ã«æ確ã«è¡¨ããããããããä¾ã®ã²ã¨ã¤ã§ãããã£ããããæ°åã使ã£ã¦é度æ¯è¼ãæ°è»½ã«å¤åé·ã使ããè¨èªã§ãªãã¨ãç·å½¢ã¨logã®éãã¯è¦ãã¥ããã #!/usr/bin/ruby -Ku require 'matrix' def fib_recursive(n) case n when 0 retâ¦
æ°å¦ãªãªã³ããã¯äºé¸ããã¦ãã¾ãããããã°ã©ã ã§è§£ããããªåé¡ããã£ãã®ã§ãçãããããã¦ã¿ã¾ããã #!/usr/bin/ruby -Ku require "rational" anssum = 0 (-100..400).each do |i| x = Rational(i, 44) sum = 0 (1..9).each do |j| sum += (x*j).flooâ¦
ç§ãPythonãé¸ãã çç±(ä¿®æ£:10/29 15:37) - CanIâs Diary æ¬äººéè¨ãææ³ã«é¢ãã¦ã¯ãã¾ã£ãããã®éãã ã¨æãããã ããã£ã±ãéè¦ãªã®ã¯ãRubyã¯ã¹ã¯ãªããè¨èªã ã¨ãããã¨ã ã¨æããRubyã¨Pythonã¯ãæ©è½ã¯ä¼¼ã¦ããããè¨èªã®æ§è³ªã¯ã ãã¶éãã¨æãâ¦
åæ°ãã循ç°å°æ°ã¸ã®å¤æã§ãåç´ãªçç®ã¢ã«ã´ãªãºã ã«å¤æ´ããã¨ãã¹ããæ¹è¯ãããassert_equal(æå¾ ããå¤, è¨ç®å¼)ã¨ããå½¢å¼ã§å¼ã¶ã®ããã¹ãããªï¼ #repeating-decimal.rb require 'rational' class Rational def self.from_rd(s) case s when /^(-?)â¦
Rubyã§ãã¹ãã±ã¼ã¹ä½ãã®ã¯ããã¦ããã¹ãã±ã¼ã¹ã®æ¸ãæ¹ãããããããã¦ããããå¤ããã㨠ã¾ããreadlineâprintã¨ããã«ã¼ãã³ãããå¼æ°âæ»ãå¤ã¨ããã«ã¼ãã³ã«ããã ãããRationalã®ã¡ã½ããã¨ãã¦çµã¿è¾¼ãã ã mathn.rbã¯ã·ã¹ãã å ¨ä½ã«å½±é¿ãä¸â¦
Rubyã¯ç´ æ´ãããè¨èªã ã(ããã§ææ 表ç¾ããã¦ããããã©ã¢ã¬ãªå 容ã«ãªããããªã®ã§ãããã)ã ãã©ãRubyã¦ã¼ã¶ã¼ãå¢ããããã«ã¯ãã¾ã ã¾ã é害ã大ãããããã¦ãã®ä¸ã«ã¯ãé常ã«äºç´°ãªåé¡ãããã¨æããä¾ãã°ãå¥ã®è¨èªã®ã¦ã¼ã¶ã¼ãRubyã«è§¦ããâ¦
ã¾ããæ¨æ¥æ¸ããå°æ°âåæ°ã®ã³ã¼ãã«ä¸åããã£ããä¸è¦ã #!/usr/bin/ruby -Ku require 'mathn' case readline when /^(-?)([0-9]+)$/ print $1, $2.to_i, $/ when /^(-?)([0-9]*)\.([0-9]*)$/ print $1, $2.to_i + $3.to_i / 10**$3.size, $/ when /^(-?)â¦
é¨æ´»ã®å宿ã§ã循ç°å°æ°ãåæ°ã«ç´ãã®ã¨ããã課é¡ãããã¨èª°ããè¨ã£ã¦ãã¦ãRubyã ã¨ç°¡åã«ä½ãããã ã£ãã®ã§ä½ã£ã¦ã¿ãã³ã¼ãã #!/usr/bin/ruby require 'mathn' case readline when /^(-?)([0-9]+)$/ print $1, $2.to_i, $/ when /^(-?)([0-9]*)\.([0â¦