我有两个同名的Const; 一个是全局const,另一个是在命名空间Admin下定义的.但是我需要区分它们;全局的已经定义了,如果还没有定义,那么作用域需要自动定义:
A = 'A Global Const'
module Admin
A = 'A Const within the Admin namespace' if const_defined? 'A' # always true and the Admin::A can never be defined!
end
puts A # => 'A Global Const'
puts Admin::A # => NameError: uninitialized constant Admin::A
# the Admin::A will never be defined.
Run Code Online (Sandbox Code Playgroud)
但如果定义了全局A,那么"const_defind?" 部分将永远回归!
我甚至尝试过:
... if defined? A
... if self.const_defined? 'A'
... if Object.const_get('Admin').const_defined? 'A'
Run Code Online (Sandbox Code Playgroud)
总是如此!
我需要区分它们,因为我需要使用A中的A和Admin :: A两种形式;
像PostsController一样供公众使用,Admin :: PostsController供管理员使用;
救命!
<%= will_paginate(@posts) %>
# will generate the links like this '<a href="/posts?page=n">a link</a>'
Run Code Online (Sandbox Code Playgroud)
如果我想更改href基础等/contents,我该怎么办<a href="/contents?page=n">a link</a>?
似乎没有选择,帮助!
假设我有一个模型:
class Post
end
posts = Post.where(***)
puts posts.class # => ActiveRecord::Relation
Run Code Online (Sandbox Code Playgroud)
那么如何通过变量'posts'获取模型类名称,也许是一些名为model_class_name的方法:
puts posts.model_class_name#=> Post
谢谢 :)
我的代码:
package net.tq5.bubbleexplosion;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class BubbleExplosionActivity extends Activity {
public static final String TAG = "BubbleExplosionActivity";
/** Called when the activity is first created. */
private FrameLayout parent;
private ExplosionView customView;
private AnimationDrawable exal;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set no title;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Create a FrameLayout and set the background; …Run Code Online (Sandbox Code Playgroud)