Number |
RUBY_VERSION => 1.8.6 |
RUBY_VERSION => 1.9.1 |
1 |
“ruby”[0] #=>114 |
“ruby”[0] #=> “r” |
2 |
“ruby”.unpack(‘U*’)
#=> [114,117,98,121] |
“ruby”.unpack(‘U*’)
#=> [1603,1604,1605,1575,1578] |
3 |
item=1
2.upto(4) do |item|
p item
end
# Outputs:
# 2
# 3
# 4
item
#=> 4 |
item=1
2.upto(4) do |item|
p item
end
# Outputs:
# 2
# 3
# 4
item
#=> 1 |
4 |
d=2
-> {d = 1 }.()
d
#=> 1
(changed the outer variable) |
d=2
-> (;d) {d=1}.()
d
#=> 2
(this didn’t, but you still get the shadowing warning) |
5 |
conferences.select do |name, _|
name == :lsrc
end
#=> [[:lsrc, “Austin”]] |
conferences.select do |name, _|
name == :lsrc
end
#=> {:lsrc => “Austin”} |
6 |
conferences.select do | data|
p data
end
# [:lsrc, “Austin”]
# [:scotland_on_rails, “Edinburgh”]
# [:railsconf_europe, “Berlin”]
(warning : multiple valiues for a block parameter(2 for 1 ) |
conferences.select do | data|
p data
end
# :lsrc
# :scotland_on_rails
# :railsconf_europe
conferences.select do | name,city|
p [name,city]
end
# [:lsrc, “Austin”]
# [:scotland_on_rails, “Edinburgh”]
# [:railsconf_europe, “Berlin”] |
7 |
>> {‘name’, “Akhil”}
=> {“name”=>”Akhil”}
>> {‘name’, “Akhil”}
=> {“name”=>”Akhil”} |
1. >> {‘name’, “Akhil”}
=> syntax error, unexpected ‘,’, expecting tASSOC
>> {name: “Akhil”}
=> {:name=>”Akhil”}
>> {‘name’, “Akhil”}
=> syntax error, unexpected ‘,’, expecting tASSOC
>> {name: “Akhil”}
=> {:name=>”Akhil”} |
8 |
>> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:b=>”B”, :c=>”C”, :d=>”D”, :a=>”A”}
>> hash.to_a
=> [[:b, “B”], [:c, “C”], [:d, “D”], [:a, “A”]]
>> hash.keys
=> [:b, :c, :d, :a]
>> hash.values
=> [“B”, “C”, “D”, “A”]
>> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:b=>”B”, :c=>”C”, :d=>”D”, :a=>”A”}
>> hash.to_a
=> [[:b, “B”], [:c, “C”], [:d, “D”], [:a, “A”]]
>> hash.keys
=> [:b, :c, :d, :a]
>> hash.values
=> [“B”, “C”, “D”, “A”] |
>> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:a=>”A”, :b=>”B”, :c=>”C”, :d=>”D”}
>> hash.to_a
=> [[:a, “A”], [:b, “B”], [:c, “C”], [:d, “D”]]
>> hash.keys
=> [:a, :b, :c, :d]
>> hash.values
=> [“A”, “B”, “C”, “D”]
>> hash = {:a=> ‘A’, :b=>’B’, :c=>’C’, :d=>’D’}
=> {:a=>”A”, :b=>”B”, :c=>”C”, :d=>”D”}
>> hash.to_a
=> [[:a, “A”], [:b, “B”], [:c, “C”], [:d, “D”]]
>> hash.keys
=> [:a, :b, :c, :d]
>> hash.values
=> [“A”, “B”, “C”, “D”] |
9 |
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> “b2c3d4a1”
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> “b2c3d4a1” |
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.to_s
=> “{:a=>1, :b=>2, :c=>3, :d=>4}”
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.to_s
=> “{:a=>1, :b=>2, :c=>3, :d=>4}” |
10 |
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each{|x| p x}
[:b, 2]
[:c, 3]
[:d, 4]
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each_pair{|x| p x}
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:b, 2]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:c, 3]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:d, 4]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each{|x| p x}
[:b, 2]
[:c, 3]
[:d, 4]
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each_pair{|x| p x}
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:b, 2]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:c, 3]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:d, 4]
(irb):48: warning: multiple values for a block parameter (2 for 1)
from (irb):48
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1} |
# >> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each_pair{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each_pair{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4} |
11 |
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]]
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]] |
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.select{|k,v| k == :c }
=> {:c=>3} |
12 |
Not Available |
“his name is Raveendran”.match(/name is(?\S+)/ )[:name]
#=> “Raveendran” |
13 |
Not Available |
File.read(“input.txt”).encoding
#=> # |
14 |
Not Available |
File.read(“input.txt”, encoding: ‘ascii-8bit’).encoding
# => # |
15 |
Not Available |
result = File.open(“input.txt, “r:euc-jp”) do |f|
f.read
end
result.encoding
# => #
result.valid_encoding?
# => true |
16 |
Not Available |
%w(Jazzez Ravi Kumar).map.with_index do |name,offset|
“{name} is #{offset +1}”
end
#=> [“Jazzez is #1″,”Ravi is #2″,”Kumar is #3”] |
17 |
Not Available |
[1,2,3,4].reduce(:+)
#=> 10
(Symbol or Symbol#to_proc)
[1,2,3,4].reduce(&:+)
#=> 10 |
18 |
Not Available |
array=[1,2,3,4,5]
array.take(3)
#=> [1,2,3]
array
#=> [1,2,3,4,5] |
19 |
Not Available |
array=[1,2,3,4,5]
array.drop(3)
#=> [4,5]
array
#=> [1,2,3,4,5] |
20 |
Not Available |
:foo[1]
#=> “o” |
21 |
Not Available |
:this === “this”
#=> true |