developer tip

Ruby 1.8과 Ruby 1.9의 차이점은 무엇입니까?

copycodes 2020. 8. 16. 21:01
반응형

Ruby 1.8과 Ruby 1.9의 차이점은 무엇입니까?


루비의 "현재"버전 (1.8)과 "새"버전 (1.9)의 차이점은 명확하지 않습니다. 차이점에 대한 "쉬운"또는 "간단한"설명이 있으며 왜 그렇게 다른가요?


Sam Ruby에는 차이점을 설명 하는 멋진 슬라이드 쇼가 있습니다.

보다 쉽게 ​​참조 할 수 있도록이 정보를 인라인으로 가져오고 추상적 인 미래에 링크가 중단 될 경우를 대비하여 Sam의 슬라이드 개요는 다음과 같습니다. 슬라이드 쇼는 검토하기에 덜 부담스럽지 않지만 이와 같은 목록에 모두 배치하는 것도 도움이됩니다.

Ruby 1.9-주요 기능

  • 공연
  • 스레드 / 섬유
  • 인코딩 / 유니 코드
  • gems는 (대부분) 내장되어 있습니다.
  • if 문은 Ruby에서 범위를 도입하지 않습니다.

무엇이 변경 되었습니까?

단일 문자열.

루비 1.9

irb(main):001:0> ?c
=> "c"

루비 1.8.6

irb(main):001:0> ?c
=> 99

문자열 색인.

루비 1.9

irb(main):001:0> "cat"[1]
=> "a"

루비 1.8.6

irb(main):001:0> "cat"[1]
=> 97

{ "a", "b"} 더 이상 지원되지 않음

루비 1.9

irb(main):002:0> {1,2}
SyntaxError: (irb):2: syntax error, unexpected ',', expecting tASSOC

루비 1.8.6

irb(main):001:0> {1,2}
=> {1=>2}

조치 : {1 => 2}로 변환


Array.to_s 이제 구두점 포함

루비 1.9

irb(main):001:0> [1,2,3].to_s
=> "[1, 2, 3]"

루비 1.8.6

irb(main):001:0> [1,2,3].to_s
=> "123"

조치 : 대신 .join을 사용하십시오.


콜론이 When 문에서 더 이상 유효하지 않음

루비 1.9

irb(main):001:0> case 'a'; when /\w/: puts 'word'; end
SyntaxError: (irb):1: syntax error, unexpected ':',
expecting keyword_then or ',' or ';' or '\n'

루비 1.8.6

irb(main):001:0> case 'a'; when /\w/: puts 'word'; end
word

조치 : 세미콜론, then 또는 개행을 사용하십시오.


블록 변수는 이제 섀도우 로컬 변수

루비 1.9

irb(main):001:0> i=0; [1,2,3].each {|i|}; i
=> 0
irb(main):002:0> i=0; for i in [1,2,3]; end; i
=> 3

루비 1.8.6

irb(main):001:0> i=0; [1,2,3].each {|i|}; i
=> 3

Hash.index 사용되지 않음

루비 1.9

irb(main):001:0> {1=>2}.index(2)
(irb):18: warning: Hash#index is deprecated; use Hash#key
=> 1
irb(main):002:0> {1=>2}.key(2)
=> 1

루비 1.8.6

irb(main):001:0> {1=>2}.index(2)
=> 1

조치 : Hash.key 사용


Fixnum.to_sym 이제 사라

루비 1.9

irb(main):001:0> 5.to_sym
NoMethodError: undefined method 'to_sym' for 5:Fixnum

루비 1.8.6

irb(main):001:0> 5.to_sym
=> nil

(계속) Ruby 1.9

# Find an argument value by name or index.
def [](index)
  lookup(index.to_sym)
end

svn.ruby-lang.org/repos/ruby/trunk/lib/rake.rb


이제 순서가 지정되지 않은 해시 키

루비 1.9

irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"}
=> {:a=>"a", :c=>"c", :b=>"b"}

루비 1.8.6

irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"}
=> {:a=>"a", :b=>"b", :c=>"c"}

주문은 게재 신청서입니다.


더 엄격한 유니 코드 정규식

루비 1.9

irb(main):001:0> /\x80/u
SyntaxError: (irb):2: invalid multibyte escape: /\x80/

루비 1.8.6

irb(main):001:0> /\x80/u
=> /\x80/u

tr그리고 Regexp이제 유니 코드 이해

루비 1.9

unicode(string).tr(CP1252_DIFFERENCES, UNICODE_EQUIVALENT).
  gsub(INVALID_XML_CHAR, REPLACEMENT_CHAR).
  gsub(XML_PREDEFINED) {|c| PREDEFINED[c.ord]}

