{"id":295,"date":"2022-01-13T16:20:32","date_gmt":"2022-01-13T09:20:32","guid":{"rendered":"http:\/\/thnkandgrow.com\/?p=295"},"modified":"2022-04-22T15:10:22","modified_gmt":"2022-04-22T08:10:22","slug":"ruby-map-and-return","status":"publish","type":"post","link":"https:\/\/thnkandgrow.com\/blog\/2022\/01\/13\/ruby-map-and-return\/","title":{"rendered":"[Ruby] Map and return?"},"content":{"rendered":"\n
H\u00f4m nay nh\u1eadn \u0111\u01b0\u1ee3c 1 c\u00e2u h\u1ecfi \u0111\u1ebfn t\u1eeb v\u1ecb tr\u00ed c\u1ee7a C\u01b0\u1eddngN\u0110, m\u00ecnh v\u1eabn ch\u01b0a t\u00ecm hi\u1ec3u l\u00fd do t\u1ea1i sao l\u1ea1i c\u00f3 k\u1ebft qu\u1ea3 kh\u00e1c nhau nh\u01b0 v\u1eady. Vi\u1ebft \u0111\u00e2y \u0111\u1ec3 nh\u1edb, mai m\u1ed1t t\u00ecm ra l\u00fd do r\u1ed3i update l\u1ea1i b\u00e0i blog n\u00e0y<\/p>\n\n\n\n
<\/p>\n\n\n\n
C\u00e2u h\u1ecfi:<\/p>\n\n\n\n
<\/p>\n\n\n\n
# ruby\narr =[[1,2], [3,4], [5,6]]\narr.map{|a,b| return a if a + b > 10}\n\n#> LocalJumpError (unexpected return)\nN\u1ebfu \u0111\u1ed5i l\u1ea1i th\u1ebf n\u00e0y\narr.map{|a,b| a if a + b > 10}\n#> [nil, nil, 5]<\/code><\/pre>\n\n\n\nV\u00e0 n\u1ebfu \u0111\u1ec3 v\u00e0o method nh\u01b0 th\u1ebf n\u00e0y<\/p>\n\n\n\n
# ruby\n\n\ndef test_arr\n arr =[[1,2], [3,4], [5,6]]\n arr.map{|a, b| return a if a + b > 10}\nend\n\n#> 5<\/code><\/pre>\n\n\n\n