Category Ruby quiz (easy)

Ruby Quiz (Easy) #10

Question How do you make this line a one-liner?     Solution value = [compute_value,CONSTANT].max Advertisements

Ruby Quiz (Easy) #9

Question What will be the output of the following snippet: Solution If run in user’s account, the output will be: /home/user /home/user/test Surprised? So was I.

Ruby Quiz (Easy) #8

Question What would be the output of this code snippet? Hint: note the type of the key in the hash and the type of the key when accessing the hash.   Solution The output is nil because the hash is defined using symbols, and the access to it was done using a string. Rails’ active_support […]

Ruby Quiz (Easy) #7

Question What would be the output of this code snippet? [gist https://gist.github.com/cawel/69131717ccbe95700e67]   Solution The output would be [1, 2, 3]. The entire array is not modified by the Array#each method. This is because the iterator, while traversing the collection, passes a copy of each element in turn, to the block. If you want to dig […]

Ruby Quiz (Easy) #6

Question what is the output of those 3 tricky puts? [gist https://gist.github.com/cawel/f291d03c2c40b0e696a9] Solution false z + 2 = 3 z +2 = 3 First puts You might have expected 1 as the answer. However, x = y or z actually evaluates as if it were (x = y) or z, thus assigning y to x. The […]

Ruby quiz (easy) #5

Question Monkeypatch the Time class by adding a next_business_day method, which takes the amount of business days as a parameter. Note: the active_support gem will be helpful for ‘navigating’ days, e.g. Time.now.tomorrow To make things clear, this test case should pass: Solution Thoughts Say you want to find the 1000th business day, it is not […]

Ruby quiz (easy) #4

Question What is the difference between those two options to convert a candidate value into an integer: Integer(candidate) and candidate.to_i ? Solution Integer(candidate) will raise an ArgumentError if the candidate value cannot be converted into an integer value, whereas :to_i will give 0. See for yourself: