Ruby – get the file being executed
Use methods in the File class to manipulate FILE Inside test.rb, I have puts __FILE__ puts File.dirname(__FILE__) puts File.basename(__FILE__) When you run, ruby a/b/test.rb You get a/b/test.rb a/b test.rb
Use methods in the File class to manipulate FILE Inside test.rb, I have puts __FILE__ puts File.dirname(__FILE__) puts File.basename(__FILE__) When you run, ruby a/b/test.rb You get a/b/test.rb a/b test.rb
Extract the attributes in to an array, assign them to a constant, then splat them in to attr_accessor. class SubClass < MyBaseClass ATTRS = [:id, :title, :body] attr_accessor(*ATTRS) end Now you can access them via the constant: puts SubClass.ATTRS #=> [:id, :title, :body]
Incase, while trying to update a bundle, you get an error “You have to install development tools first.” on OSX Mavericks (OSX 10.9), but you already have Xcode installed, you can still manually install the dev tools via terminal: xcode-select –install Do that and follow on-screen instructions. Your problem will be solved.
There are many ways to do this. Your lack of context makes it impossible to elect a “best” way. Here’s a few ayways. Kernel.const_get(:Bob) eval(:Bob.to_s) Kernel.const_get(:bob.to_s.capitalize)
You can monkey-patch the Hash class class Hash def to_o JSON.parse to_json, object_class: OpenStruct end end then you can say h = { a: ‘a’, b: ‘b’, c: { d: ‘d’, e: ‘e’} } o = h.to_o o.c.d # => ‘d’ See Convert a complex nested hash to an object.
You can use: = f.input :remember_me, as: :boolean, inline_label: ‘Remember me’, label: false
Try: MyClass.ancestors.select {|o| o.class == Module } for example: >> Array.ancestors.select {|o| o.class == Module} => [Enumerable, Kernel] UPDATE To get the modules mixed into an object instance at runtime you’ll need to retrieve the eigenclass of the instance. There is no clean way to do this in Ruby, but a reasonably common idiom is …
Braces define an anonymous function, called a block. Tokens between the pipe are the arguments of this block. The number of arguments required depends on how the block is used. Each time the block is evaluated, the method requiring the block will pass a value based on the object calling it. It’s the same as …
No, Ruby does not have inline comments. Comments of this style have a tendency to reduce readability, since they makes the code harder to follow. In your case it would be best to split your array items into separate rows and comment out the one row. my_array = [‘first’, # ‘second’, ‘third’, ‘fourth’]
I do not know how you manage to do this, but the line endings \r\n in your string seem to be there as 4-byte character sequences, not as 2-byte escaped CRLF. If I copy your file into a ruby string with single ticks: unescaped=’PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48cmV2aWV3LWNhc2UgY3JlYXRl\r\nZGF0ZT0iMTMvTWFyLzIwMTQgMDk6MDQ6NTEiIHN5c3RlbT0iVHJhZmlndXJhX1RlbXBsYXRlX01h\r\nbmFnZW1lbnRfdjUuMSIgYmF0Y2hpZD0iMCIgdHJhbnNhY3Rpb25ubz0iMSIgYmF0Y2huYW1lPSJH’ Base64.decode64(unescaped) #=> garbled text for every second line if I do …