This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[35] pry(main)> total = words.inject(0){ |result, word| word.size > 3 ? word.size + result : result} | |
=> 14 | |
[36] pry(main)> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[32] pry(main)> words = %w{mary had a little lamb} | |
=> ["mary", "had", "a", "little", "lamb"] | |
[33] pry(main)> total = words.inject(0){ |result, word| word.size + result} | |
=> 18 | |
[34] pry(main)> total = words.inject(0){ |result, word| word.size + result if word.size > 3} | |
TypeError: nil can't be coerced into Fixnum | |
from (pry):52:in `+' | |
[35] pry(main)> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
k = [1,2,3,4] | |
k.each do |c| | |
puts c | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountAction implements Action | |
{ | |
@Override | |
public void process(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
//do your processing here, take values from DB, may be! | |
SomeBean user = new SomeBean("Paddy") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Tree | |
class Node | |
attr_reader :word, :left, :right | |
include Enumerable | |
def initialize(value) | |
@word = value | |
@left = @right = nil | |
end | |
def insert(node, dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Observable | |
def initialize | |
@observers = [] | |
end | |
def observe(o) | |
@observers << o | |
end | |
def called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Whenever we visit the cell in the pattern, we call this method | |
to get the next state of the cell when the Tick happens. | |
Here, we add the values of all the eight cells around the visited cell | |
and apply the game_of_life rules as specified in the comments below. | |
=end | |
def next_gen(seed, row, col) | |
next_state = 0 | |
(-1..1).each do |xdir| | |
(-1..1).each do |ydir| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#To find the equivalent of 582 in base-14 NS do this : | |
582.to_s(14) #will return "2d8" | |
#and vice versa | |
"2d8".to_i(14) #will return 582 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This program shows various ways of writing 100 | |
require 'base_converter' | |
bc = BaseConverter.new | |
(2..36).each do |num| | |
x = bc.dec_to_base(num,100) | |
puts "100 in base-#{num} is written as #{x}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a, b = 5, 10 | |
a, b = b/a, a-1 # => [2, 4] | |
a, b, c = 'A', 'B', 'C' | |
a, b, c = [a, b], { b => c }, a | |
puts a # => ["A", "B"] |
NewerOlder