티스토리 뷰
the simple way if check value is null
- object&.spouse&.name == object.spouse.name if object && object.spouse
class << self- Method defined within
class << objblock will be added to theobjobject. objcan be any object.
- Method defined within
hash array sort by multiple condition
arr = [{"count"=>2, "hit"=>2, "name"=>"1"}, {"count"=>2, "hit"=>1, "name"=>"1"}, {"count"=>1, "hit"=>5, "name"=>"1"} ] # sort sorted_arr = arr.sort_by {|data| [data["count"], data["hit"]]}결과
[{"count"=>1, "hit"=>5, "name"=>"1"}, {"count"=>2, "hit"=>1, "name"=>"1"}, {"count"=>2, "hit"=>2, "name"=>"1"}]it's different type
hash = {"count"=>2, "hit"=>2, "name"=>"1"}
hash.keys[0].class # => String
hash = {:count=>2, :hit=>2, :name=>"1"}
hash.keys[0].class # => Symbol
console에서 module reload
load "#{Rails.root}/lib/yourfile.rb"
module 도 변수를 가질 수 있다. 하지만 static member 와 유사
hash 를 다른 hash 로 만들 때
hash.inject({}) |m,e|와 같이 사용byebug
- rake 든 rails 에서 실행하든 byebug 가 있으면 걸림
- c : 다음 byebug 까지 실행
- step: 한 줄씩 실행
is it good style to explicitly return in Ruby
- 굳이 return 을 빼서 명료하지 않게 할 이유는 없음. return 을 명시해주는게 다른 개발자가 이해하기에도 좋음
find_by vs where
- where 로 하면 string 으로 조건절 작성. 마지막에 take 를 하거나 each do end 문을 돌려야 함
- find_by 는 하나만 가져옴
class vs module

ruby hash array sort
- https://stackoverflow.com/questions/3154111/how-do-i-sort-an-array-of-hashes-by-a-value-in-the-hash
- hash = {"five" => 5, "ten" => 10}
- hash.keys[0].class // String
- hash = {:five => 5, :ten => 10}
- hash.keys[0].class // Symbol
'Ruby' 카테고리의 다른 글
| ruby에서 exception 처리 (0) | 2023.06.20 |
|---|---|
| ActiveAdmin index column width 변경하는 방법 (0) | 2023.06.20 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- IntelliJ
- aws
- DesignSystem
- 실용주의
- Java
- Generic
- 이펙티브자바
- annotation
- querydsl
- ruby
- effective-java
- 암호화
- ActiveAdmin
- 코테 log
- Lombok
- gitignore
- point
- SHA
- SQL 전문가 가이드
- TroubleShooting
- WebClient
- 메모리 릭
- 이벤트스토밍
- fetchResults
- 사고..
- Git
- Spring-Boot
- Encoding
- sort algorithm
- ASCII
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
글 보관함