루비 알송달송
프로그래밍 언어 퀴즈
Biweekly Lecture
2015-08-25
Ruby on Rails Korea
- ROR Lab. Season 4 -

1. val1 과 val2 의 실행결과
# hint: output of this statement in IRB is NOT value of val1!
val1 = true and false
val2 = true && falseval1 과 val2 의 실행결과
# hint: output of this statement in IRB is NOT value of val1!
val1 = true and false
val2 = true && false
# results in val1 being equal to true
> (val1 = true) and false
# results in val2 being equal to false
> val2 = (true && false) 2.실행결과 "false"로 출력되는 것은
true ? "true" : "false" # 1
false ? "true" : "false" # 2
nil ? "true" : "false" # 3
1 ? "true" : "false" # 4
0 ? "true" : "false" # 5
"false" ? "true" : "false" # 6
"" ? "true" : "false" # 7
[] ? "true" : "false" # 8실행결과 "false"로 출력되는 것은
true ? "true" : "false" # 1
false ? "true" : "false" # 2 <==
nil ? "true" : "false" # 3 <==
1 ? "true" : "false" # 4
0 ? "true" : "false" # 5
"false" ? "true" : "false" # 6
"" ? "true" : "false" # 7
[] ? "true" : "false" # 83. 해시의 '키'로 정렬하세요.
# 해시
{ abc: 'hello', 'another_key' => 123, 4567 => 'third' }
# ==> 정렬 결과
["abc", "4567", "another_key"]해시의 '키'로 정렬하세요.
{ abc: 'hello', 'another_key' => 123, 4567 => 'third' }
["abc", "4567", "another_key"]
hsh.keys.map(&:to_s).sort_by(&:length)
hsh.keys.collect(&:to_s).sort_by { |key| key.length }
def key_sort hsh
hsh.keys.collect(&:to_s).sort { |a, b| a.length <=> b.length }
end
# collect(&:to_s) <===> # collect { |x| x.to_s }def times_two(arg1);
puts arg1 * 2;
end
def sum(arg1, arg2);
puts arg1 + arg2;
end
# 실행결과는?
times_two 5
times_two(5)
times_two (5)
sum 1, 2
sum(1, 2)
sum (1, 2)4. 메소드 파라미터
def times_two(arg1); puts arg1 * 2; end
def sum(arg1, arg2); puts arg1 + arg2; end
# 실행결과는?
times_two 5 # => 10
times_two(5)
times_two (5) # OK, single argument
sum 1, 2 # => 3
sum(1, 2)
sum (1, 2)
# syntax error, unexpected ',', expecting ')'
# sum (1, 2) # (1, 2) is expression
VAL = 'Global'
module Foo
VAL = 'Foo Local'
class Bar
def value1
VAL
end
end
end
class Foo::Bar
def value2
VAL
end
end5. 변수 스코프
Foo::Bar.new.value1
#
Foo::Bar.new.value2
#VAL = 'Global'
module Foo
VAL = 'Foo Local'
class Bar
def value1
VAL
end
end
end
class Foo::Bar
def value2
VAL
end
end변수 스코프
Foo::Bar.new.value1
# => 'Foo Local'
Foo::Bar.new.value2
# => 'Global'
== – 양쪽의 값이 같은지 비교 (대체로 클래스 오버라이드)
=== – case 구문에서 사용 (대체로 클래스에 따라 오버라이드)
eql? – 양쪽의 값과 타입을 비교 (== 오퍼레이드는 타입 무시)
1 == 1.0 # true,
1.eql?(1.0) # false
equal? – 같은 오브젝트를 가리키는지 비교 루비 버추어머신의
메모리에 있는 오브젝트 아이디가 같아야함.6. ==, ===, eql? 오퍼레이터 비교
7. 문자열 연결 오퍼레이터
foo = "foo"
foo2 = foo
foo.concat "bar"
puts foo
=>
puts foo2
=>
foo += "baz"
puts foo
=>
puts foo2
=>
foo = "foo"
foo2 = foo
foo.concat "bar"
puts foo
=>
puts foo2
=>
foo += "baz"
puts foo
=>
puts foo2
=>
문자열 연결 오퍼레이터
foo = "foo"
foo2 = foo
foo.concat "bar"
puts foo
=> "foobar"
puts foo2
=> "foobar"
foo += "baz"
puts foo
=> "foobarbaz"
puts foo2
=> "foobar"
8. inject 로 피보나치 메소드 작성
(1..20).inject( ) { | fib | fib << fib.last( ).inject( ) }inject 로 피보나치 메소드 작성
(1..20).inject( [0, 1] ) { | fib | fib << fib.last(2).inject(:+) }감사합니다
루비 알송당송 프로그래밍 언어 퀴즈
By wagurano
루비 알송당송 프로그래밍 언어 퀴즈
루비 프로그래밍언어 퀴즈
- 1,292