packunpack

루비 1.8.6

def xchr(escape=true)
  n = XChar::CP1252[self] || self
  case n when *XChar::VALID
    XChar::PREDEFINED[n] or 
      (n>128 ? n.chr : (escape ? "&##{n};" : [n].pack('U*')))
  else
    Builder::XChar::REPLACEMENT_CHAR
  end
end
unpack('U*').map {|n| n.xchr(escape)}.join

BasicObject 보다 잔인한 BlankSlate

루비 1.9

irb(main):001:0> class C < BasicObject; def f; Math::PI; end; end; C.new.f
NameError: uninitialized constant C::Math

루비 1.8.6

irb(main):001:0> require 'blankslate'
=> true
irb(main):002:0> class C < BlankSlate; def f; Math::PI; end; end; C.new.f
=> 3.14159265358979

조치 : :: Math :: PI 사용


위임 변경

루비 1.9

irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> String

루비 1.8.6

irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> C
irb(main):004:0>

결함 17700


$ KCODE 사용시 경고 발생

루비 1.9

irb(main):004:1> $KCODE = 'UTF8'
(irb):4: warning: variable $KCODE is no longer effective; ignored
=> "UTF8"

루비 1.8.6

irb(main):001:0> $KCODE = 'UTF8'
=> "UTF8"

instance_methods 이제 심볼의 배열

루비 1.9

irb(main):001:0> {}.methods.sort.last
=> :zip

루비 1.8.6

irb(main):001:0> {}.methods.sort.last
=> "zip"

응답 : instance_methods.include를 바꾸시겠습니까? method_defined?


Source File Encoding

Basic

# coding: utf-8

Emacs

# -*- encoding: utf-8 -*-

Shebang

#!/usr/local/rubybook/bin/ruby
# encoding: utf-8

Real Threading

  • Race Conditions
  • Implicit Ordering Assumptions
  • Test Code

What's New?

Alternate Syntax for Symbol as Hash Keys

Ruby 1.9

{a: b}

redirect_to action: show

Ruby 1.8.6

{:a => b}

redirect_to :action => show

Block Local Variables

Ruby 1.9

[1,2].each {|value; t| t=value*value}

Inject Methods

Ruby 1.9

[1,2].inject(:+)

Ruby 1.8.6

[1,2].inject {|a,b| a+b}

to_enum

Ruby 1.9

short_enum = [1, 2, 3].to_enum
long_enum = ('a'..'z').to_enum
loop do
  puts "#{short_enum.next} #{long_enum.next}"
end

No block? Enum!

Ruby 1.9

e = [1,2,3].each

Lambda Shorthand

Ruby 1.9

p = -> a,b,c {a+b+c}
puts p.(1,2,3)
puts p[1,2,3]

Ruby 1.8.6

p = lambda {|a,b,c| a+b+c}
puts p.call(1,2,3)

Complex Numbers

Ruby 1.9

Complex(3,4) == 3 + 4.im

Decimal Is Still Not The Default

Ruby 1.9

irb(main):001:0> 1.2-1.1
=> 0.0999999999999999

Regex “Properties”

Ruby 1.9

/\p{Space}/

Ruby 1.8.6

/[:space:]/

Splat in Middle

Ruby 1.9

def foo(first, *middle, last)

(->a, *b, c {p a-c}).(*5.downto(1))

Fibers

Ruby 1.9

f = Fiber.new do
  a,b = 0,1
  Fiber.yield a
  Fiber.yield b
  loop do
    a,b = b,a+b
    Fiber.yield b
  end
end
10.times {puts f.resume}

Break Values

Ruby 1.9

match =
   while line = gets
     next if line =~ /^#/
     break line if line.find('ruby')
   end

“Nested” Methods

Ruby 1.9

def toggle
  def toggle
    "subsequent times"
  end
  "first time"
end

HTH!


One huge difference would be the move from Matz's interpreter to YARV, a bytecode virtual machine that helps significantly with performance.


Many now recommend The Ruby Programming Language over the Pickaxe - more to the point, it has all the details of the 1.8/1.9 differences.


Some more changes:

Returning a splat singleton array:

def function
  return *[1]
end

a=function
  • ruby 1.9 : [1]
  • ruby 1.8 : 1

array arguments

def function(array)
  array.each { |v| p v }
end
function "1"
  • ruby 1.8: "1"
  • ruby 1.9: undefined method `each' for "1":String

참고URL : https://stackoverflow.com/questions/21574/what-is-the-difference-between-ruby-1-8-and-ruby-1-9

반응형