Category Ruby quiz (easy)

Ruby Quiz (Easy) #10

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

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]   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] 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. 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:  

Ruby quiz (easy) #3

Question Write a method my_map in the Array class that would do the same as Array#map using a block. Make sure this test passes: [1,2,3].my_map {|e| e+= 1} produces: [2,3,4]   Solution

Ruby quiz (easy) #2

Question Write a short ruby script which uses ‘open-uri’ to find out using a regular expression the rating of ‘Domino’ on imdb? Domino is a movie directed by Tony Scott and starring Keira Knightley. Solution

Ruby quiz (easy) #1

Question Run a ‘netcat’ on your machine like so: “nc -l -p 2222”. Write a short ruby script which will send the ‘hello’ string to it. Solution